u-checkbox.vue 13 KB

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