All files / src/components/u-table-view.vue filters-popper.vue

86.95% Statements 20/23
75% Branches 9/12
57.14% Functions 4/7
86.36% Lines 19/22

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189      7x   7x     7x                                                                                                                                                                                                                                                                                                                                                                        
<template>
<m-popper :class="$style.root" ref="popper" :trigger="trigger" :placement="placement" :disabled="disabled" @click.stop @open="onOpen" v-if="!hidden">
    <f-scroll-view @click.stop :class="$style.scrollview" trigger="hover">
        <div :class="$style.wrap">
            <u-table-view-filters :value.sync="currentValue" @before-select="onBeforeSelect" :multiple="multiple" @select="onSelect">
                <u-table-view-filter v-for="(filter, index) in currentData"
                    :key="getItemValue(filter)"
                    :value="getItemValue(filter)"
                    :text="getItemText(filter)"
                    :disabled="filter.disabled"
                    :item="filter" :index="index">
                    <slot name="item" :item="filter" :index="index">{{ getItemText(filter) }}</slot>
                </u-table-view-filter>
            </u-table-view-filters>
        </div>
    </f-scroll-view>
    <u-linear-layout justify="space-between" :class="$style.footer" v-if="multiple && showFooter">
        <slot name="footer">
            <u-link @click="reset">重置</u-link>
            <u-link @click="confirm">确定</u-link>
        </slot>
    </u-linear-layout>
</m-popper>
</template>
 
<script>
 
import isObject from 'lodash/isObject';
 
export default {
    name: 'u-table-view-filters-popper',
    props: {
        value: [Array, String, Number],
        data: Array,
        multiple: { type: Boolean, default: false },
        disabled: { type: Boolean, default: false },
        max: { type: Number, default: Infinity },
        showFooter: { type: Boolean, default: true },
        placement: { type: String, default: 'bottom-start' },
        trigger: { type: String, default: 'click.stop' },
        valueField: { type: String, default: 'value' },
        textField: { type: String, default: 'text' },
        hidden: { type: Boolean, default: false },
    },
    data() {
        return {
            currentValue: this.value,
            currentData: this.data,
        };
    },
    watch: {
        value(value) {
            this.currentValue = value;
        },
        currentValue(value, oldValue) {
            this.handleData();
            this.$emit('change', { value: this.currentValue, oldValue });
        },
        data(value) {
            this.currentData = value;
            this.handleData();
            this.$emit('load', this.currentData);
        },
    },
    created() {
        this.transfomValue(); // 老的value是string格式,兼容
        this.handleData();
    },
    methods: {
        open() {
            this.$refs.popper && this.$refs.popper.open();
        },
        close() {
            this.$refs.popper && this.$refs.popper.close();
        },
        toggle(opened) {
            this.$refs.popper && this.$refs.popper.toggle(opened);
        },
        reset() {
            this.currentValue = [];
        },
        confirm() {
            this.close();
            let value;
            if (this.multiple) {
                if (this.currentValue && this.currentValue.length) {
                    value = this.currentValue;
                }
            } else {
                value = this.currentValue;
            }
            const data = Object.assign({}, this.selectedData);
            data.value = value;
            this.$emit('select', data);
        },
        onSelect($event) {
            if (!this.multiple) {
                this.$emit('select', $event);
                this.close();
            } else if (!this.showFooter) {
                this.$emit('select', $event);
            }
            this.selectedData = $event;
        },
        onBeforeSelect(event) {
            if (this.multiple) {
                if (event.selected && this.exceedMax()) {
                    event.preventDefault();
                }
            }
        },
        handleData() {
            if (this.multiple) {
                const exceedMax = this.exceedMax();
                this.currentData.forEach((item) => {
                    if (isObject(item))
                        item.disabled = exceedMax && !this.currentValue.includes(item.value);
                });
            }
        },
        onOpen() {
            this.transfomValue();
            this.handleData();
            this.$emit('open');
        },
        transfomValue() {
            if (!this.multiple)
                return;
            if (this.value !== undefined) {
                this.currentValue = !Array.isArray(this.value) ? [this.value] : this.value;
            }
        },
        exceedMax() {
            return Array.isArray(this.currentValue) && this.currentValue.length >= this.max;
        },
        cancel() {
            this.close();
        },
        getItemValue(item) {
            if (isObject(item)) {
                return this.$at2(item, this.valueField) || item.value;
            } else {
                return item;
            }
        },
        getItemText(item) {
            if (isObject(item)) {
                return this.$at2(item, this.textField) || item.text;
            } else {
                return item;
            }
        },
    },
};
 
</script>
 
<style module>
.root {
    position: absolute;
    line-height: var(--table-view-filter-line-height);
    /* max-height: var(--table-view-filter-max-height); */
    /* overflow: auto; */
    background: var(--table-view-filter-background);
    border: 1px solid var(--border-color-base);
    border-radius: var(--border-radius-base);
    z-index: var(--z-index-popper);
    max-width: 300px;
}
.wrap {
    min-width: 100%;
    max-height: var(--table-view-filter-max-height);
}
.footer {
    padding: 0 12px;
    height: 36px;
    line-height: 36px;
    border-top: 1px solid var(--table-view-filter-border-color);
}
.scrollview {
    padding: 8px 0;
}
/* 作用:出滚动条时hover背景能展示全 */
.scrollview [class^="f-scroll-view_wrap__"] > div {
    display: inline-flex;
    min-width: 100%
}
</style>