u-checkbox.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-checkbox",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props],
  6. data() {
  7. return {
  8. isChecked: false,
  9. // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式
  10. // 故只能使用如此方法
  11. parentData: {
  12. iconSize: 12,
  13. labelDisabled: null,
  14. disabled: null,
  15. shape: "square",
  16. activeColor: null,
  17. inactiveColor: null,
  18. size: 18,
  19. modelValue: null,
  20. iconColor: null,
  21. placement: "row",
  22. borderBottom: false,
  23. iconPlacement: "left"
  24. }
  25. };
  26. },
  27. computed: {
  28. // 是否禁用,如果父组件u-raios-group禁用的话,将会忽略子组件的配置
  29. elDisabled() {
  30. return this.disabled !== "" ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
  31. },
  32. // 是否禁用label点击
  33. elLabelDisabled() {
  34. return this.labelDisabled !== "" ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false;
  35. },
  36. // 组件尺寸,对应size的值,默认值为21px
  37. elSize() {
  38. return this.size ? this.size : this.parentData.size ? this.parentData.size : 21;
  39. },
  40. // 组件的勾选图标的尺寸,默认12px
  41. elIconSize() {
  42. return this.iconSize ? this.iconSize : this.parentData.iconSize ? this.parentData.iconSize : 12;
  43. },
  44. // 组件选中激活时的颜色
  45. elActiveColor() {
  46. return this.activeColor ? this.activeColor : this.parentData.activeColor ? this.parentData.activeColor : "#2979ff";
  47. },
  48. // 组件选未中激活时的颜色
  49. elInactiveColor() {
  50. return this.inactiveColor ? this.inactiveColor : this.parentData.inactiveColor ? this.parentData.inactiveColor : "#c8c9cc";
  51. },
  52. // label的颜色
  53. elLabelColor() {
  54. return this.labelColor ? this.labelColor : this.parentData.labelColor ? this.parentData.labelColor : "#606266";
  55. },
  56. // 组件的形状
  57. elShape() {
  58. return this.shape ? this.shape : this.parentData.shape ? this.parentData.shape : "circle";
  59. },
  60. // label大小
  61. elLabelSize() {
  62. return common_vendor.index.$u.addUnit(this.labelSize ? this.labelSize : this.parentData.labelSize ? this.parentData.labelSize : "15");
  63. },
  64. elIconColor() {
  65. const iconColor = this.iconColor ? this.iconColor : this.parentData.iconColor ? this.parentData.iconColor : "#ffffff";
  66. if (this.elDisabled) {
  67. return this.isChecked ? this.elInactiveColor : "transparent";
  68. } else {
  69. return this.isChecked ? iconColor : "transparent";
  70. }
  71. },
  72. iconClasses() {
  73. let classes = [];
  74. classes.push("u-checkbox__icon-wrap--" + this.elShape);
  75. if (this.elDisabled) {
  76. classes.push("u-checkbox__icon-wrap--disabled");
  77. }
  78. if (this.isChecked && this.elDisabled) {
  79. classes.push("u-checkbox__icon-wrap--disabled--checked");
  80. }
  81. return classes;
  82. },
  83. iconWrapStyle() {
  84. const style = {};
  85. style.backgroundColor = this.isChecked && !this.elDisabled ? this.elActiveColor : "#ffffff";
  86. style.borderColor = this.isChecked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor;
  87. style.width = common_vendor.index.$u.addUnit(this.elSize);
  88. style.height = common_vendor.index.$u.addUnit(this.elSize);
  89. if (this.parentData.iconPlacement === "right") {
  90. style.marginRight = 0;
  91. }
  92. return style;
  93. },
  94. checkboxStyle() {
  95. const style = {};
  96. if (this.parentData.borderBottom && this.parentData.placement === "row") {
  97. common_vendor.index.$u.error("检测到您将borderBottom设置为true,需要同时将u-checkbox-group的placement设置为column才有效");
  98. }
  99. if (this.parentData.borderBottom && this.parentData.placement === "column") {
  100. style.paddingBottom = "8px";
  101. }
  102. return common_vendor.index.$u.deepMerge(style, common_vendor.index.$u.addStyle(this.customStyle));
  103. }
  104. },
  105. mounted() {
  106. this.init();
  107. },
  108. methods: {
  109. init() {
  110. this.updateParentData();
  111. if (!this.parent) {
  112. common_vendor.index.$u.error("u-checkbox必须搭配u-checkbox-group组件使用");
  113. }
  114. const value = this.parentData.modelValue;
  115. if (this.checked) {
  116. this.isChecked = true;
  117. } else if (common_vendor.index.$u.test.array(value)) {
  118. this.isChecked = value.some((item) => {
  119. return item === this.name;
  120. });
  121. }
  122. },
  123. updateParentData() {
  124. this.getParentData("u-checkbox-group");
  125. },
  126. // 横向两端排列时,点击组件即可触发选中事件
  127. wrapperClickHandler(e) {
  128. this.parentData.iconPlacement === "right" && this.iconClickHandler(e);
  129. },
  130. // 点击图标
  131. iconClickHandler(e) {
  132. this.preventEvent(e);
  133. if (!this.elDisabled) {
  134. this.setRadioCheckedStatus();
  135. }
  136. },
  137. // 点击label
  138. labelClickHandler(e) {
  139. this.preventEvent(e);
  140. if (!this.elLabelDisabled && !this.elDisabled) {
  141. this.setRadioCheckedStatus();
  142. }
  143. },
  144. emitEvent() {
  145. this.$emit("change", this.isChecked);
  146. this.$nextTick(() => {
  147. common_vendor.index.$u.formValidate(this, "change");
  148. });
  149. },
  150. // 改变组件选中状态
  151. // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有u-checkbox实例
  152. // 将本组件外的其他u-checkbox的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
  153. setRadioCheckedStatus() {
  154. this.isChecked = !this.isChecked;
  155. this.emitEvent();
  156. typeof this.parent.unCheckedOther === "function" && this.parent.unCheckedOther(this);
  157. }
  158. },
  159. watch: {
  160. checked() {
  161. this.isChecked = this.checked;
  162. }
  163. }
  164. };
  165. if (!Array) {
  166. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  167. _easycom_u_icon2();
  168. }
  169. const _easycom_u_icon = () => "../u-icon/u-icon.js";
  170. if (!Math) {
  171. _easycom_u_icon();
  172. }
  173. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  174. return {
  175. a: common_vendor.p({
  176. name: "checkbox-mark",
  177. size: $options.elIconSize,
  178. color: $options.elIconColor
  179. }),
  180. b: common_vendor.o((...args) => $options.iconClickHandler && $options.iconClickHandler(...args)),
  181. c: common_vendor.n($options.iconClasses),
  182. d: common_vendor.s($options.iconWrapStyle),
  183. e: common_vendor.t(_ctx.label),
  184. f: common_vendor.o((...args) => $options.labelClickHandler && $options.labelClickHandler(...args)),
  185. g: $options.elDisabled ? $options.elInactiveColor : $options.elLabelColor,
  186. h: $options.elLabelSize,
  187. i: $options.elLabelSize,
  188. j: common_vendor.s($options.checkboxStyle),
  189. k: common_vendor.o((...args) => $options.wrapperClickHandler && $options.wrapperClickHandler(...args)),
  190. l: common_vendor.n(`u-checkbox-label--${$data.parentData.iconPlacement}`),
  191. m: common_vendor.n($data.parentData.borderBottom && $data.parentData.placement === "column" && "u-border-bottom")
  192. };
  193. }
  194. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-abd63d8e"], ["__file", "F:/兴元/开门柜项目/平台端管理系统小程序/node_modules/uview-plus/components/u-checkbox/u-checkbox.vue"]]);
  195. wx.createComponent(Component);