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.
 
 
 

394 lines
13 KiB

  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" :elIconSize="elIconSize" :elIconColor="elIconColor" :checked="isChecked" :elDisabled="elDisabled">
  15. <up-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 : this.upThemeVar('--up-primary', '#2979ff'));
  114. },
  115. // 组件选未中激活时的颜色
  116. elInactiveColor() {
  117. return this.inactiveColor ? this.inactiveColor : (this.parentData.inactiveColor ? this.parentData.inactiveColor :
  118. this.upThemeVar('--up-border-color', '#c8c9cc'));
  119. },
  120. // label的颜色
  121. elLabelColor() {
  122. return this.labelColor ? this.labelColor : (this.parentData.labelColor ? this.parentData.labelColor : this.upThemeVar('--up-content-color', '#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. this.upThemeVar('--up-card-bg-color', '#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) {
  152. if (this.elDisabled) {
  153. classes.push('u-checkbox__icon-wrap--disabled--checked')
  154. } else {
  155. classes.push('u-checkbox__icon-wrap--checked')
  156. }
  157. }
  158. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  159. // #ifdef MP-ALIPAY || MP-TOUTIAO
  160. classes = classes.join(' ')
  161. // #endif
  162. return classes
  163. },
  164. iconWrapStyle() {
  165. // checkbox的整体样式
  166. const style = {}
  167. style.backgroundColor = this.isChecked && !this.elDisabled ? this.elActiveColor : this.upThemeVar('--up-card-bg-color', '#ffffff')
  168. style.borderColor = this.isChecked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor
  169. style.width = addUnit(this.elSize)
  170. style.height = addUnit(this.elSize)
  171. // 如果是图标在右边的话,移除它的右边距
  172. if (!this.usedAlone) {
  173. if (this.parentData.iconPlacement === 'right') {
  174. style.marginRight = 0
  175. }
  176. }
  177. return style
  178. },
  179. checkboxStyle() {
  180. const style = {}
  181. if (!this.usedAlone) {
  182. if (this.parentData.borderBottom && this.parentData.placement === 'row') {
  183. error('检测到您将borderBottom设置为true,需要同时将up-checkbox-group的placement设置为column才有效')
  184. }
  185. // 当父组件设置了显示下边框并且排列形式为纵向时,给内容和边框之间加上一定间隔
  186. if (this.parentData.borderBottom && this.parentData.placement === 'column') {
  187. style.paddingBottom = '8px'
  188. }
  189. }
  190. return deepMerge(style, addStyle(this.customStyle))
  191. }
  192. },
  193. mounted() {
  194. this.init()
  195. },
  196. emits: ["change", "update:checked"],
  197. methods: {
  198. init() {
  199. if (!this.usedAlone) {
  200. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  201. this.updateParentData()
  202. if (!this.parent) {
  203. error('up-checkbox必须搭配up-checkbox-group组件使用')
  204. }
  205. let value = '';
  206. // #ifdef VUE2
  207. value = this.parentData.value
  208. // #endif
  209. // #ifdef VUE3
  210. value = this.parentData.modelValue
  211. // #endif
  212. // 设置初始化时,是否默认选中的状态,父组件u-checkbox-group的value可能是array,所以额外判断
  213. if (this.checked) {
  214. this.isChecked = true
  215. } else if (!this.usedAlone && test.array(value)) {
  216. // 查找数组是是否存在this.name元素值
  217. this.isChecked = value.some(item => {
  218. return item === this.name
  219. })
  220. }
  221. } else {
  222. if (this.checked) {
  223. this.isChecked = true
  224. }
  225. }
  226. },
  227. updateParentData() {
  228. this.getParentData('u-checkbox-group')
  229. },
  230. // 横向两端排列时,点击组件即可触发选中事件
  231. wrapperClickHandler(e) {
  232. if (!this.usedAlone) {
  233. this.parentData.iconPlacement === 'right' && this.iconClickHandler(e)
  234. } else {
  235. this.iconClickHandler(e)
  236. }
  237. },
  238. // 点击图标
  239. iconClickHandler(e) {
  240. this.preventEvent(e)
  241. // 如果整体被禁用,不允许被点击
  242. if (!this.elDisabled) {
  243. this.setRadioCheckedStatus()
  244. }
  245. },
  246. // 点击label
  247. labelClickHandler(e) {
  248. this.preventEvent(e)
  249. // 如果按钮整体被禁用或者label被禁用,则不允许点击文字修改状态
  250. if (!this.elLabelDisabled && !this.elDisabled) {
  251. this.setRadioCheckedStatus()
  252. }
  253. },
  254. emitEvent() {
  255. this.$emit('change', this.isChecked, {
  256. name: this.name
  257. })
  258. // 双向绑定
  259. if (this.usedAlone) {
  260. this.$emit('update:checked', this.isChecked)
  261. }
  262. // 尝试调用u-form的验证方法,进行一定延迟,否则微信小程序更新可能会不及时
  263. this.$nextTick(() => {
  264. formValidate(this, 'change')
  265. })
  266. },
  267. // 改变组件选中状态
  268. // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有u-checkbox实例
  269. // 将本组件外的其他u-checkbox的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
  270. setRadioCheckedStatus() {
  271. // 将本组件标记为与原来相反的状态
  272. this.isChecked = !this.isChecked
  273. this.emitEvent()
  274. if (!this.usedAlone) {
  275. typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this)
  276. }
  277. }
  278. },
  279. watch:{
  280. checked(newValue, oldValue){
  281. if (newValue !== this.isChecked) {
  282. this.isChecked = newValue
  283. }
  284. }
  285. }
  286. }
  287. </script>
  288. <style lang="scss" scoped>
  289. $u-checkbox-icon-wrap-margin-right:6px !default;
  290. $u-checkbox-icon-wrap-font-size:6px !default;
  291. $u-checkbox-icon-wrap-border-width:1px !default;
  292. $u-checkbox-icon-wrap-border-color:#c8c9cc !default;
  293. $u-checkbox-icon-wrap-icon-line-height:0 !default;
  294. $u-checkbox-icon-wrap-circle-border-radius:100% !default;
  295. $u-checkbox-icon-wrap-square-border-radius:3px !default;
  296. $u-checkbox-icon-wrap-checked-color:#fff !default;
  297. $u-checkbox-icon-wrap-checked-background-color:red !default;
  298. $u-checkbox-icon-wrap-checked-border-color:#2979ff !default;
  299. $u-checkbox-icon-wrap-disabled-background-color:var(--up-bg-color, #ebedf0) !default;
  300. $u-checkbox-icon-wrap-disabled-checked-color:#c8c9cc !default;
  301. $u-checkbox-label-margin-left:5px !default;
  302. $u-checkbox-label-margin-right:12px !default;
  303. $u-checkbox-label-color:$u-content-color !default;
  304. $u-checkbox-label-font-size:15px !default;
  305. $u-checkbox-label-disabled-color:#c8c9cc !default;
  306. .u-checkbox {
  307. /* #ifndef APP-NVUE */
  308. @include flex(row);
  309. /* #endif */
  310. overflow: hidden;
  311. flex-direction: row;
  312. align-items: center;
  313. margin-bottom: 5px;
  314. margin-top: 5px;
  315. &-label--left {
  316. flex-direction: row
  317. }
  318. &-label--right {
  319. flex-direction: row-reverse;
  320. justify-content: space-between
  321. }
  322. &__icon-wrap {
  323. /* #ifndef APP-NVUE */
  324. box-sizing: border-box;
  325. // nvue下,border-color过渡有问题
  326. transition-property: border-color, background-color, color;
  327. transition-duration: 0.2s;
  328. /* #endif */
  329. color: $u-content-color;
  330. @include flex;
  331. align-items: center;
  332. justify-content: center;
  333. color: transparent;
  334. text-align: center;
  335. margin-right: $u-checkbox-icon-wrap-margin-right;
  336. font-size: $u-checkbox-icon-wrap-font-size;
  337. border-width: $u-checkbox-icon-wrap-border-width;
  338. border-color: $u-checkbox-icon-wrap-border-color;
  339. border-style: solid;
  340. /* #ifdef MP-TOUTIAO */
  341. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  342. &__icon {
  343. line-height: $u-checkbox-icon-wrap-icon-line-height;
  344. }
  345. /* #endif */
  346. &--circle {
  347. border-radius: $u-checkbox-icon-wrap-circle-border-radius;
  348. }
  349. &--square {
  350. border-radius: $u-checkbox-icon-wrap-square-border-radius;
  351. }
  352. &--checked {
  353. color: $u-checkbox-icon-wrap-checked-color;
  354. background-color: $u-checkbox-icon-wrap-checked-background-color;
  355. border-color: $u-checkbox-icon-wrap-checked-border-color;
  356. }
  357. &--disabled {
  358. background-color: $u-checkbox-icon-wrap-disabled-background-color !important;
  359. }
  360. &--disabled--checked {
  361. color: $u-checkbox-icon-wrap-disabled-checked-color !important;
  362. }
  363. }
  364. &__label {
  365. /* #ifndef APP-NVUE */
  366. word-wrap: break-word;
  367. /* #endif */
  368. margin-left: $u-checkbox-label-margin-left;
  369. margin-right: $u-checkbox-label-margin-right;
  370. color: $u-checkbox-label-color;
  371. font-size: $u-checkbox-label-font-size;
  372. &--disabled {
  373. color: $u-checkbox-label-disabled-color;
  374. }
  375. }
  376. }
  377. </style>