u-checkbox.vue 8.3 KB

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