u-checkbox.vue 12 KB

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