AI销管
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

285 lines
8.3 KiB

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