index.jsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { ElCheckbox, ElCheckboxButton, ElCheckboxGroup } from 'element-plus'
  2. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  3. import { formInputProps, fromInputEmits } from '../../form-input-props'
  4. import { computed, getCurrentInstance, h } from 'vue'
  5. export default {
  6. props: formInputProps,
  7. emits: [...fromInputEmits],
  8. setup (props, context) {
  9. const instance = getCurrentInstance()
  10. const { width, updateStream, ...formInput } = useFormInput(props, context)
  11. const { optionProps, options, getOptions, proxyOptionsValue } = useOptions(props, true, updateStream)
  12. instance.ctx.getOptions = getOptions // 此处代码的意义
  13. const isButton = computed(() => {
  14. return props.config?.isButton ?? false
  15. })
  16. const display = computed(() => {
  17. return props.config?.display ?? 'inline-flex'
  18. })
  19. const getCheckboxComponent = (isButton) => {
  20. return isButton ? ElCheckboxButton : ElCheckbox
  21. }
  22. const checkboxItem = (option) => {
  23. return h(getCheckboxComponent(isButton.value), {
  24. label: option[optionProps.value.value] ?? option,
  25. style: { display: display.value }
  26. }, {
  27. default: () => `${option[optionProps.value.label] ?? option}`
  28. })
  29. }
  30. const checkboxItems = () => options.value.map(checkboxItem)
  31. // [BUG EL2.2.17] ElCheckboxGroup 存在bug
  32. // modelValue 默认值为空数组 内部watch props.modelValue后进行数据校验,导致初始化时及触发数据验证
  33. // [TEMP FIX] 临时解决方案 当为空数组时 赋值为 undefined
  34. return () => h(ElCheckboxGroup, {
  35. ...context.attrs,
  36. ...formInput,
  37. style: { width: width.value },
  38. disabled: props.disabled,
  39. modelValue: proxyOptionsValue.value.length === 0 ? undefined : proxyOptionsValue.value,
  40. 'onUpdate:modelValue': (value) => {
  41. proxyOptionsValue.value = value
  42. }
  43. },
  44. { default: () => checkboxItems() })
  45. }
  46. }