u-checkbox.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <view class="u-checkbox" :style="[checkboxStyle]">
  3. <view class="u-checkbox__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
  4. <u-icon v-if="indeterminate" class="u-checkbox__icon-wrap__icon" name="minus" :size="checkboxIconSize"
  5. :color="iconColor" />
  6. <u-icon v-else class="u-checkbox__icon-wrap__icon" name="checkbox-mark" :size="checkboxIconSize"
  7. :color="iconColor" />
  8. </view>
  9. <view class="u-checkbox__label" @tap="onClickLabel" :style="{
  10. fontSize: $u.addUnit(labelSize)
  11. }">
  12. <slot />
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * checkbox 复选框
  19. * @description 该组件需要搭配checkboxGroup组件使用,以便用户进行操作时,获得当前复选框组的选中情况。
  20. * @tutorial https://www.uviewui.com/components/checkbox.html
  21. * @property {String Number} icon-size 图标大小,单位rpx(默认20)
  22. * @property {String Number} label-size label字体大小,单位rpx(默认28)
  23. * @property {String Number} name checkbox组件的标示符
  24. * @property {String} shape 形状,外观形状,shape-方形,circle-圆形(默认circle)
  25. * @property {Boolean} disabled 是否禁用
  26. * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox
  27. * @property {String} active-color 选中时的颜色,如设置CheckboxGroup的active-color将失效
  28. * @event {Function} change 某个checkbox状态发生变化时触发,回调为一个对象
  29. * @example <u-checkbox v-model="checked" :disabled="false">天涯</u-checkbox>
  30. */
  31. export default {
  32. name: "u-checkbox",
  33. emits: ["update:modelValue", "input", "change"],
  34. props: {
  35. // 是否为选中状态
  36. value: {
  37. type: Boolean,
  38. default: false
  39. },
  40. modelValue: {
  41. type: Boolean,
  42. default: false
  43. },
  44. // checkbox的名称
  45. name: {
  46. type: [String, Number],
  47. default: ""
  48. },
  49. // 形状,square为方形,circle为圆型
  50. shape: {
  51. type: String,
  52. default: ""
  53. },
  54. // 是否禁用
  55. disabled: {
  56. type: [String, Boolean],
  57. default: ""
  58. },
  59. // 是否禁止点击提示语选中复选框
  60. labelDisabled: {
  61. type: [String, Boolean],
  62. default: ""
  63. },
  64. // 选中状态下的颜色,如设置此值,将会覆盖checkboxGroup的activeColor值
  65. activeColor: {
  66. type: String,
  67. default: ""
  68. },
  69. // 图标的大小,单位rpx
  70. iconSize: {
  71. type: [String, Number],
  72. default: ""
  73. },
  74. // label的字体大小,rpx单位
  75. labelSize: {
  76. type: [String, Number],
  77. default: ""
  78. },
  79. // 组件的整体大小
  80. size: {
  81. type: [String, Number],
  82. default: ""
  83. },
  84. // 设置不确定状态,仅负责样式控制
  85. indeterminate: {
  86. type: Boolean,
  87. default: false
  88. }
  89. },
  90. data() {
  91. return {
  92. parentDisabled: false,
  93. newParams: {}
  94. };
  95. },
  96. created() {
  97. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环应用
  98. this.parent = this.$u.$parent.call(this, "u-checkbox-group");
  99. // 如果存在u-checkbox-group,将本组件的this塞进父组件的children中
  100. this.parent && this.parent.children.push(this);
  101. },
  102. computed: {
  103. valueCom() {
  104. // #ifndef VUE3
  105. return this.value;
  106. // #endif
  107. // #ifdef VUE3
  108. return this.modelValue;
  109. // #endif
  110. },
  111. // 是否禁用,如果父组件u-checkbox-group禁用的话,将会忽略子组件的配置
  112. isDisabled() {
  113. return this.disabled !== "" ? this.disabled : this.parent ? this.parent.disabled : false;
  114. },
  115. // 是否禁用label点击
  116. isLabelDisabled() {
  117. return this.labelDisabled !== "" ? this.labelDisabled : this.parent ? this.parent.labelDisabled : false;
  118. },
  119. // 组件尺寸,对应size的值,默认值为34rpx
  120. checkboxSize() {
  121. return this.size ? this.size : this.parent ? this.parent.size : 34;
  122. },
  123. // 组件的勾选图标的尺寸,默认20
  124. checkboxIconSize() {
  125. return this.iconSize ? this.iconSize : this.parent ? this.parent.iconSize : 20;
  126. },
  127. // 组件选中激活时的颜色
  128. elActiveColor() {
  129. return this.activeColor ? this.activeColor : this.parent ? this.parent.activeColor : "primary";
  130. },
  131. // 组件的形状
  132. elShape() {
  133. return this.shape ? this.shape : this.parent ? this.parent.shape : "square";
  134. },
  135. iconStyle() {
  136. let style = {};
  137. // 既要判断是否手动禁用,还要判断用户v-model绑定的值,如果绑定为false,那么也无法选中
  138. if (this.elActiveColor && this.valueCom && !this.isDisabled) {
  139. style.borderColor = this.elActiveColor;
  140. style.backgroundColor = this.elActiveColor;
  141. }
  142. style.width = this.$u.addUnit(this.checkboxSize);
  143. style.height = this.$u.addUnit(this.checkboxSize);
  144. return style;
  145. },
  146. // checkbox内部的勾选图标,如果选中状态,为白色,否则为透明色即可
  147. iconColor() {
  148. if (this.indeterminate) return '#ffffff';
  149. return this.valueCom ? "#ffffff" : "transparent";
  150. },
  151. iconClass() {
  152. let classes = [];
  153. classes.push("u-checkbox__icon-wrap--" + this.elShape);
  154. if (this.valueCom == true) classes.push("u-checkbox__icon-wrap--checked");
  155. if (this.isDisabled) classes.push("u-checkbox__icon-wrap--disabled");
  156. if (this.valueCom && this.isDisabled) classes.push("u-checkbox__icon-wrap--disabled--checked");
  157. if (this.indeterminate === true) classes.push('u-checkbox__icon-wrap--indeterminate')
  158. // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  159. return classes.join(" ");
  160. },
  161. checkboxStyle() {
  162. let style = {};
  163. if (this.parent && this.parent.width) {
  164. style.width = this.parent.width;
  165. // #ifdef MP
  166. // 各家小程序因为它们特殊的编译结构,使用float布局
  167. style.float = "left";
  168. // #endif
  169. // #ifndef MP
  170. // H5和APP使用flex布局
  171. style.flex = `0 0 ${this.parent.width}`;
  172. // #endif
  173. }
  174. if (this.parent && this.parent.wrap) {
  175. style.width = "100%";
  176. // #ifndef MP
  177. // H5和APP使用flex布局,将宽度设置100%,即可自动换行
  178. style.flex = "0 0 100%";
  179. // #endif
  180. }
  181. return style;
  182. }
  183. },
  184. mounted() {
  185. this._emitEvent();
  186. },
  187. watch: {
  188. valueCom: {
  189. handler: function(newVal, oldVal) {
  190. this._emitEvent();
  191. }
  192. }
  193. },
  194. methods: {
  195. _emitEvent() {
  196. let value = this.valueCom;
  197. let obj = {
  198. value,
  199. name: this.name
  200. };
  201. // 执行父组件u-checkbox-group的事件方法
  202. if (this.parent && this.parent.emitEvent) this.parent.emitEvent(obj);
  203. },
  204. onClickLabel() {
  205. if (!this.isLabelDisabled && !this.isDisabled) {
  206. this.setValue();
  207. }
  208. },
  209. toggle() {
  210. if (!this.isDisabled) {
  211. this.setValue();
  212. }
  213. },
  214. emitEvent() {
  215. let obj = {
  216. value: !this.valueCom,
  217. name: this.name
  218. };
  219. this.$emit("change", obj);
  220. // 执行父组件u-checkbox-group的事件方法
  221. if (this.parent && this.parent.emitEvent) this.parent.emitEvent(obj);
  222. },
  223. // 设置input的值,这里通过input事件,设置通过v-model绑定的组件的值
  224. setValue() {
  225. let value = this.valueCom;
  226. // 判断是否超过了可选的最大数量
  227. let checkedNum = 0;
  228. if (this.parent && this.parent.children) {
  229. // 只要父组件的某一个子元素的value为true,就加1(已有的选中数量)
  230. this.parent.children.map(val => {
  231. if (val.value) checkedNum++;
  232. });
  233. }
  234. // 如果原来为选中状态,那么可以取消
  235. if (value == true) {
  236. this.emitEvent();
  237. this.$emit("input", !value);
  238. this.$emit("update:modelValue", !value);
  239. } else {
  240. // 如果超出最多可选项,提示
  241. if (this.parent && checkedNum >= this.parent.max) {
  242. return this.$u.toast(`最多可选${this.parent.max}项`);
  243. }
  244. // 如果原来为未选中状态,需要选中的数量少于父组件中设置的max值,才可以选中
  245. this.emitEvent();
  246. this.$emit("input", !value);
  247. this.$emit("update:modelValue", !value);
  248. }
  249. }
  250. }
  251. };
  252. </script>
  253. <style lang="scss" scoped>
  254. @import "../../libs/css/style.components.scss";
  255. .u-checkbox {
  256. /* #ifndef APP-NVUE */
  257. display: inline-flex;
  258. /* #endif */
  259. align-items: center;
  260. overflow: hidden;
  261. user-select: none;
  262. line-height: 1.8;
  263. &__icon-wrap {
  264. color: $u-content-color;
  265. flex: none;
  266. display: -webkit-flex;
  267. @include vue-flex;
  268. align-items: center;
  269. justify-content: center;
  270. box-sizing: border-box;
  271. width: 42rpx;
  272. height: 42rpx;
  273. color: transparent;
  274. text-align: center;
  275. transition-property: color, border-color, background-color;
  276. font-size: 20px;
  277. border: 1px solid #c8c9cc;
  278. transition-duration: 0.2s;
  279. /* #ifdef MP-TOUTIAO */
  280. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  281. &__icon {
  282. line-height: 0;
  283. }
  284. /* #endif */
  285. &--circle {
  286. border-radius: 100%;
  287. }
  288. &--square {
  289. border-radius: 6rpx;
  290. }
  291. &--checked {
  292. color: #fff;
  293. background-color: $u-type-primary;
  294. border-color: $u-type-primary;
  295. }
  296. &--disabled {
  297. background-color: #ebedf0;
  298. border-color: #c8c9cc;
  299. }
  300. &--disabled--checked {
  301. color: #c8c9cc !important;
  302. }
  303. &--indeterminate {
  304. color: #fff;
  305. background-color: $u-type-primary;
  306. border-color: $u-type-primary;
  307. }
  308. }
  309. &__label {
  310. word-wrap: break-word;
  311. margin-left: 10rpx;
  312. margin-right: 24rpx;
  313. color: $u-content-color;
  314. font-size: 30rpx;
  315. &--disabled {
  316. color: #c8c9cc;
  317. }
  318. }
  319. }
  320. </style>