合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

  1. <template>
  2. <view class="l-radio-group" :class="'l-radio-group--'+ direction">
  3. <slot />
  4. </view>
  5. </template>
  6. <script lang="uts" setup>
  7. import { RadioGroupProps } from './type';
  8. const emit = defineEmits(['change', 'update:modelValue'])
  9. const props = withDefaults(defineProps<RadioGroupProps>(), {
  10. allowUncheck: false,
  11. disabled: false,
  12. direction: 'horizontal'
  13. // size: 'medium'
  14. })
  15. const innerValue = ref<any|null>(null);
  16. const groupValue = computed({
  17. set(value: any|null) {
  18. innerValue.value = value
  19. emit('update:modelValue', value)
  20. emit('change', value)
  21. },
  22. get():any|null {
  23. return props.value ?? props.modelValue ?? props.defaultValue ?? innerValue.value
  24. }
  25. } as WritableComputedOptions<any|null>)
  26. const handleRadioChange = (val: any) => {
  27. if (props.allowUncheck && val == groupValue.value) {
  28. groupValue.value = ''
  29. } else {
  30. groupValue.value = val
  31. }
  32. }
  33. provide('limeRadioGroupProps', props)
  34. provide('limeRadioGroupValue', groupValue)
  35. provide('limeRadioGroupChange', handleRadioChange)
  36. </script>
  37. <style lang="scss">
  38. .l-radio-group {
  39. flex-direction: row;
  40. }
  41. .l-radio-group--vertical {
  42. flex-direction: column;
  43. }
  44. </style>