¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view |
| | | class="u-checkbox" |
| | | :style="[checkboxStyle]" |
| | | @tap.stop="wrapperClickHandler" |
| | | :class="[`u-checkbox-label--${parentData.iconPlacement}`, parentData.borderBottom && parentData.placement === 'column' && 'u-border-bottom']" |
| | | > |
| | | <view |
| | | class="u-checkbox__icon-wrap" |
| | | @tap.stop="iconClickHandler" |
| | | :class="iconClasses" |
| | | :style="[iconWrapStyle]" |
| | | > |
| | | <slot name="icon"> |
| | | <u-icon |
| | | class="u-checkbox__icon-wrap__icon" |
| | | name="checkbox-mark" |
| | | :size="elIconSize" |
| | | :color="elIconColor" |
| | | /> |
| | | </slot> |
| | | </view> |
| | | <text |
| | | @tap.stop="labelClickHandler" |
| | | :style="{ |
| | | color: elDisabled ? elInactiveColor : elLabelColor, |
| | | fontSize: elLabelSize, |
| | | lineHeight: elLabelSize |
| | | }" |
| | | >{{label}}</text> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * checkbox å¤éæ¡ |
| | | * @description å¤éæ¡ç»ä»¶ä¸è¬ç¨äºéè¦å¤ä¸ªéæ©çåºæ¯ï¼è¯¥ç»ä»¶åè½å®æ´ï¼ä½¿ç¨æ¹ä¾¿ |
| | | * @tutorial https://uviewui.com/components/checkbox.html |
| | | * @property {String | Number | Boolean} name checkboxç»ä»¶çæ 示符 |
| | | * @property {String} shape å½¢ç¶ï¼square为æ¹å½¢ï¼circle为åå |
| | | * @property {String | Number} size æ´ä½çå¤§å° |
| | | * @property {Boolean} checked æ¯å¦é»è®¤éä¸ |
| | | * @property {String | Boolean} disabled æ¯å¦ç¦ç¨ |
| | | * @property {String} activeColor éä¸ç¶æä¸çé¢è²ï¼å¦è®¾ç½®æ¤å¼ï¼å°ä¼è¦çparentçactiveColorå¼ |
| | | * @property {String} inactiveColor æªéä¸çé¢è² |
| | | * @property {String | Number} iconSize 徿 ç大å°ï¼åä½px |
| | | * @property {String} iconColor 徿 é¢è² |
| | | * @property {String | Number} label labelæç¤ºæåï¼å 为nvueä¸ï¼ç´æ¥slotè¿æ¥çæåï¼ç±äºç¹æ®çç»æï¼æ æ³ä¿®æ¹æ ·å¼ |
| | | * @property {String} labelColor labelçé¢è² |
| | | * @property {String | Number} labelSize labelçåä½å¤§å°ï¼pxåä½ |
| | | * @property {String | Boolean} labelDisabled æ¯å¦ç¦æ¢ç¹å»æç¤ºè¯éä¸å¤éæ¡ |
| | | * @property {Object} customStyle å®ä¹éè¦ç¨å°çå¤é¨æ ·å¼ |
| | | * |
| | | * @event {Function} change ä»»ä¸ä¸ªcheckboxç¶æåçååæ¶è§¦åï¼åè°ä¸ºä¸ä¸ªå¯¹è±¡ |
| | | * @example <u-checkbox v-model="checked" :disabled="false">天涯</u-checkbox> |
| | | */ |
| | | export default { |
| | | name: "u-checkbox", |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | data() { |
| | | return { |
| | | isChecked: false, |
| | | // ç¶ç»ä»¶çé»è®¤å¼ï¼å 为头æ¡å°ç¨åºä¸æ¯æå¨computedä¸ä½¿ç¨this.parent.shapeçå½¢å¼ |
| | | // æ
åªè½ä½¿ç¨å¦æ¤æ¹æ³ |
| | | parentData: { |
| | | iconSize: 12, |
| | | labelDisabled: null, |
| | | disabled: null, |
| | | shape: 'square', |
| | | activeColor: null, |
| | | inactiveColor: null, |
| | | size: 18, |
| | | value: null, |
| | | iconColor: null, |
| | | placement: 'row', |
| | | borderBottom: false, |
| | | iconPlacement: 'left' |
| | | } |
| | | } |
| | | }, |
| | | computed: { |
| | | // æ¯å¦ç¦ç¨ï¼å¦æç¶ç»ä»¶u-raios-groupç¦ç¨çè¯ï¼å°ä¼å¿½ç¥åç»ä»¶çé
ç½® |
| | | elDisabled() { |
| | | return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false; |
| | | }, |
| | | // æ¯å¦ç¦ç¨labelç¹å» |
| | | elLabelDisabled() { |
| | | return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : |
| | | false; |
| | | }, |
| | | // ç»ä»¶å°ºå¯¸ï¼å¯¹åºsizeçå¼ï¼é»è®¤å¼ä¸º21px |
| | | elSize() { |
| | | return this.size ? this.size : (this.parentData.size ? this.parentData.size : 21); |
| | | }, |
| | | // ç»ä»¶çå¾é徿 ç尺寸ï¼é»è®¤12px |
| | | elIconSize() { |
| | | return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 12); |
| | | }, |
| | | // ç»ä»¶é䏿¿æ´»æ¶çé¢è² |
| | | elActiveColor() { |
| | | return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : '#2979ff'); |
| | | }, |
| | | // ç»ä»¶éæªä¸æ¿æ´»æ¶çé¢è² |
| | | elInactiveColor() { |
| | | return this.inactiveColor ? this.inactiveColor : (this.parentData.inactiveColor ? this.parentData.inactiveColor : |
| | | '#c8c9cc'); |
| | | }, |
| | | // labelçé¢è² |
| | | elLabelColor() { |
| | | return this.labelColor ? this.labelColor : (this.parentData.labelColor ? this.parentData.labelColor : '#606266') |
| | | }, |
| | | // ç»ä»¶çå½¢ç¶ |
| | | elShape() { |
| | | return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle'); |
| | | }, |
| | | // labelå¤§å° |
| | | elLabelSize() { |
| | | return uni.$u.addUnit(this.labelSize ? this.labelSize : (this.parentData.labelSize ? this.parentData.labelSize : |
| | | '15')) |
| | | }, |
| | | elIconColor() { |
| | | const iconColor = this.iconColor ? this.iconColor : (this.parentData.iconColor ? this.parentData.iconColor : |
| | | '#ffffff'); |
| | | // 徿 çé¢è² |
| | | if (this.elDisabled) { |
| | | // disabledç¶æä¸ï¼å·²å¾éçcheckbox徿 æ¹ä¸ºelInactiveColor |
| | | return this.isChecked ? this.elInactiveColor : 'transparent' |
| | | } else { |
| | | return this.isChecked ? iconColor : 'transparent' |
| | | } |
| | | }, |
| | | iconClasses() { |
| | | let classes = [] |
| | | // ç»ä»¶çå½¢ç¶ |
| | | classes.push('u-checkbox__icon-wrap--' + this.elShape) |
| | | if (this.elDisabled) { |
| | | classes.push('u-checkbox__icon-wrap--disabled') |
| | | } |
| | | if (this.isChecked && this.elDisabled) { |
| | | classes.push('u-checkbox__icon-wrap--disabled--checked') |
| | | } |
| | | // æ¯ä»å®ï¼å¤´æ¡å°ç¨åºæ æ³å¨æç»å®ä¸ä¸ªæ°ç»ç±»åï¼å¦åè§£æåºæ¥çç»æä¼å¸¦æ","ï¼è导è´å¤±æ |
| | | // #ifdef MP-ALIPAY || MP-TOUTIAO |
| | | classes = classes.join(' ') |
| | | // #endif |
| | | return classes |
| | | }, |
| | | iconWrapStyle() { |
| | | // checkboxçæ´ä½æ ·å¼ |
| | | const style = {} |
| | | style.backgroundColor = this.isChecked && !this.elDisabled ? this.elActiveColor : '#ffffff' |
| | | style.borderColor = this.isChecked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor |
| | | style.width = uni.$u.addUnit(this.elSize) |
| | | style.height = uni.$u.addUnit(this.elSize) |
| | | // 妿æ¯å¾æ å¨å³è¾¹çè¯ï¼ç§»é¤å®çå³è¾¹è· |
| | | if (this.parentData.iconPlacement === 'right') { |
| | | style.marginRight = 0 |
| | | } |
| | | return style |
| | | }, |
| | | checkboxStyle() { |
| | | const style = {} |
| | | if (this.parentData.borderBottom && this.parentData.placement === 'row') { |
| | | uni.$u.error('æ£æµå°æ¨å°borderBottom设置为trueï¼éè¦åæ¶å°u-checkbox-groupçplacement设置为columnæææ') |
| | | } |
| | | // å½ç¶ç»ä»¶è®¾ç½®äºæ¾ç¤ºä¸è¾¹æ¡å¹¶ä¸æåå½¢å¼ä¸ºçºµåæ¶ï¼ç»å
容åè¾¹æ¡ä¹é´å ä¸ä¸å®é´é |
| | | if (this.parentData.borderBottom && this.parentData.placement === 'column') { |
| | | style.paddingBottom = '8px' |
| | | } |
| | | return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)) |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.init() |
| | | }, |
| | | methods: { |
| | | init() { |
| | | // æ¯ä»å®å°ç¨åºä¸æ¯æprovide/injectï¼æä»¥ä½¿ç¨è¿ä¸ªæ¹æ³è·åæ´ä¸ªç¶ç»ä»¶ï¼å¨createdå®ä¹ï¼é¿å
循ç¯å¼ç¨ |
| | | this.updateParentData() |
| | | if (!this.parent) { |
| | | uni.$u.error('u-checkboxå¿
é¡»æé
u-checkbox-groupç»ä»¶ä½¿ç¨') |
| | | } |
| | | // 设置åå§åæ¶ï¼æ¯å¦é»è®¤éä¸çç¶æï¼ç¶ç»ä»¶u-checkbox-groupçvalueå¯è½æ¯arrayï¼æä»¥é¢å¤å¤æ |
| | | if (this.checked) { |
| | | this.isChecked = true |
| | | } else if (uni.$u.test.array(this.parentData.value)) { |
| | | // æ¥æ¾æ°ç»æ¯æ¯å¦åå¨this.nameå
ç´ å¼ |
| | | this.isChecked = this.parentData.value.some(item => { |
| | | return item === this.name |
| | | }) |
| | | } |
| | | }, |
| | | updateParentData() { |
| | | this.getParentData('u-checkbox-group') |
| | | }, |
| | | // 横å两端æåæ¶ï¼ç¹å»ç»ä»¶å³å¯è§¦åéä¸äºä»¶ |
| | | wrapperClickHandler(e) { |
| | | this.parentData.iconPlacement === 'right' && this.iconClickHandler(e) |
| | | }, |
| | | // ç¹å»å¾æ |
| | | iconClickHandler(e) { |
| | | this.preventEvent(e) |
| | | // 妿æ´ä½è¢«ç¦ç¨ï¼ä¸å
许被ç¹å» |
| | | if (!this.elDisabled) { |
| | | this.setRadioCheckedStatus() |
| | | } |
| | | }, |
| | | // ç¹å»label |
| | | labelClickHandler(e) { |
| | | this.preventEvent(e) |
| | | // 妿æé®æ´ä½è¢«ç¦ç¨æè
label被ç¦ç¨ï¼åä¸å
许ç¹å»æåä¿®æ¹ç¶æ |
| | | if (!this.elLabelDisabled && !this.elDisabled) { |
| | | this.setRadioCheckedStatus() |
| | | } |
| | | }, |
| | | emitEvent() { |
| | | this.$emit('change', this.isChecked) |
| | | // å°è¯è°ç¨u-formçéªè¯æ¹æ³ï¼è¿è¡ä¸å®å»¶è¿ï¼å¦å微信å°ç¨åºæ´æ°å¯è½ä¼ä¸åæ¶ |
| | | this.$nextTick(() => { |
| | | uni.$u.formValidate(this, 'change') |
| | | }) |
| | | }, |
| | | // æ¹åç»ä»¶éä¸ç¶æ |
| | | // è¿éçæ¹åç便®æ¯ï¼æ´æ¹æ¬ç»ä»¶çcheckedå¼ä¸ºtrueï¼åæ¶éè¿ç¶ç»ä»¶éåææu-checkboxå®ä¾ |
| | | // å°æ¬ç»ä»¶å¤çå
¶ä»u-checkboxçcheckedé½è®¾ç½®ä¸ºfalse(é½è¢«åæ¶éä¸ç¶æ)ï¼å èåªå©ä¸ä¸ä¸ªä¸ºéä¸ç¶æ |
| | | setRadioCheckedStatus() { |
| | | // å°æ¬ç»ä»¶æ 记为ä¸åæ¥ç¸åçç¶æ |
| | | this.isChecked = !this.isChecked |
| | | this.emitEvent() |
| | | typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this) |
| | | } |
| | | }, |
| | | watch:{ |
| | | checked(){ |
| | | this.isChecked = this.checked |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | $u-checkbox-icon-wrap-margin-right:6px !default; |
| | | $u-checkbox-icon-wrap-font-size:6px !default; |
| | | $u-checkbox-icon-wrap-border-width:1px !default; |
| | | $u-checkbox-icon-wrap-border-color:#c8c9cc !default; |
| | | $u-checkbox-icon-wrap-icon-line-height:0 !default; |
| | | $u-checkbox-icon-wrap-circle-border-radius:100% !default; |
| | | $u-checkbox-icon-wrap-square-border-radius:3px !default; |
| | | $u-checkbox-icon-wrap-checked-color:#fff !default; |
| | | $u-checkbox-icon-wrap-checked-background-color:red !default; |
| | | $u-checkbox-icon-wrap-checked-border-color:#2979ff !default; |
| | | $u-checkbox-icon-wrap-disabled-background-color:#ebedf0 !default; |
| | | $u-checkbox-icon-wrap-disabled-checked-color:#c8c9cc !default; |
| | | $u-checkbox-label-margin-left:5px !default; |
| | | $u-checkbox-label-margin-right:12px !default; |
| | | $u-checkbox-label-color:$u-content-color !default; |
| | | $u-checkbox-label-font-size:15px !default; |
| | | $u-checkbox-label-disabled-color:#c8c9cc !default; |
| | | |
| | | .u-checkbox { |
| | | /* #ifndef APP-NVUE */ |
| | | @include flex(row); |
| | | /* #endif */ |
| | | overflow: hidden; |
| | | flex-direction: row; |
| | | align-items: center; |
| | | |
| | | &-label--left { |
| | | flex-direction: row |
| | | } |
| | | |
| | | &-label--right { |
| | | flex-direction: row-reverse; |
| | | justify-content: space-between |
| | | } |
| | | |
| | | &__icon-wrap { |
| | | /* #ifndef APP-NVUE */ |
| | | box-sizing: border-box; |
| | | // nvueä¸ï¼border-colorè¿æ¸¡æé®é¢ |
| | | transition-property: border-color, background-color, color; |
| | | transition-duration: 0.2s; |
| | | /* #endif */ |
| | | color: $u-content-color; |
| | | @include flex; |
| | | align-items: center; |
| | | justify-content: center; |
| | | color: transparent; |
| | | text-align: center; |
| | | margin-right: $u-checkbox-icon-wrap-margin-right; |
| | | |
| | | font-size: $u-checkbox-icon-wrap-font-size; |
| | | border-width: $u-checkbox-icon-wrap-border-width; |
| | | border-color: $u-checkbox-icon-wrap-border-color; |
| | | border-style: solid; |
| | | |
| | | /* #ifdef MP-TOUTIAO */ |
| | | // 头æ¡å°ç¨åºå
¼å®¹æ§é®é¢ï¼éè¦è®¾ç½®è¡é«ä¸º0ï¼å¦å徿 åä¸ |
| | | &__icon { |
| | | line-height: $u-checkbox-icon-wrap-icon-line-height; |
| | | } |
| | | |
| | | /* #endif */ |
| | | |
| | | &--circle { |
| | | border-radius: $u-checkbox-icon-wrap-circle-border-radius; |
| | | } |
| | | |
| | | &--square { |
| | | border-radius: $u-checkbox-icon-wrap-square-border-radius; |
| | | } |
| | | |
| | | &--checked { |
| | | color: $u-checkbox-icon-wrap-checked-color; |
| | | background-color: $u-checkbox-icon-wrap-checked-background-color; |
| | | border-color: $u-checkbox-icon-wrap-checked-border-color; |
| | | } |
| | | |
| | | &--disabled { |
| | | background-color: $u-checkbox-icon-wrap-disabled-background-color !important; |
| | | } |
| | | |
| | | &--disabled--checked { |
| | | color: $u-checkbox-icon-wrap-disabled-checked-color !important; |
| | | } |
| | | } |
| | | |
| | | &__label { |
| | | /* #ifndef APP-NVUE */ |
| | | word-wrap: break-word; |
| | | /* #endif */ |
| | | margin-left: $u-checkbox-label-margin-left; |
| | | margin-right: $u-checkbox-label-margin-right; |
| | | color: $u-checkbox-label-color; |
| | | font-size: $u-checkbox-label-font-size; |
| | | |
| | | &--disabled { |
| | | color: $u-checkbox-label-disabled-color; |
| | | } |
| | | } |
| | | } |
| | | </style> |