index.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, ref, watch } from 'vue'
  5. import BasicDivider from '../divider'
  6. import { isObject } from '@cip/utils/util'
  7. export default {
  8. inheritAttrs: false,
  9. props: formInputProps,
  10. emits: [...fromInputEmits],
  11. setup (props, context) {
  12. const instance = getCurrentInstance()
  13. const { width, updateStream, securityConfig, ...formInput } = useFormInput(props, context)
  14. const { optionProps, options, getOptions, proxyOptionsValue } = useOptions(props, true, updateStream)
  15. instance.ctx.getOptions = getOptions // 此处代码的意义
  16. const isButton = computed(() => {
  17. return props.config?.isButton ?? false
  18. })
  19. const display = computed(() => {
  20. return props.config?.display ?? 'inline-flex'
  21. })
  22. const getCheckboxComponent = (isButton) => {
  23. return isButton ? ElCheckboxButton : ElCheckbox
  24. }
  25. const checkboxItem = (option) => {
  26. return h(getCheckboxComponent(isButton.value), {
  27. key: option[optionProps.value.value] ?? option,
  28. label: option[optionProps.value.value] ?? option,
  29. style: { display: display.value, width: securityConfig.value.itemWidth ?? undefined },
  30. disabled: option[optionProps.value.disabled] ?? false
  31. },
  32. () => `${option[optionProps.value.label] ?? option}`
  33. )
  34. }
  35. const isIndeterminate = ref()
  36. const checkAll = ref(false)
  37. // WARN: 判断是否权限监听开关不具有响应性请注意
  38. if (securityConfig.value.checkAllAble) {
  39. watch([options, proxyOptionsValue], () => {
  40. if (proxyOptionsValue.value.length === options.value.length && options.value.length > 0) {
  41. checkAll.value = true
  42. } else {
  43. checkAll.value = false
  44. }
  45. }, { immediate: true })
  46. }
  47. const handleCheckAllChange = (val) => {
  48. if (val) {
  49. proxyOptionsValue.value = options.value.map(option => isObject(option) ? option[optionProps.value.value] : option)
  50. } else {
  51. proxyOptionsValue.value = undefined
  52. }
  53. isIndeterminate.value = false
  54. checkAll.value = val
  55. }
  56. const handleCheckItemChange = (val) => {
  57. const checkedLen = val.length
  58. const allCheckLen = options.value.length
  59. checkAll.value = checkedLen === allCheckLen
  60. isIndeterminate.value = checkedLen > 0 && checkedLen < allCheckLen
  61. }
  62. const checkboxItems = () => options.value.map(checkboxItem)
  63. // [BUG EL2.2.17] ElCheckboxGroup 存在bug
  64. // modelValue 默认值为空数组 内部watch props.modelValue后进行数据校验,导致初始化时及触发数据验证
  65. // [TEMP FIX] 临时解决方案 当为空数组时 赋值为 undefined
  66. // 新增checkAllAble,但注意此功能暂时不支持存在disabled的options
  67. return () => [
  68. securityConfig.value.checkAllAble
  69. ? [
  70. h(ElCheckbox, {
  71. modelValue: checkAll.value,
  72. indeterminate: isIndeterminate.value,
  73. onChange: (val) => handleCheckAllChange(val)
  74. }, () => '全选'),
  75. h(BasicDivider)
  76. ]
  77. : undefined,
  78. h(ElCheckboxGroup, {
  79. ...context.attrs,
  80. ...formInput,
  81. style: { width: width.value },
  82. disabled: props.disabled,
  83. modelValue: proxyOptionsValue.value.length === 0 ? undefined : proxyOptionsValue.value,
  84. 'onUpdate:modelValue': (value) => {
  85. proxyOptionsValue.value = value
  86. },
  87. onChange: (val) => handleCheckItemChange(val)
  88. },
  89. () => checkboxItems())
  90. ]
  91. }
  92. }