index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view :class="classes">
  3. <input
  4. type="radio"
  5. :value="currentValue"
  6. :class="{ 'nut-radio-ani': isAnimated }"
  7. :checked="currentValue === label"
  8. :disabled="isDisabled"
  9. :label="label"
  10. @click="clickEvt"
  11. />
  12. <view class="nut-radio-label">
  13. <slot></slot>
  14. </view>
  15. </view>
  16. </template>
  17. <script lang="ts">
  18. import { computed, getCurrentInstance, inject } from 'vue';
  19. import { createComponent } from '@/utils/create';
  20. const { componentName, create } = createComponent('radio');
  21. type Iparent = {
  22. parentNode: boolean;
  23. };
  24. export default create({
  25. props: {
  26. value: {
  27. type: [String, Number, Boolean],
  28. default: false
  29. },
  30. label: [String, Number, Boolean],
  31. size: {
  32. type: String,
  33. default: 'base'
  34. },
  35. disabled: {
  36. type: Boolean,
  37. default: false
  38. },
  39. animated: {
  40. type: Boolean,
  41. default: true
  42. }
  43. },
  44. emits: ['update:value', 'change'],
  45. setup(props, { emit }) {
  46. const parentGroup = inject('radiogroup', {
  47. parentNode: false,
  48. changeVal: (val: string) => {}
  49. });
  50. const internalInstance = getCurrentInstance()?.parent;
  51. const parentProps = internalInstance?.props;
  52. const isParentGroup = computed(() => parentGroup && parentGroup.parentNode);
  53. const currentSize = computed(() => {
  54. return isParentGroup.value ? parentProps?.size : props.size;
  55. });
  56. const classes = computed(() => {
  57. const prefixCls = componentName;
  58. return {
  59. [prefixCls]: true,
  60. [`${prefixCls}-size-${currentSize.value}`]: true
  61. };
  62. });
  63. const currentValue = computed({
  64. get: () => {
  65. return isParentGroup.value ? parentProps?.value : props.value;
  66. },
  67. set: (val: any) => {
  68. isParentGroup.value ? parentGroup.changeVal(val) : emit('change', val);
  69. }
  70. });
  71. const isDisabled = computed(() => {
  72. return isParentGroup.value ? parentProps?.disabled : props.disabled;
  73. });
  74. const isAnimated = computed(() => {
  75. return isParentGroup ? parentProps?.animated : props.animated;
  76. });
  77. const clickEvt = (event: Event) => {
  78. event.stopPropagation();
  79. if (isDisabled.value) {
  80. return false;
  81. }
  82. currentValue.value = props.label ?? '';
  83. emit('update:value', currentValue.value);
  84. emit('change', currentValue.value);
  85. };
  86. return {
  87. classes,
  88. currentValue,
  89. isDisabled,
  90. isAnimated,
  91. clickEvt
  92. };
  93. }
  94. });
  95. </script>
  96. <style lang="scss">
  97. @import 'index.scss';
  98. </style>