mobile.jsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { ref, computed, onBeforeUpdate } from 'vue'
  2. import { CheckboxGroup as VanCheckboxGroup, Checkbox as VanCheckbox, cell as VanCell } from 'vant'
  3. import { useFormInput, useOptions } from '../../../hooks/form-input'
  4. import { formInputProps } from '../../form-input-props'
  5. export default {
  6. props: formInputProps,
  7. setup (props, context) {
  8. const { emitModelValue, width, ...formInput } = useFormInput(props, context)
  9. const { optionProps, options, getValue, getModelValue, getOtherValue } = useOptions(props, true, formInput.emitInput)
  10. // 另一个键
  11. const otherKey = computed(() => {
  12. return props.config?.otherKey ?? ''
  13. })
  14. const proxyValue = computed(() => {
  15. return getValue(props.modelValue)
  16. })
  17. const emitInput = (value) => {
  18. const modelValue = getModelValue(value)
  19. context.emit('update:modelValue', modelValue)
  20. if (otherKey.value) {
  21. const otherValue = getOtherValue(modelValue, value)
  22. context.emit('update:otherValue', otherValue)
  23. }
  24. }
  25. const checkboxRefs = ref([])
  26. const checkRefFn = (el, index) => {
  27. checkboxRefs.value[index] = el
  28. }
  29. const cellToggle = (index) => {
  30. checkboxRefs.value[index].toggle()
  31. }
  32. onBeforeUpdate(() => {
  33. checkboxRefs.value = []
  34. })
  35. const checkboxItem = (option, index) => <VanCheckbox shape="square"
  36. name={option[optionProps.value.value] ?? option}
  37. ref={el => checkRefFn(el, index)}
  38. onclick={e => e.stopPropagation()} />
  39. // 优化操作-cell增加点击事件
  40. const cellItem = (option, index) => <VanCell v-slots={{ 'right-icon': () => checkboxItem(option, index) }}
  41. title={option[optionProps.value.label] ?? option}
  42. onclick={() => cellToggle(index)}
  43. clickable />
  44. const checkboxItems = () => options.value.map(cellItem)
  45. return () => <VanCheckboxGroup modelValue={proxyValue.value}
  46. onUpdate:modelValue={emitInput}
  47. style={{ width: width.value }}>
  48. {checkboxItems()}
  49. </VanCheckboxGroup>
  50. }
  51. }