index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="m-switch-default" :class="sizeClass">
  3. <span
  4. class="m-switch-label m-switch-label-left"
  5. :class="[!curChecked ? 'is-active' : '']"
  6. v-if="inactiveText && !inlinePrompt">
  7. {{inactiveText}}
  8. </span>
  9. <div
  10. class="m-switch-content"
  11. :class="styleClass"
  12. @click="handleSwitch"
  13. >
  14. <input type="checkbox" v-model="curChecked">
  15. <span class="core" :class="[curChecked ? 'on' : 'off']" :style="coreStyle">
  16. <span class="m-switch-inner" v-if="inlinePrompt">
  17. {{curChecked ? activeText : inactiveText}}
  18. </span>
  19. </span>
  20. </div>
  21. <span
  22. class="m-switch-label m-switch-label-right"
  23. :class="[curChecked ? 'is-active' : '']"
  24. v-if="activeText && !inlinePrompt">
  25. {{activeText}}
  26. </span>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. name: "mSwitch"
  32. };
  33. </script>
  34. <script setup>
  35. import { computed, ref } from 'vue-demi'
  36. const emit = defineEmits(['update:modelValue','change'])
  37. const props = defineProps({
  38. modelValue:Boolean,
  39. disabled: {
  40. type: Boolean,
  41. default: false
  42. },
  43. activeText: {
  44. type: String,
  45. default: ''
  46. },
  47. inactiveText: {
  48. type: String,
  49. default: ''
  50. },
  51. activeColor: {
  52. type: String,
  53. default: ''
  54. },
  55. inactiveColor: {
  56. type: String,
  57. default: ''
  58. },
  59. inlinePrompt: {
  60. type: Boolean,
  61. default: false
  62. },
  63. size: String
  64. })
  65. const curChecked = ref(props.modelValue)
  66. const styleClass = computed(() => {
  67. return {
  68. 'is-disabled': props.disabled
  69. }
  70. })
  71. const sizeClass = computed(() => {
  72. return {
  73. [`m-switch--${props.size}`]: props.size,
  74. }
  75. })
  76. const coreStyle = computed(() => {
  77. let newColor = curChecked.value ? props.activeColor : props.inactiveColor
  78. return {
  79. backgroundColor: newColor
  80. }
  81. })
  82. const handleSwitch =() => {
  83. if(props.disabled) return
  84. curChecked.value = !curChecked.value
  85. emit('update:modelValue',curChecked.value)
  86. emit('change',curChecked.value)
  87. }
  88. </script>
  89. <style lang="scss" scoped>
  90. @import '../../styles/components/switch.scss';
  91. </style>