u-checkbox.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 class="u-checkbox__icon-wrap__icon" name="checkbox-mark" :size="checkboxIconSize" :color="iconColor" />
  5. </view>
  6. <view
  7. class="u-checkbox__label"
  8. @tap="onClickLabel"
  9. :style="{
  10. fontSize: $u.addUnit(labelSize),
  11. color: labelColor
  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} label-color label字体颜色,默认#606266) (2022-05-29 花儿为何这样红 新增)
  26. * @property {String Number} name checkbox组件的标示符
  27. * @property {String} shape 形状,外观形状,shape-方形,circle-圆形(默认circle)
  28. * @property {Boolean} disabled 是否禁用
  29. * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox
  30. * @property {String} active-color 选中时的颜色,如设置CheckboxGroup的active-color将失效
  31. * @event {Function} change 某个checkbox状态发生变化时触发,回调为一个对象
  32. * @example <u-checkbox v-model="checked" :disabled="false">天涯</u-checkbox>
  33. */
  34. export default {
  35. name: 'u-checkbox',
  36. emits: ['update:modelValue', 'input', 'change'],
  37. props: {
  38. // 是否为选中状态
  39. value: {
  40. type: Boolean,
  41. default: false
  42. },
  43. modelValue: {
  44. type: Boolean,
  45. default: false
  46. },
  47. // checkbox的名称
  48. name: {
  49. type: [String, Number],
  50. default: ''
  51. },
  52. // 形状,square为方形,circle为圆型
  53. shape: {
  54. type: String,
  55. default: ''
  56. },
  57. // 是否禁用
  58. disabled: {
  59. type: [String, Boolean],
  60. default: ''
  61. },
  62. // 是否禁止点击提示语选中复选框
  63. labelDisabled: {
  64. type: [String, Boolean],
  65. default: ''
  66. },
  67. // 选中状态下的颜色,如设置此值,将会覆盖checkboxGroup的activeColor值
  68. activeColor: {
  69. type: String,
  70. default: ''
  71. },
  72. // 图标的大小,单位rpx
  73. iconSize: {
  74. type: [String, Number],
  75. default: ''
  76. },
  77. // label的字体大小,rpx单位
  78. labelSize: {
  79. type: [String, Number],
  80. default: ''
  81. },
  82. // label的颜色
  83. labelColor: {
  84. type: [String, Number],
  85. default: "#606266"
  86. },
  87. // 组件的整体大小
  88. size: {
  89. type: [String, Number],
  90. default: ''
  91. }
  92. },
  93. data() {
  94. return {
  95. parentDisabled: false,
  96. newParams: {}
  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. // #ifndef VUE3
  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. return this.valueCom ? '#ffffff' : 'transparent';
  152. },
  153. iconClass() {
  154. let classes = [];
  155. classes.push('u-checkbox__icon-wrap--' + this.elShape);
  156. if (this.valueCom == true) classes.push('u-checkbox__icon-wrap--checked');
  157. if (this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled');
  158. if (this.valueCom && this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled--checked');
  159. // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  160. return classes.join(' ');
  161. },
  162. checkboxStyle() {
  163. let style = {};
  164. if (this.parent && this.parent.width) {
  165. style.width = this.parent.width;
  166. // #ifdef MP
  167. // 各家小程序因为它们特殊的编译结构,使用float布局
  168. style.float = 'left';
  169. // #endif
  170. // #ifndef MP
  171. // H5和APP使用flex布局
  172. style.flex = `0 0 ${this.parent.width}`;
  173. // #endif
  174. }
  175. if (this.parent && this.parent.wrap) {
  176. style.width = '100%';
  177. // #ifndef MP
  178. // H5和APP使用flex布局,将宽度设置100%,即可自动换行
  179. style.flex = '0 0 100%';
  180. // #endif
  181. }
  182. return style;
  183. }
  184. },
  185. mounted() {
  186. this._emitEvent();
  187. },
  188. watch: {
  189. valueCom: {
  190. handler: function(newVal, oldVal) {
  191. this._emitEvent();
  192. }
  193. }
  194. },
  195. methods: {
  196. _emitEvent() {
  197. let value = this.valueCom;
  198. let obj = {
  199. value,
  200. name: this.name
  201. };
  202. // 执行父组件u-checkbox-group的事件方法
  203. if (this.parent && this.parent.emitEvent) this.parent._emitEvent(obj);
  204. },
  205. onClickLabel() {
  206. if (!this.isLabelDisabled && !this.isDisabled) {
  207. this.setValue();
  208. }
  209. },
  210. toggle() {
  211. if (!this.isDisabled) {
  212. this.setValue();
  213. }
  214. },
  215. emitEvent() {
  216. let obj = {
  217. value: !this.valueCom,
  218. name: this.name
  219. };
  220. this.$emit('change', obj);
  221. // 执行父组件u-checkbox-group的事件方法
  222. if (this.parent && this.parent.emitEvent) this.parent.emitEvent(obj);
  223. },
  224. // 设置input的值,这里通过input事件,设置通过v-model绑定的组件的值
  225. setValue() {
  226. let value = this.valueCom;
  227. // 判断是否超过了可选的最大数量
  228. let checkedNum = 0;
  229. if (this.parent && this.parent.children) {
  230. // 只要父组件的某一个子元素的value为true,就加1(已有的选中数量)
  231. this.parent.children.map(val => {
  232. if (val.value) checkedNum++;
  233. });
  234. }
  235. // 如果原来为选中状态,那么可以取消
  236. if (value == true) {
  237. this.emitEvent();
  238. this.$emit('input', !value);
  239. this.$emit('update:modelValue', !value);
  240. } else {
  241. // 如果超出最多可选项,提示
  242. if (this.parent && checkedNum >= this.parent.max) {
  243. return this.$u.toast(`最多可选${this.parent.max}项`);
  244. }
  245. // 如果原来为未选中状态,需要选中的数量少于父组件中设置的max值,才可以选中
  246. this.emitEvent();
  247. this.$emit('input', !value);
  248. this.$emit('update:modelValue', !value);
  249. }
  250. }
  251. }
  252. };
  253. </script>
  254. <style lang="scss" scoped>
  255. @import '../../libs/css/style.components.scss';
  256. .u-checkbox {
  257. /* #ifndef APP-NVUE */
  258. display: inline-flex;
  259. /* #endif */
  260. align-items: center;
  261. overflow: hidden;
  262. user-select: none;
  263. line-height: 1.8;
  264. &__icon-wrap {
  265. color: $u-content-color;
  266. flex: none;
  267. display: -webkit-flex;
  268. @include vue-flex;
  269. align-items: center;
  270. justify-content: center;
  271. box-sizing: border-box;
  272. width: 42rpx;
  273. height: 42rpx;
  274. color: transparent;
  275. text-align: center;
  276. transition-property: color, border-color, background-color;
  277. font-size: 20px;
  278. border: 1px solid #c8c9cc;
  279. transition-duration: 0.2s;
  280. /* #ifdef MP-TOUTIAO */
  281. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  282. &__icon {
  283. line-height: 0;
  284. }
  285. /* #endif */
  286. &--circle {
  287. border-radius: 100%;
  288. }
  289. &--square {
  290. border-radius: 6rpx;
  291. }
  292. &--checked {
  293. color: #fff;
  294. background-color: $u-type-primary;
  295. border-color: $u-type-primary;
  296. }
  297. &--disabled {
  298. background-color: #ebedf0;
  299. border-color: #c8c9cc;
  300. }
  301. &--disabled--checked {
  302. color: #c8c9cc !important;
  303. }
  304. }
  305. &__label {
  306. word-wrap: break-word;
  307. margin-left: 10rpx;
  308. margin-right: 24rpx;
  309. color: $u-content-color;
  310. font-size: 30rpx;
  311. &--disabled {
  312. color: #c8c9cc;
  313. }
  314. }
  315. }
  316. </style>