cl-checkbox.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="cl-checkbox" :class="[classList]" @tap="change">
  3. <view class="cl-checkbox__input">
  4. <text class="cl-icon-toast-success" v-if="checked"></text>
  5. </view>
  6. <view class="cl-checkbox__label">
  7. <slot></slot>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import Emitter from "../../mixins/emitter";
  13. import Form from "../../mixins/form";
  14. import Parent from "../../mixins/parent";
  15. import { isBoolean } from "../../utils";
  16. /**
  17. * checkbox 多选框
  18. * @description 多选框
  19. * @tutorial https://docs.cool-js.com/uni/components/form/checkbox.html
  20. * @property {String, Number} value 绑定值
  21. * @property {String, Number} label 标识
  22. * @property {Boolean} disabled 是否禁用
  23. * @property {Boolean} border 是否边框样式
  24. * @property {Boolean} round 是否圆角
  25. * @property {Boolean} fill 是否宽度填充
  26. * @event {Function} change 绑定值改变时触发
  27. * @example <cl-checkbox label="1"></cl-checkbox>
  28. */
  29. export default {
  30. name: "cl-checkbox",
  31. componentName: "ClCheckbox",
  32. props: {
  33. // 绑定值
  34. value: null,
  35. // 标识
  36. label: [String, Number],
  37. // 是否禁用
  38. disabled: Boolean,
  39. // 是否边框样式
  40. border: Boolean,
  41. // 是否圆角
  42. round: Boolean,
  43. // 是否宽度填充
  44. fill: Boolean,
  45. },
  46. mixins: [Emitter, Form, Parent],
  47. data() {
  48. return {
  49. checked: false,
  50. Keys: ["border", "disabled", "value"],
  51. ComponentName: "ClCheckboxGroup",
  52. };
  53. },
  54. watch: {
  55. value: {
  56. immediate: true,
  57. handler(val) {
  58. this.checked = Boolean(val);
  59. },
  60. },
  61. "parent.value": {
  62. immediate: true,
  63. handler(val) {
  64. if (this.hasParent) {
  65. this.checked = val.includes(this.label);
  66. }
  67. },
  68. },
  69. },
  70. computed: {
  71. isRound() {
  72. return this.hasParent ? this.parent.round || this.round : this.round;
  73. },
  74. isBorder() {
  75. return this.hasParent ? this.parent.border || this.border : this.border;
  76. },
  77. isDisabled() {
  78. let disabled = this.hasParent ? this.parent.disabled || this.disabled : this.disabled;
  79. return this.$form ? this.$form.disabled || disabled : disabled;
  80. },
  81. classList() {
  82. let list = [];
  83. if (this.isBorder) {
  84. list.push("cl-checkbox--border");
  85. }
  86. if (this.isRound) {
  87. list.push("cl-checkbox--round");
  88. }
  89. if (this.isDisabled) {
  90. list.push("is-disabled");
  91. }
  92. if (this.checked) {
  93. list.push("is-checked");
  94. }
  95. if (this.fill) {
  96. list.push("is-fill");
  97. }
  98. return list.join(" ");
  99. },
  100. },
  101. methods: {
  102. change() {
  103. if (this.isDisabled) {
  104. return false;
  105. }
  106. this.checked = !this.checked;
  107. // 回调到组件或者多选框组
  108. if (this.hasParent) {
  109. this.dispatch("ClCheckboxGroup", "checkbox.change", this.label);
  110. } else {
  111. this.$emit("input", this.checked);
  112. this.$emit("change", this.checked);
  113. }
  114. },
  115. },
  116. };
  117. </script>