Switch.vue 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template lang="html">
  2. <div :class="switchClass" @click="toggle">
  3. <span :class="`${prefixCls}__inner`">
  4. <span v-if="value">True</span>
  5. <span v-if="!value">False</span>
  6. </span>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'zk-switch',
  12. props: {
  13. value: {
  14. type: Boolean,
  15. default: false,
  16. },
  17. disabled: {
  18. type: Boolean,
  19. default: false,
  20. },
  21. },
  22. data() {
  23. return {
  24. prefixCls: 'zk-switch',
  25. };
  26. },
  27. computed: {
  28. switchClass() {
  29. return [
  30. `${this.prefixCls}`,
  31. {
  32. [`${this.prefixCls}--checked`]: this.value,
  33. [`${this.prefixCls}--disabled`]: this.disabled,
  34. },
  35. ];
  36. },
  37. },
  38. methods: {
  39. toggle() {
  40. if (this.disabled) {
  41. return false;
  42. }
  43. const value = !this.value;
  44. this.$emit('input', value);
  45. return this.$emit('on-change', value);
  46. },
  47. },
  48. };
  49. </script>
  50. <style lang="less" src="./Switch.less"></style>