Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

u-checkbox.vue 8.1 KiB

há 3 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 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) {
  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. if(this.parent && this.parent.emitEvent) this.parent.emitEvent();
  179. },
  180. // 设置input的值,这里通过input事件,设置通过v-model绑定的组件的值
  181. setValue() {
  182. // 判断是否超过了可选的最大数量
  183. let checkedNum = 0;
  184. if(this.parent && this.parent.children) {
  185. // 只要父组件的某一个子元素的value为true,就加1(已有的选中数量)
  186. this.parent.children.map(val => {
  187. if (val.value) checkedNum++;
  188. })
  189. }
  190. // 如果原来为选中状态,那么可以取消
  191. if (this.value == true) {
  192. this.$emit('input', !this.value);
  193. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  194. this.$nextTick(function() {
  195. this.emitEvent();
  196. })
  197. } else if ((this.parent && checkedNum < this.parent.max) || !this.parent) {
  198. // 如果原来为未选中状态,需要选中的数量少于父组件中设置的max值,才可以选中
  199. this.$emit('input', !this.value);
  200. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  201. this.$nextTick(function() {
  202. this.emitEvent();
  203. })
  204. }
  205. }
  206. }
  207. };
  208. </script>
  209. <style lang="scss" scoped>
  210. @import "../../libs/css/style.components.scss";
  211. .u-checkbox {
  212. display: inline-flex;
  213. align-items: center;
  214. overflow: hidden;
  215. user-select: none;
  216. line-height: 1.8;
  217. &__icon-wrap {
  218. color: $u-content-color;
  219. flex: none;
  220. display: -webkit-flex;
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. box-sizing: border-box;
  225. width: 42rpx;
  226. height: 42rpx;
  227. color: transparent;
  228. text-align: center;
  229. transition-property: color, border-color, background-color;
  230. font-size: 20px;
  231. border: 1px solid #c8c9cc;
  232. transition-duration: 0.2s;
  233. &--circle {
  234. border-radius: 100%;
  235. }
  236. &--square {
  237. border-radius: 6rpx;
  238. }
  239. &--checked {
  240. color: #fff;
  241. background-color: $u-type-primary;
  242. border-color: $u-type-primary;
  243. }
  244. &--disabled {
  245. background-color: #ebedf0;
  246. border-color: #c8c9cc;
  247. }
  248. &--disabled--checked {
  249. color: #c8c9cc !important;
  250. }
  251. }
  252. &__label {
  253. word-wrap: break-word;
  254. margin-left: 10rpx;
  255. margin-right: 24rpx;
  256. color: $u-content-color;
  257. font-size: 30rpx;
  258. &--disabled {
  259. color: #c8c9cc;
  260. }
  261. }
  262. }
  263. </style>