index.jsx 3.8 KB

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