index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { ElRadioGroup, ElRadio, ElRadioButton } 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. import './index.less'
  6. export default {
  7. props: formInputProps,
  8. emits: ['update:modelValue', 'update:otherValue'],
  9. setup (props, context) {
  10. const instance = getCurrentInstance()
  11. const { width, ...formInput } = useFormInput(props, context)
  12. const { optionProps, options, getOptions, getValue, getModelValue, getOtherValue } = useOptions(props, false, formInput.emitInput)
  13. instance.ctx.getOptions = getOptions
  14. const display = computed(() => {
  15. return props.config?.display ?? 'inline-block'
  16. })
  17. const isButton = computed(() => {
  18. return props.config?.isButton ?? false
  19. })
  20. // 另一个键
  21. const otherKey = computed(() => {
  22. return props.config?.otherKey ?? ''
  23. })
  24. const proxyValue = computed({
  25. get () {
  26. return getValue(props.modelValue)
  27. },
  28. set (value) {
  29. const modelValue = getModelValue(value)
  30. context.emit('update:modelValue', modelValue)
  31. if (otherKey.value) {
  32. const otherValue = getOtherValue(modelValue, value)
  33. context.emit('update:otherValue', otherValue)
  34. }
  35. }
  36. })
  37. const getRadioComponent = (isButton) => {
  38. return isButton ? ElRadioButton : ElRadio
  39. }
  40. const radioItem = (option) => {
  41. return h(getRadioComponent(isButton.value), {
  42. label: option[optionProps.value.value] ?? option,
  43. style: { display: display.value },
  44. disabled: option.disabled
  45. }, {
  46. default: () => `${option[optionProps.value.label] ?? option}`
  47. })
  48. }
  49. const radioItems = () => options.value.map(radioItem)
  50. return () => {
  51. return h(ElRadioGroup, {
  52. ...context.attrs,
  53. ...formInput,
  54. class: ['cip-basic-radio'],
  55. style: { width: width.value },
  56. disabled: props.disabled,
  57. modelValue: proxyValue.value,
  58. 'onUpdate:modelValue': (value) => {
  59. proxyValue.value = value
  60. }
  61. },
  62. { default: () => radioItems() })
  63. }
  64. }
  65. }