You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
12 KiB

5 months ago
  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-raios-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"],
  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. }
  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. },
  217. updateParentData() {
  218. this.getParentData('u-checkbox-group')
  219. },
  220. // 横向两端排列时,点击组件即可触发选中事件
  221. wrapperClickHandler(e) {
  222. if (!this.usedAlone) {
  223. this.parentData.iconPlacement === 'right' && this.iconClickHandler(e)
  224. } else {
  225. this.iconClickHandler(e)
  226. }
  227. },
  228. // 点击图标
  229. iconClickHandler(e) {
  230. this.preventEvent(e)
  231. // 如果整体被禁用,不允许被点击
  232. if (!this.elDisabled) {
  233. this.setRadioCheckedStatus()
  234. }
  235. },
  236. // 点击label
  237. labelClickHandler(e) {
  238. this.preventEvent(e)
  239. // 如果按钮整体被禁用或者label被禁用,则不允许点击文字修改状态
  240. if (!this.elLabelDisabled && !this.elDisabled) {
  241. this.setRadioCheckedStatus()
  242. }
  243. },
  244. emitEvent() {
  245. this.$emit('change', this.isChecked)
  246. // 尝试调用u-form的验证方法,进行一定延迟,否则微信小程序更新可能会不及时
  247. this.$nextTick(() => {
  248. formValidate(this, 'change')
  249. })
  250. },
  251. // 改变组件选中状态
  252. // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有u-checkbox实例
  253. // 将本组件外的其他u-checkbox的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
  254. setRadioCheckedStatus() {
  255. // 将本组件标记为与原来相反的状态
  256. this.isChecked = !this.isChecked
  257. this.emitEvent()
  258. if (!this.usedAlone) {
  259. typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this)
  260. }
  261. }
  262. },
  263. watch:{
  264. checked(){
  265. this.isChecked = this.checked
  266. }
  267. }
  268. }
  269. </script>
  270. <style lang="scss" scoped>
  271. @import "../../libs/css/components.scss";
  272. $u-checkbox-icon-wrap-margin-right:6px !default;
  273. $u-checkbox-icon-wrap-font-size:6px !default;
  274. $u-checkbox-icon-wrap-border-width:1px !default;
  275. $u-checkbox-icon-wrap-border-color:#c8c9cc !default;
  276. $u-checkbox-icon-wrap-icon-line-height:0 !default;
  277. $u-checkbox-icon-wrap-circle-border-radius:100% !default;
  278. $u-checkbox-icon-wrap-square-border-radius:3px !default;
  279. $u-checkbox-icon-wrap-checked-color:#fff !default;
  280. $u-checkbox-icon-wrap-checked-background-color:red !default;
  281. $u-checkbox-icon-wrap-checked-border-color:#2979ff !default;
  282. $u-checkbox-icon-wrap-disabled-background-color:#ebedf0 !default;
  283. $u-checkbox-icon-wrap-disabled-checked-color:#c8c9cc !default;
  284. $u-checkbox-label-margin-left:5px !default;
  285. $u-checkbox-label-margin-right:12px !default;
  286. $u-checkbox-label-color:$u-content-color !default;
  287. $u-checkbox-label-font-size:15px !default;
  288. $u-checkbox-label-disabled-color:#c8c9cc !default;
  289. .u-checkbox {
  290. /* #ifndef APP-NVUE */
  291. @include flex(row);
  292. /* #endif */
  293. overflow: hidden;
  294. flex-direction: row;
  295. align-items: center;
  296. margin-bottom: 5px;
  297. margin-top: 5px;
  298. &-label--left {
  299. flex-direction: row
  300. }
  301. &-label--right {
  302. flex-direction: row-reverse;
  303. justify-content: space-between
  304. }
  305. &__icon-wrap {
  306. /* #ifndef APP-NVUE */
  307. box-sizing: border-box;
  308. // nvue下,border-color过渡有问题
  309. transition-property: border-color, background-color, color;
  310. transition-duration: 0.2s;
  311. /* #endif */
  312. color: $u-content-color;
  313. @include flex;
  314. align-items: center;
  315. justify-content: center;
  316. color: transparent;
  317. text-align: center;
  318. margin-right: $u-checkbox-icon-wrap-margin-right;
  319. font-size: $u-checkbox-icon-wrap-font-size;
  320. border-width: $u-checkbox-icon-wrap-border-width;
  321. border-color: $u-checkbox-icon-wrap-border-color;
  322. border-style: solid;
  323. /* #ifdef MP-TOUTIAO */
  324. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  325. &__icon {
  326. line-height: $u-checkbox-icon-wrap-icon-line-height;
  327. }
  328. /* #endif */
  329. &--circle {
  330. border-radius: $u-checkbox-icon-wrap-circle-border-radius;
  331. }
  332. &--square {
  333. border-radius: $u-checkbox-icon-wrap-square-border-radius;
  334. }
  335. &--checked {
  336. color: $u-checkbox-icon-wrap-checked-color;
  337. background-color: $u-checkbox-icon-wrap-checked-background-color;
  338. border-color: $u-checkbox-icon-wrap-checked-border-color;
  339. }
  340. &--disabled {
  341. background-color: $u-checkbox-icon-wrap-disabled-background-color !important;
  342. }
  343. &--disabled--checked {
  344. color: $u-checkbox-icon-wrap-disabled-checked-color !important;
  345. }
  346. }
  347. &__label {
  348. /* #ifndef APP-NVUE */
  349. word-wrap: break-word;
  350. /* #endif */
  351. margin-left: $u-checkbox-label-margin-left;
  352. margin-right: $u-checkbox-label-margin-right;
  353. color: $u-checkbox-label-color;
  354. font-size: $u-checkbox-label-font-size;
  355. &--disabled {
  356. color: $u-checkbox-label-disabled-color;
  357. }
  358. }
  359. }
  360. </style>