RequestSelect.vue 1.06 KB
Newer Older
陈浩玮's avatar
yonghu  
陈浩玮 committed
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
<template>
    <a-select
        :value="value"
        style="width: 100%"
        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
        placeholder="Please select"
        allow-clear
        :options="data"
        @change="onChange"
        v-bind="$attrs"
    />
</template>

<script>
import { formatObj } from '@/utils';

export default {
    model: {
        prop: 'value',
        event: 'change',
    },
    props: {
        value: [String, Number, Array],
        request: Function,
        formatData: { type: Object, default: undefined },
        getName: Function,
    },
    data() {
        return {
            data: [],
        };
    },
    async mounted() {
        if (this.request) {
            const newArr = await this.request();
            this.data = this.formatData ? await formatObj(newArr, this.formatData) : newArr;
        }
    },
    methods: {
        onChange(val, opt) {
            this.$emit('change', val);
            this.getName && this.getName(opt?.componentOptions?.children[0]?.text || '');
        },
    },
};
</script>