index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view :class="classes" @click="onClick" :style="style">
  3. <view class="switch-button">
  4. <view v-show="!isOpen" class="close-line"></view>
  5. <template v-if="activeText">
  6. <view class="nut-switch-label open" v-show="isOpen">{{
  7. activeText
  8. }}</view>
  9. <view class="nut-switch-label close" v-show="!isOpen">{{
  10. inactiveText
  11. }}</view>
  12. </template>
  13. </view>
  14. </view>
  15. </template>
  16. <script lang="ts">
  17. import { computed, ref } from 'vue';
  18. import { createComponent } from '@/utils/create';
  19. const { componentName, create } = createComponent('switch');
  20. export default create({
  21. props: {
  22. modelValue: {
  23. type: Boolean,
  24. default: false
  25. },
  26. checked: {
  27. type: Boolean,
  28. default: true
  29. },
  30. disable: {
  31. type: Boolean,
  32. default: false
  33. },
  34. activeColor: {
  35. type: String,
  36. default: ''
  37. },
  38. inactiveColor: {
  39. type: String,
  40. default: ''
  41. },
  42. activeText: {
  43. type: String,
  44. default: ''
  45. },
  46. inactiveText: {
  47. type: String,
  48. default: ''
  49. }
  50. },
  51. emits: ['change', 'update:modelValue'],
  52. setup(props, { emit }) {
  53. const classes = computed(() => {
  54. const prefixCls = componentName;
  55. return {
  56. [prefixCls]: true,
  57. [props.modelValue ? 'switch-open' : 'switch-close']: true,
  58. [`${prefixCls}-disable`]: props.disable,
  59. [`${prefixCls}-base`]: true
  60. };
  61. });
  62. const style = computed(() => {
  63. return {
  64. backgroundColor: props.modelValue
  65. ? props.activeColor
  66. : props.inactiveColor
  67. };
  68. });
  69. const onClick = (event: Event) => {
  70. if (props.disable) return;
  71. emit('update:modelValue', !props.modelValue);
  72. emit('change', !props.modelValue, event);
  73. };
  74. return {
  75. classes,
  76. style,
  77. onClick
  78. };
  79. }
  80. });
  81. </script>
  82. <style lang="scss">
  83. @import 'index.scss';
  84. </style>