123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <template>
- <view class="u-checkbox" :style="[checkboxStyle]">
- <view class="u-checkbox__icon-wrap" @tap="toggle">
- <u-icon :class="iconClass" name="checkbox-mark" :size="iconSize" :color="iconColor" class="u-checkbox__icon" :style="[iconStyle]" />
- </view>
- <view class="u-label-class u-checkbox__label" @tap="onClickLabel" :style="{
- fontSize: labelSize + 'rpx'
- }">
- <slot />
- </view>
- </view>
- </template>
- <script>
-
- export default {
- name: "u-checkbox",
- props: {
-
- name: {
- type: [String, Number],
- default: ''
- },
-
- shape: {
- type: String,
- default: 'square'
- },
-
- value: {
- type: Boolean,
- default: false
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
-
- labelDisabled: {
- type: Boolean,
- default: false
- },
-
- activeColor: {
- type: String,
- default: ''
- },
-
- iconSize: {
- type: [String, Number],
- default: 20
- },
-
- labelSize: {
- type: [String, Number],
- default: 28
- },
-
- size: {
- type: [String, Number],
- default: 34
- },
- },
- inject: {
- checkboxGroup: {
-
- default() {
- return {
- disabled: false,
- children: [],
- size: 34,
- activeColor: '#2979ff',
- max: 999999,
- emitEvent: () => {},
- width: '',
- wrap: false
- }
- }
- }
- },
- data() {
- return {
- parentDisabled: false,
- };
- },
- created() {
- this.parentDisabled = this.checkboxGroup.disabled;
- this.checkboxGroup.children.push(this);
- },
- computed: {
- iconStyle() {
- let style = {};
- if (this.checkboxActiveColor && this.value && !this.disabled && !this.parentDisabled) {
- style.borderColor = this.checkboxActiveColor;
- style.backgroundColor = this.checkboxActiveColor;
- }
- style.width = this.checkboxGroup.size + 'rpx';
- style.height = this.checkboxGroup.size + 'rpx';
- return style;
- },
- iconColor() {
- return this.value ? '#ffffff' : 'transparent';
- },
- iconClass() {
- let classs = [];
- classs.push('u-checkbox__icon--' + this.shape);
- if (this.value == true) classs.push('u-checkbox__icon--checked');
- if (this.disabled || this.parentDisabled) classs.push('u-checkbox__icon--disabled');
- if (this.value && (this.disabled || this.parentDisabled)) classs.push('u-checkbox__icon--disabled--checked');
- return classs;
- },
-
-
- checkboxActiveColor() {
- return this.activeColor ? this.activeColor : this.checkboxGroup.activeColor;
- },
- checkboxStyle() {
- let style = {};
- if(this.checkboxGroup.width) {
- style.width = this.checkboxGroup.width;
-
-
- style.float = 'left';
-
-
-
- style.flex = `0 0 ${this.checkboxGroup.width}`;
-
- }
- if(this.checkboxGroup.wrap) {
- style.width = '100%';
-
-
- style.flex = '0 0 100%';
-
- }
- return style;
- }
- },
- methods: {
- onClickLabel() {
- if (!this.disabled && !this.labelDisabled && !this.parentDisabled) {
- this.setValue();
- }
- },
- toggle() {
- if (!this.disabled && !this.parentDisabled) {
- this.setValue();
- }
- },
- emitEvent() {
- this.$emit('change', {
- value: this.value,
- name: this.name
- })
- this.checkboxGroup.emitEvent();
- },
-
- setValue() {
-
- let checkedNum = 0;
- this.checkboxGroup.children.map(val => {
- if (val.value) checkedNum++;
- })
-
- if (this.value == true) {
- this.$emit('input', !this.value);
-
- this.$nextTick(function() {
- this.emitEvent();
- })
- } else if (checkedNum < this.checkboxGroup.max && this.value == false) {
-
- this.$emit('input', !this.value);
-
- this.$nextTick(function() {
- this.emitEvent();
- })
- }
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .u-checkbox {
- display: -webkit-flex;
- display: flex;
- -webkit-align-items: center;
- align-items: center;
- overflow: hidden;
- -webkit-user-select: none;
- user-select: none;
- line-height: 1.8;
- }
-
- .u-checkbox__icon-wrap,
- .u-checkbox__label {
- color: $u-content-color;
- }
- .u-checkbox__icon-wrap {
- -webkit-flex: none;
- flex: none;
- }
- .u-checkbox__icon {
- display: -webkit-flex;
- display: flex;
- -webkit-align-items: center;
- align-items: center;
- -webkit-justify-content: 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;
- }
- .u-checkbox__icon--circle {
- border-radius: 100%;
- }
- .u-checkbox__icon--square {
- border-radius: 3px;
- }
- .u-checkbox__icon--checked {
- color: #fff;
- background-color: #2979ff;
- border-color: #2979ff;
- }
- .u-checkbox__icon--disabled {
- background-color: #ebedf0;
- border-color: #c8c9cc;
- }
- .u-checkbox__icon--disabled--checked {
- color: #c8c9cc !important;
- }
- .u-checkbox__label {
- word-wrap: break-word;
- margin-left: 10rpx;
- margin-right: 24rpx;
- color: $u-content-color;
- font-size: 30rpx;
- }
- .u-checkbox__label--disabled {
- color: #c8c9cc;
- }
- .u-checkbox__label:empty {
- margin: 0;
- }
- </style>
|