mobile.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { computed } from 'vue'
  2. import { RadioGroup as VanRadioGroup, Radio as VanRadio, 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, false, 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 radioItem = (option) => <VanRadio name={option[optionProps.value.value] ?? option} />
  26. // 优化操作-cell增加点击事件
  27. const cellItem = (option) => <VanCell v-slots={{ 'right-icon': () => radioItem(option) }}
  28. title={option[optionProps.value.label] ?? option}
  29. onclick={() => emitInput(option[optionProps.value.value] ?? option)}
  30. clickable />
  31. const radioItems = () => options.value.map(cellItem)
  32. return () => <VanRadioGroup modelValue={proxyValue.value}
  33. onUpdate:modelValue={emitInput}
  34. style={{ width: width.value }}>
  35. {radioItems()}
  36. </VanRadioGroup>
  37. }
  38. }