index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { ElRadioGroup, ElRadio, ElRadioButton } from 'element-plus'
  2. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  3. import { formInputProps, fromInputEmits } from '../../form-input-props'
  4. import { computed, getCurrentInstance, h } from 'vue'
  5. import './index.less'
  6. export default {
  7. props: formInputProps,
  8. emits: [...fromInputEmits],
  9. setup (props, context) {
  10. const instance = getCurrentInstance()
  11. const { inputRef, width, updateStream } = useFormInput(props, context)
  12. const { optionProps, options, getOptions, proxyOptionsValue } = useOptions(props, false, updateStream)
  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. const getRadioComponent = (isButton) => {
  21. return isButton ? ElRadioButton : ElRadio
  22. }
  23. const radioItem = (option) => {
  24. return h(getRadioComponent(isButton.value), {
  25. label: option[optionProps.value.value] ?? option,
  26. style: { display: display.value },
  27. disabled: option.disabled
  28. }, {
  29. default: () => `${option[optionProps.value.label] ?? option}`
  30. })
  31. }
  32. const radioItems = () => options.value.map(radioItem)
  33. return () => {
  34. return h(ElRadioGroup, {
  35. ...context.attrs,
  36. ref: inputRef,
  37. class: ['cip-basic-radio'],
  38. style: { width: width.value },
  39. disabled: props.disabled,
  40. modelValue: proxyOptionsValue.value,
  41. 'onUpdate:modelValue': (value) => {
  42. proxyOptionsValue.value = value
  43. }
  44. },
  45. { default: () => radioItems() })
  46. }
  47. }
  48. }