All files / src/components/u-checkbox-card.vue index.vue

59.09% Statements 13/22
20% Branches 2/10
16.66% Functions 1/6
61.9% Lines 13/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141      7x   7x     7x                                                                                                                                                                                                                                                                 7x 7x    
<template>
<div :class="$style.root" :size="size">
    <div :class="$style.head">
        <u-checkbox :disabled="data.length===0" v-model="allChecked">全选</u-checkbox>
    </div>
    <div :class="$style.body">
        <u-checkboxes v-if="data.length > 0" v-model="checkedList">
            <div v-for="(item, index) in data" :style="{width: column ? `${columnWidth}%`:undefined}" :class="$style.item">
                <u-checkbox :label="item.value" :disabled="item.disabled" :title="item.text">
                    <slot :data="item" :index="index">
                        {{ item.text }}
                    </slot>
                </u-checkbox>
            </div>
        </u-checkboxes>
        <div :class="$style.placeholder" v-else>
            <slot name="placeholder">
                {{ placeholder }}
            </slot>
        </div>
    </div>
</div>
</template>
 
<script>
// import i18n from '@/utils/i18n';
import MField from '../m-field.vue';
import MConverter from '../m-converter.vue';
 
export default {
    name: 'u-checkbox-card',
    mixins: [MField, MConverter],
    props: {
        data: { type: Array, default: () => [] },
        value: { type: [Array, String], default: () => [] },
        placeholder: { type: String, default: '暂无数据' },
        size: { type: String, default: 'normal' },
        column: Number,
    },
    data() {
        return { checkedList: this.value };
    },
    computed: {
        columnWidth() {
            if (!this.column)
                return 0;
            return 100 / this.column;
        },
        allChecked: {
            set(checked) {
                if (checked) {
                    this.checkedList = this.data.map((item) => item.value);
                } else {
                    this.checkedList = [];
                }
            },
            get() {
                if (this.data.length === 0) {
                    return false;
                }
                if (this.checkedList.length === this.data.length)
                    return true;
                else if (this.checkedList.length === 0)
                    return false;
                else
                    return null;
            },
        },
    },
    watch: {
        value(value) {
            if (this.converter)
                value = this.currentConverter.set(value);
            this.checkedList = value;
        },
        checkedList(value, oldValue) {
            if (this.converter) {
                value = this.currentConverter.get(value);
                oldValue = this.currentConverter.get(oldValue);
            }
            this.$emit('update:value', value);
            this.$emit('change', { value, oldValue }, this);
            this.$emit('input', value, this);
        },
    },
    methods: {
        getCheckList() {
            return [...this.checkedList];
        },
    },
};
</script>
 
<style module>
.root {
    max-width: 100%;
    width: 515px;
    max-height: 360px;
    border: 1px solid var(--border-color-base);
}
.root[size="small"] {
    width: 180px;
}
.root[size="large"] {
    width: 654px;
}
.root[size="huge"] {
    width: 812px;
}
.root[size="auto"] {
    width: 100%;
}
.head {
    padding: 5px 10px;
    height: 41px;
    line-height: 30px;
    border-bottom: 1px solid var(--border-color-base);
    background-color: #eef2f5;
}
.body {
    max-height: 318px;
    overflow: auto;
    padding: 10px;
    padding-bottom: 5px;
}
.item {
    display: inline-block;
    width: 158px;
    padding-right: 20px;
    margin-right: 0 !important;
}
.placeholder {
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 0;
    margin-bottom: 5px;
    color: rgb(153, 153, 153);
}
</style>