u-checkbox.vue 12 KB

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