cl-switch.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <template>
  2. <switch
  3. class="cl-switch"
  4. :checked="checked"
  5. :disabled="isDisabled"
  6. :color="color"
  7. @change="onChange"
  8. ></switch>
  9. </template>
  10. <script>
  11. import Form from "../../mixins/form";
  12. /**
  13. * switch 开关
  14. * @description 开关
  15. * @tutorial https://docs.cool-js.com/uni/components/form/switch.html
  16. * @property {String, Number} value 绑定值
  17. * @property {Boolean} disabled 是否禁用
  18. * @property {Boolean} activeValue 打开时的值,默认true
  19. * @property {Boolean} inactiveValue 关闭时的值,默认false
  20. * @property {Boolean} color 打开时的背景色,默认#409eff
  21. * @event {Function} change 绑定值改变时触发
  22. * @example <cl-switch />
  23. */
  24. export default {
  25. props: {
  26. // 绑定值
  27. value: [Boolean, String, Number],
  28. // 是否禁用
  29. disabled: Boolean,
  30. // 打开时的值
  31. activeValue: {
  32. type: [Boolean, String, Number],
  33. default: true,
  34. },
  35. // 关闭时的值
  36. inactiveValue: {
  37. type: [Boolean, String, Number],
  38. default: false,
  39. },
  40. // 打开时的背景色
  41. color: String,
  42. },
  43. mixins: [Form],
  44. computed: {
  45. checked() {
  46. return this.value === this.activeValue;
  47. },
  48. },
  49. methods: {
  50. onChange(e) {
  51. let d = e.detail.value ? this.activeValue : this.inactiveValue;
  52. this.$emit("input", d);
  53. this.$emit("change", d);
  54. },
  55. },
  56. };
  57. </script>