index.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { ElCheckboxGroup, ElCheckbox, ElRadioGroup, ElRadio, ElInput, ElButton, ElCol } from 'element-plus'
  2. import { useFormInput } from '@cip/components/hooks/form-input'
  3. import { formInputProps, fromInputEmits } from '../../form-input-props'
  4. import { computed, h } from 'vue'
  5. import { isArray } from '@cip/utils/util'
  6. import './index.less'
  7. export default {
  8. inheritAttrs: false,
  9. props: formInputProps,
  10. emits: [...fromInputEmits],
  11. setup (props, context) {
  12. const { width, emitModelValue, ...formInput } = useFormInput(props, context)
  13. const isMultiple = computed(() => {
  14. if (['radio', 'checkbox'].includes(props.model?.type)) {
  15. return props.model.type === 'checkbox'
  16. } else {
  17. return props.model?.multiple ?? false
  18. }
  19. })
  20. const modelOptions = computed(() => {
  21. return props.otherValue ?? []
  22. })
  23. const limit = computed(() => {
  24. return props.config?.limit ?? ''
  25. })
  26. const transValue = computed({
  27. get () {
  28. const value = props.modelValue
  29. if (isMultiple.value) {
  30. if (!isArray(value)) {
  31. return value ? value.split(props.config.splitKey || ',') : []
  32. } else {
  33. return value ?? []
  34. }
  35. } else {
  36. return value ?? ''
  37. }
  38. }
  39. })
  40. const emitInput = val => {
  41. let modelValue = val
  42. if (isMultiple.value) {
  43. modelValue = val?.join(props.config.splitKey || ',') ?? val
  44. }
  45. emitModelValue(modelValue)
  46. }
  47. const getModelValue = (val) => {
  48. if (isMultiple.value) {
  49. return val.filter(v => modelOptions.value.includes(v))
  50. } else {
  51. return modelOptions.value.includes(val) ? val : ''
  52. }
  53. }
  54. // 输入框
  55. const inputItem = (value, index) => h(ElInput, {
  56. modelValue: value.label ?? value,
  57. maxlength: limit.value,
  58. 'show-word-limit': true,
  59. 'onUpdate:modelValue': val => {
  60. value.label ? value.label = val : value = val
  61. modelOptions.value[index] = value
  62. emitInput(getModelValue(transValue.value))
  63. context.emit('update:otherValue', modelOptions.value)
  64. }
  65. })
  66. // 删除按钮
  67. const deleteItem = (val, idx) => h(ElButton, {
  68. icon: 'el-icon-minus',
  69. type: 'danger',
  70. plain: true,
  71. round: true,
  72. size: 'mini',
  73. onClick: e => {
  74. const deleteItem = modelOptions.value.splice(idx, 1)
  75. // 删除已选值时,清空默认值
  76. if (props.modelValue === deleteItem[0]) {
  77. emitInput()
  78. }
  79. context.emit('update:otherValue', modelOptions.value)
  80. }
  81. })
  82. const checkboxItem = (val, idx) => {
  83. const ElComponent = isMultiple.value ? ElCheckbox : ElRadio
  84. return h(ElComponent, {
  85. label: val.label ?? val,
  86. style: { display: 'block' },
  87. 'onUpdate:modelValue': emitInput
  88. }, {
  89. default: () => [inputItem(val, idx), deleteItem(val, idx)]
  90. })
  91. }
  92. // 新增
  93. const addItem = () => h(ElButton, {
  94. type: 'text',
  95. onClick: e => {
  96. modelOptions.value.push('新选项')
  97. context.emit('update:otherValue', modelOptions.value)
  98. }
  99. }, { default: () => '新增' })
  100. const checkboxItems = () => modelOptions.value.map(checkboxItem)
  101. const checkboxDefaultSlots = () => {
  102. return checkboxItems()
  103. }
  104. const checkboxGroup = () => {
  105. const ElComponentGroup = isMultiple.value ? ElCheckboxGroup : ElRadioGroup
  106. return h(ElComponentGroup, {
  107. ...context.attrs,
  108. ...formInput,
  109. modelValue: transValue.value,
  110. class: 'option-static-data',
  111. 'onUpdate:modelValue': emitInput
  112. },
  113. { default: checkboxDefaultSlots })
  114. }
  115. return () => h(ElCol, {}, { default: () => [checkboxGroup(), addItem()] })
  116. }
  117. }