index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view :class="classes" @click="onClick" :style="style">
  3. <view class="switch-button">
  4. <nut-icon v-if="loading" :name="name" :size="size" :color="color"></nut-icon>
  5. <!-- <view v-show="!modelValue" class="close-line"></view> -->
  6. <template v-if="activeText">
  7. <view class="nut-switch-label open" v-show="modelValue">{{ activeText }}</view>
  8. <view class="nut-switch-label close" v-show="!modelValue">{{ inactiveText }}</view>
  9. </template>
  10. </view>
  11. </view>
  12. </template>
  13. <script lang="ts">
  14. import { computed, watch } from 'vue';
  15. import { createComponent } from '@/packages/utils/create';
  16. const { componentName, create } = createComponent('switch');
  17. export default create({
  18. props: {
  19. modelValue: {
  20. type: [String, Number, Boolean],
  21. default: false
  22. },
  23. disable: {
  24. type: Boolean,
  25. default: false
  26. },
  27. activeColor: {
  28. type: String,
  29. default: ''
  30. },
  31. inactiveColor: {
  32. type: String,
  33. default: ''
  34. },
  35. activeText: {
  36. type: String,
  37. default: ''
  38. },
  39. inactiveText: {
  40. type: String,
  41. default: ''
  42. },
  43. activeValue: {
  44. type: [String, Number, Boolean],
  45. default: true
  46. },
  47. inactiveValue: {
  48. type: [String, Number, Boolean],
  49. default: false
  50. },
  51. loading: {
  52. type: Boolean,
  53. default: false
  54. },
  55. name: {
  56. type: String,
  57. default: 'loading1'
  58. },
  59. size: {
  60. type: [String, Number],
  61. default: '12px'
  62. },
  63. color: {
  64. type: String,
  65. default: ''
  66. }
  67. },
  68. emits: ['change', 'update:modelValue', 'update:loading'],
  69. setup(props, { emit }) {
  70. const isActive = computed(() => props.modelValue === props.activeValue);
  71. const classes = computed(() => {
  72. const prefixCls = componentName;
  73. return {
  74. [prefixCls]: true,
  75. [isActive.value ? 'switch-open' : 'switch-close']: true,
  76. [`${prefixCls}-disable`]: props.disable,
  77. [`${prefixCls}-base`]: true
  78. };
  79. });
  80. const style = computed(() => {
  81. return {
  82. backgroundColor: isActive.value ? props.activeColor : props.inactiveColor
  83. };
  84. });
  85. let updateType = '';
  86. const onClick = (event: Event) => {
  87. if (props.disable || props.loading) return;
  88. const value = isActive.value ? props.inactiveValue : props.activeValue;
  89. updateType = 'click';
  90. emit('update:modelValue', value);
  91. emit('change', value, event);
  92. };
  93. watch(
  94. () => props.modelValue,
  95. (v) => {
  96. if (updateType == 'click') {
  97. updateType = '';
  98. } else {
  99. emit('change', v);
  100. }
  101. }
  102. );
  103. return {
  104. classes,
  105. style,
  106. onClick
  107. };
  108. }
  109. });
  110. </script>