您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

u-checkbox.vue 8.6KB

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