u-checkbox.vue 9.1 KB

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