u-checkbox.vue 12 KB

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