123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <template>
- <view class="u-checkbox" :style="[checkboxStyle]">
- <view class="u-checkbox__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
- <u-icon class="u-checkbox__icon-wrap__icon" name="checkbox-mark" :size="checkboxIconSize" :color="iconColor"/>
- </view>
- <view class="u-checkbox__label" @tap="onClickLabel" :style="{
- fontSize: $u.addUnit(labelSize)
- }">
- <slot />
- </view>
- </view>
- </template>
- <script>
-
- export default {
- name: "u-checkbox",
- props: {
-
- name: {
- type: [String, Number],
- default: ''
- },
-
- shape: {
- type: String,
- default: ''
- },
-
- value: {
- type: Boolean,
- default: false
- },
-
- disabled: {
- type: [String, Boolean],
- default: ''
- },
-
- labelDisabled: {
- type: [String, Boolean],
- default: ''
- },
-
- activeColor: {
- type: String,
- default: ''
- },
-
- iconSize: {
- type: [String, Number],
- default: ''
- },
-
- labelSize: {
- type: [String, Number],
- default: ''
- },
-
- size: {
- type: [String, Number],
- default: ''
- },
- },
- data() {
- return {
- parentDisabled: false,
- newParams: {},
- };
- },
- created() {
-
- this.parent = this.$u.$parent.call(this, 'u-checkbox-group');
-
- this.parent && this.parent.children.push(this);
- },
- computed: {
-
- isDisabled() {
- return this.disabled !== '' ? this.disabled : this.parent ? this.parent.disabled : false;
- },
-
- isLabelDisabled() {
- return this.labelDisabled !== '' ? this.labelDisabled : this.parent ? this.parent.labelDisabled : false;
- },
-
- checkboxSize() {
- return this.size ? this.size : (this.parent ? this.parent.size : 34);
- },
-
- checkboxIconSize() {
- return this.iconSize ? this.iconSize : (this.parent ? this.parent.iconSize : 20);
- },
-
- elActiveColor() {
- return this.activeColor ? this.activeColor : (this.parent ? this.parent.activeColor : 'primary');
- },
-
- elShape() {
- return this.shape ? this.shape : (this.parent ? this.parent.shape : 'square');
- },
- iconStyle() {
- let style = {};
-
- if (this.elActiveColor && this.value && !this.isDisabled) {
- style.borderColor = this.elActiveColor;
- style.backgroundColor = this.elActiveColor;
- }
- style.width = this.$u.addUnit(this.checkboxSize);
- style.height = this.$u.addUnit(this.checkboxSize);
- return style;
- },
-
- iconColor() {
- return this.value ? '#ffffff' : 'transparent';
- },
- iconClass() {
- let classes = [];
- classes.push('u-checkbox__icon-wrap--' + this.elShape);
- if (this.value == true) classes.push('u-checkbox__icon-wrap--checked');
- if (this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled');
- if (this.value && this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled--checked');
-
- return classes.join(' ');
- },
- checkboxStyle() {
- let style = {};
- if(this.parent && this.parent.width) {
- style.width = this.parent.width;
-
-
- style.float = 'left';
-
-
-
- style.flex = `0 0 ${this.parent.width}`;
-
- }
- if(this.parent && this.parent.wrap) {
- style.width = '100%';
-
-
- style.flex = '0 0 100%';
-
- }
- return style;
- }
- },
- methods: {
- onClickLabel() {
- if (!this.isLabelDisabled && !this.isDisabled) {
- this.setValue();
- }
- },
- toggle() {
- if (!this.isDisabled) {
- this.setValue();
- }
- },
- emitEvent() {
- this.$emit('change', {
- value: !this.value,
- name: this.name
- })
-
-
- setTimeout(() => {
- if(this.parent && this.parent.emitEvent) this.parent.emitEvent();
- }, 80);
- },
-
- setValue() {
-
- let checkedNum = 0;
- if(this.parent && this.parent.children) {
-
- this.parent.children.map(val => {
- if (val.value) checkedNum++;
- })
- }
-
- if (this.value == true) {
- this.emitEvent();
- this.$emit('input', !this.value);
- } else {
-
- if(this.parent && checkedNum >= this.parent.max) {
- return this.$u.toast(`最多可选${this.parent.max}项`);
- }
-
- this.emitEvent();
- this.$emit('input', !this.value);
- }
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/style.components.scss";
- .u-checkbox {
- /* #ifndef APP-NVUE */
- display: inline-flex;
- /* #endif */
- align-items: center;
- overflow: hidden;
- user-select: none;
- line-height: 1.8;
-
- &__icon-wrap {
- color: $u-content-color;
- flex: none;
- display: -webkit-flex;
- @include vue-flex;
- align-items: center;
- justify-content: center;
- box-sizing: border-box;
- width: 42rpx;
- height: 42rpx;
- color: transparent;
- text-align: center;
- transition-property: color, border-color, background-color;
- font-size: 20px;
- border: 1px solid #c8c9cc;
- transition-duration: 0.2s;
-
- /* #ifdef MP-TOUTIAO */
- // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
- &__icon {
- line-height: 0;
- }
- /* #endif */
-
- &--circle {
- border-radius: 100%;
- }
-
- &--square {
- border-radius: 6rpx;
- }
-
- &--checked {
- color: #fff;
- background-color: $u-type-primary;
- border-color: $u-type-primary;
- }
-
- &--disabled {
- background-color: #ebedf0;
- border-color: #c8c9cc;
- }
-
- &--disabled--checked {
- color: #c8c9cc !important;
- }
- }
-
- &__label {
- word-wrap: break-word;
- margin-left: 10rpx;
- margin-right: 24rpx;
- color: $u-content-color;
- font-size: 30rpx;
-
- &--disabled {
- color: #c8c9cc;
- }
- }
- }
- </style>
|