综合办公系统
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

uv-checkbox.vue 12KB

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