switch.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <template>
  2. <div class="nut-switch" :class="[{ 'nut-switch-active': isActive }, 'nut-switch-' + size, { 'nut-switch-disabled': disabled }]" @click="toggle">
  3. <div class="nut-switch-btn"> </div>
  4. <div class="nut-switch-label" v-if="isActive">{{ arrLabel[0] }}</div>
  5. <div class="nut-switch-label" v-else>{{ arrLabel[1] }}</div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'nut-switch',
  11. props: {
  12. active: {
  13. type: Boolean,
  14. default: false
  15. },
  16. size: {
  17. type: String,
  18. default: 'base'
  19. },
  20. disabled: {
  21. type: Boolean,
  22. default: false
  23. },
  24. label: {
  25. type: String,
  26. default: ''
  27. }
  28. },
  29. data() {
  30. return {
  31. isActive: false,
  32. arrLabel: (this.label || '').split('|')
  33. };
  34. },
  35. created() {
  36. this.isActive = this.active;
  37. },
  38. watch: {
  39. active(newVal) {
  40. this.isActive = newVal;
  41. }
  42. },
  43. methods: {
  44. toggle() {
  45. const status = this.isActive;
  46. if (!this.disabled) {
  47. this.isActive = !status;
  48. setTimeout(() => {
  49. this.$emit('change', this.isActive);
  50. this.$emit('update:active', this.isActive);
  51. }, 300);
  52. }
  53. }
  54. }
  55. };
  56. </script>