u-checkbox.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <view
  3. class="u-checkbox cursor-pointer"
  4. :style="[checkboxStyle]"
  5. @tap.stop="wrapperClickHandler"
  6. :class="[`u-checkbox-label--${parentData.iconPlacement}`, parentData.borderBottom && parentData.placement === 'column' && 'u-border-bottom']"
  7. >
  8. <view
  9. class="u-checkbox__icon-wrap cursor-pointer"
  10. @tap.stop="iconClickHandler"
  11. :class="iconClasses"
  12. :style="[iconWrapStyle]"
  13. >
  14. <slot name="icon">
  15. <u-icon
  16. class="u-checkbox__icon-wrap__icon"
  17. name="checkbox-mark"
  18. :size="elIconSize"
  19. :color="elIconColor"
  20. />
  21. </slot>
  22. </view>
  23. <view class="u-checkbox__label-wrap cursor-pointer" @tap.stop="labelClickHandler">
  24. <slot name="label" :label="label" :elDisabled="elDisabled">
  25. <text
  26. :style="{
  27. color: elDisabled ? elInactiveColor : elLabelColor,
  28. fontSize: elLabelSize,
  29. lineHeight: elLabelSize
  30. }"
  31. >{{label}}</text>
  32. </slot>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import { props } from './props';
  38. import { mpMixin } from '../../libs/mixin/mpMixin';
  39. import { mixin } from '../../libs/mixin/mixin';
  40. import { addStyle, addUnit, deepMerge, formValidate, error } from '../../libs/function/index';
  41. import test from '../../libs/function/test';
  42. /**
  43. * checkbox 复选框
  44. * @description 复选框组件一般用于需要多个选择的场景,该组件功能完整,使用方便
  45. * @tutorial https://uview-plus.jiangruyi.com/components/checkbox.html
  46. * @property {String | Number | Boolean} name checkbox组件的标示符
  47. * @property {String} shape 形状,square为方形,circle为圆型
  48. * @property {String | Number} size 整体的大小
  49. * @property {Boolean} checked 是否默认选中
  50. * @property {String | Boolean} disabled 是否禁用
  51. * @property {String} activeColor 选中状态下的颜色,如设置此值,将会覆盖parent的activeColor值
  52. * @property {String} inactiveColor 未选中的颜色
  53. * @property {String | Number} iconSize 图标的大小,单位px
  54. * @property {String} iconColor 图标颜色
  55. * @property {String | Number} label label提示文字,因为nvue下,直接slot进来的文字,由于特殊的结构,无法修改样式
  56. * @property {String} labelColor label的颜色
  57. * @property {String | Number} labelSize label的字体大小,px单位
  58. * @property {String | Boolean} labelDisabled 是否禁止点击提示语选中复选框
  59. * @property {Object} customStyle 定义需要用到的外部样式
  60. *
  61. * @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
  62. * @example <u-checkbox v-model="checked" :disabled="false">天涯</u-checkbox>
  63. */
  64. export default {
  65. name: "u-checkbox",
  66. mixins: [mpMixin, mixin, props],
  67. data() {
  68. return {
  69. isChecked: false,
  70. // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式
  71. // 故只能使用如此方法
  72. parentData: {
  73. iconSize: 12,
  74. labelDisabled: null,
  75. disabled: null,
  76. shape: 'square',
  77. activeColor: null,
  78. inactiveColor: null,
  79. size: 18,
  80. // #ifdef VUE2
  81. value: null,
  82. // #endif
  83. // #ifdef VUE3
  84. modelValue: null,
  85. // #endif
  86. iconColor: null,
  87. placement: 'row',
  88. borderBottom: false,
  89. iconPlacement: 'left'
  90. }
  91. }
  92. },
  93. computed: {
  94. // 是否禁用,如果父组件u-radios-group禁用的话,将会忽略子组件的配置
  95. elDisabled() {
  96. return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
  97. },
  98. // 是否禁用label点击
  99. elLabelDisabled() {
  100. return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled :
  101. false;
  102. },
  103. // 组件尺寸,对应size的值,默认值为21px
  104. elSize() {
  105. return this.size ? this.size : (this.parentData.size ? this.parentData.size : 21);
  106. },
  107. // 组件的勾选图标的尺寸,默认12px
  108. elIconSize() {
  109. return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 12);
  110. },
  111. // 组件选中激活时的颜色
  112. elActiveColor() {
  113. return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : '#2979ff');
  114. },
  115. // 组件选未中激活时的颜色
  116. elInactiveColor() {
  117. return this.inactiveColor ? this.inactiveColor : (this.parentData.inactiveColor ? this.parentData.inactiveColor :
  118. '#c8c9cc');
  119. },
  120. // label的颜色
  121. elLabelColor() {
  122. return this.labelColor ? this.labelColor : (this.parentData.labelColor ? this.parentData.labelColor : '#606266')
  123. },
  124. // 组件的形状
  125. elShape() {
  126. return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
  127. },
  128. // label大小
  129. elLabelSize() {
  130. return addUnit(this.labelSize ? this.labelSize : (this.parentData.labelSize ? this.parentData.labelSize :
  131. '15'))
  132. },
  133. elIconColor() {
  134. const iconColor = this.iconColor ? this.iconColor : (this.parentData.iconColor ? this.parentData.iconColor :
  135. '#ffffff');
  136. // 图标的颜色
  137. if (this.elDisabled) {
  138. // disabled状态下,已勾选的checkbox图标改为elInactiveColor
  139. return this.isChecked ? this.elInactiveColor : 'transparent'
  140. } else {
  141. return this.isChecked ? iconColor : 'transparent'
  142. }
  143. },
  144. iconClasses() {
  145. let classes = []
  146. // 组件的形状
  147. classes.push('u-checkbox__icon-wrap--' + this.elShape)
  148. if (this.elDisabled) {
  149. classes.push('u-checkbox__icon-wrap--disabled')
  150. }
  151. if (this.isChecked && this.elDisabled) {
  152. classes.push('u-checkbox__icon-wrap--disabled--checked')
  153. }
  154. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  155. // #ifdef MP-ALIPAY || MP-TOUTIAO
  156. classes = classes.join(' ')
  157. // #endif
  158. return classes
  159. },
  160. iconWrapStyle() {
  161. // checkbox的整体样式
  162. const style = {}
  163. style.backgroundColor = this.isChecked && !this.elDisabled ? this.elActiveColor : '#ffffff'
  164. style.borderColor = this.isChecked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor
  165. style.width = addUnit(this.elSize)
  166. style.height = addUnit(this.elSize)
  167. // 如果是图标在右边的话,移除它的右边距
  168. if (!this.usedAlone) {
  169. if (this.parentData.iconPlacement === 'right') {
  170. style.marginRight = 0
  171. }
  172. }
  173. return style
  174. },
  175. checkboxStyle() {
  176. const style = {}
  177. if (!this.usedAlone) {
  178. if (this.parentData.borderBottom && this.parentData.placement === 'row') {
  179. error('检测到您将borderBottom设置为true,需要同时将u-checkbox-group的placement设置为column才有效')
  180. }
  181. // 当父组件设置了显示下边框并且排列形式为纵向时,给内容和边框之间加上一定间隔
  182. if (this.parentData.borderBottom && this.parentData.placement === 'column') {
  183. style.paddingBottom = '8px'
  184. }
  185. }
  186. return deepMerge(style, addStyle(this.customStyle))
  187. }
  188. },
  189. mounted() {
  190. this.init()
  191. },
  192. emits: ["change", "update:checked"],
  193. methods: {
  194. init() {
  195. if (!this.usedAlone) {
  196. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  197. this.updateParentData()
  198. if (!this.parent) {
  199. error('u-checkbox必须搭配u-checkbox-group组件使用')
  200. }
  201. // #ifdef VUE2
  202. const value = this.parentData.value
  203. // #endif
  204. // #ifdef VUE3
  205. const value = this.parentData.modelValue
  206. // #endif
  207. // 设置初始化时,是否默认选中的状态,父组件u-checkbox-group的value可能是array,所以额外判断
  208. if (this.checked) {
  209. this.isChecked = true
  210. } else if (!this.usedAlone && test.array(value)) {
  211. // 查找数组是是否存在this.name元素值
  212. this.isChecked = value.some(item => {
  213. return item === this.name
  214. })
  215. }
  216. } else {
  217. if (this.checked) {
  218. this.isChecked = true
  219. }
  220. }
  221. },
  222. updateParentData() {
  223. this.getParentData('u-checkbox-group')
  224. },
  225. // 横向两端排列时,点击组件即可触发选中事件
  226. wrapperClickHandler(e) {
  227. if (!this.usedAlone) {
  228. this.parentData.iconPlacement === 'right' && this.iconClickHandler(e)
  229. } else {
  230. this.iconClickHandler(e)
  231. }
  232. },
  233. // 点击图标
  234. iconClickHandler(e) {
  235. this.preventEvent(e)
  236. // 如果整体被禁用,不允许被点击
  237. if (!this.elDisabled) {
  238. this.setRadioCheckedStatus()
  239. }
  240. },
  241. // 点击label
  242. labelClickHandler(e) {
  243. this.preventEvent(e)
  244. // 如果按钮整体被禁用或者label被禁用,则不允许点击文字修改状态
  245. if (!this.elLabelDisabled && !this.elDisabled) {
  246. this.setRadioCheckedStatus()
  247. }
  248. },
  249. emitEvent() {
  250. this.$emit('change', this.isChecked)
  251. // 双向绑定
  252. if (this.usedAlone) {
  253. this.$emit('update:checked', this.isChecked)
  254. }
  255. // 尝试调用u-form的验证方法,进行一定延迟,否则微信小程序更新可能会不及时
  256. this.$nextTick(() => {
  257. formValidate(this, 'change')
  258. })
  259. },
  260. // 改变组件选中状态
  261. // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有u-checkbox实例
  262. // 将本组件外的其他u-checkbox的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
  263. setRadioCheckedStatus() {
  264. // 将本组件标记为与原来相反的状态
  265. this.isChecked = !this.isChecked
  266. this.emitEvent()
  267. if (!this.usedAlone) {
  268. typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this)
  269. }
  270. }
  271. },
  272. watch:{
  273. checked(newValue, oldValue){
  274. if (newValue !== this.isChecked) {
  275. this.isChecked = newValue
  276. }
  277. }
  278. }
  279. }
  280. </script>
  281. <style lang="scss" scoped>
  282. @import "../../libs/css/components.scss";
  283. $u-checkbox-icon-wrap-margin-right:6px !default;
  284. $u-checkbox-icon-wrap-font-size:6px !default;
  285. $u-checkbox-icon-wrap-border-width:1px !default;
  286. $u-checkbox-icon-wrap-border-color:#c8c9cc !default;
  287. $u-checkbox-icon-wrap-icon-line-height:0 !default;
  288. $u-checkbox-icon-wrap-circle-border-radius:100% !default;
  289. $u-checkbox-icon-wrap-square-border-radius:3px !default;
  290. $u-checkbox-icon-wrap-checked-color:#fff !default;
  291. $u-checkbox-icon-wrap-checked-background-color:red !default;
  292. $u-checkbox-icon-wrap-checked-border-color:#2979ff !default;
  293. $u-checkbox-icon-wrap-disabled-background-color:#ebedf0 !default;
  294. $u-checkbox-icon-wrap-disabled-checked-color:#c8c9cc !default;
  295. $u-checkbox-label-margin-left:5px !default;
  296. $u-checkbox-label-margin-right:12px !default;
  297. $u-checkbox-label-color:$u-content-color !default;
  298. $u-checkbox-label-font-size:15px !default;
  299. $u-checkbox-label-disabled-color:#c8c9cc !default;
  300. .u-checkbox {
  301. /* #ifndef APP-NVUE */
  302. @include flex(row);
  303. /* #endif */
  304. overflow: hidden;
  305. flex-direction: row;
  306. align-items: center;
  307. margin-bottom: 5px;
  308. margin-top: 5px;
  309. &-label--left {
  310. flex-direction: row
  311. }
  312. &-label--right {
  313. flex-direction: row-reverse;
  314. justify-content: space-between
  315. }
  316. &__icon-wrap {
  317. /* #ifndef APP-NVUE */
  318. box-sizing: border-box;
  319. // nvue下,border-color过渡有问题
  320. transition-property: border-color, background-color, color;
  321. transition-duration: 0.2s;
  322. /* #endif */
  323. color: $u-content-color;
  324. @include flex;
  325. align-items: center;
  326. justify-content: center;
  327. color: transparent;
  328. text-align: center;
  329. margin-right: $u-checkbox-icon-wrap-margin-right;
  330. font-size: $u-checkbox-icon-wrap-font-size;
  331. border-width: $u-checkbox-icon-wrap-border-width;
  332. border-color: $u-checkbox-icon-wrap-border-color;
  333. border-style: solid;
  334. /* #ifdef MP-TOUTIAO */
  335. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  336. &__icon {
  337. line-height: $u-checkbox-icon-wrap-icon-line-height;
  338. }
  339. /* #endif */
  340. &--circle {
  341. border-radius: $u-checkbox-icon-wrap-circle-border-radius;
  342. }
  343. &--square {
  344. border-radius: $u-checkbox-icon-wrap-square-border-radius;
  345. }
  346. &--checked {
  347. color: $u-checkbox-icon-wrap-checked-color;
  348. background-color: $u-checkbox-icon-wrap-checked-background-color;
  349. border-color: $u-checkbox-icon-wrap-checked-border-color;
  350. }
  351. &--disabled {
  352. background-color: $u-checkbox-icon-wrap-disabled-background-color !important;
  353. }
  354. &--disabled--checked {
  355. color: $u-checkbox-icon-wrap-disabled-checked-color !important;
  356. }
  357. }
  358. &__label {
  359. /* #ifndef APP-NVUE */
  360. word-wrap: break-word;
  361. /* #endif */
  362. margin-left: $u-checkbox-label-margin-left;
  363. margin-right: $u-checkbox-label-margin-right;
  364. color: $u-checkbox-label-color;
  365. font-size: $u-checkbox-label-font-size;
  366. &--disabled {
  367. color: $u-checkbox-label-disabled-color;
  368. }
  369. }
  370. }
  371. </style>