u-checkbox.vue 15 KB

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