index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { ElCheckbox, ElCheckboxButton, ElCheckboxGroup } from 'element-plus'
  2. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  3. import { formInputProps } from '../../form-input-props'
  4. import { computed, getCurrentInstance, h } from 'vue'
  5. export default {
  6. props: formInputProps,
  7. emits: ['update:modelValue'],
  8. setup (props, context) {
  9. const instance = getCurrentInstance()
  10. const { width, ...formInput } = useFormInput(props, context)
  11. const { optionProps, options, getOptions, getValue, getModelValue, getOtherValue } = useOptions(props, true, formInput.emitInput)
  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-block'
  18. })
  19. // 另一个键
  20. const otherKey = computed(() => {
  21. return props.config?.otherKey ?? ''
  22. })
  23. const proxyValue = computed({
  24. get () {
  25. return getValue(props.modelValue)
  26. },
  27. set (value) {
  28. const modelValue = getModelValue(value)
  29. context.emit('update:modelValue', modelValue)
  30. if (otherKey.value) {
  31. const otherValue = getOtherValue(modelValue, value)
  32. context.emit('update:otherValue', otherValue)
  33. }
  34. }
  35. })
  36. const getCheckboxComponent = (isButton) => {
  37. return isButton ? ElCheckboxButton : ElCheckbox
  38. }
  39. const checkboxItem = (option) => {
  40. return h(getCheckboxComponent(isButton.value), {
  41. label: option[optionProps.value.value] ?? option,
  42. style: { display: display.value }
  43. }, {
  44. default: () => `${option[optionProps.value.label] ?? option}`
  45. })
  46. }
  47. const checkboxItems = () => options.value.map(checkboxItem)
  48. return () => h(ElCheckboxGroup, {
  49. ...context.attrs,
  50. ...formInput,
  51. style: { width: width.value },
  52. disabled: props.disabled,
  53. modelValue: proxyValue.value,
  54. 'onUpdate:modelValue': (value) => {
  55. proxyValue.value = value
  56. }
  57. },
  58. { default: () => checkboxItems() })
  59. }
  60. }