mobile.jsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { ref, 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, fromInputEmits } from '../../form-input-props'
  5. export default {
  6. props: formInputProps,
  7. emits: [...fromInputEmits],
  8. setup (props, context) {
  9. const { updateStream, width } = useFormInput(props, context)
  10. const { optionProps, options, proxyOptionsValue } = useOptions(props, true, updateStream)
  11. const checkboxRefs = ref([])
  12. const checkRefFn = (el, index) => {
  13. checkboxRefs.value[index] = el
  14. }
  15. const cellToggle = (index) => {
  16. checkboxRefs.value[index].toggle()
  17. }
  18. onBeforeUpdate(() => {
  19. checkboxRefs.value = []
  20. })
  21. const checkboxItem = (option, index) => <VanCheckbox shape="square"
  22. name={option[optionProps.value.value] ?? option}
  23. ref={el => checkRefFn(el, index)}
  24. onclick={e => e.stopPropagation()} />
  25. // 优化操作-cell增加点击事件
  26. const cellItem = (option, index) => <VanCell v-slots={{ 'right-icon': () => checkboxItem(option, index) }}
  27. title={option[optionProps.value.label] ?? option}
  28. onclick={() => cellToggle(index)}
  29. clickable />
  30. const checkboxItems = () => options.value.map(cellItem)
  31. return () => <VanCheckboxGroup v-model={proxyOptionsValue.value}
  32. style={{ width: width.value }}>
  33. {checkboxItems()}
  34. </VanCheckboxGroup>
  35. }
  36. }