Select.vue 3.86 KB
<template>
    <a-select :placeholder="placeholder" :labelInValue="true" :mode="mode" :show-search="showSearch" :option-filter-prop="optionFilterProp_" @change="this.onChange" :size="size"
              v-model:value="value_" :allow-clear="allowClear" :options="options" :field-names="fieldNames" :bordered="bordered" :show-arrow="showArrow" :dropdown-match-select-width="dropdownMatchSelectWidth">
    </a-select>
</template>

<script lang="ts">
import {Component, Emit, Model, Prop, Vue, Watch} from "vue-facing-decorator";
import {Option} from "ant-design-vue/es/vc-util/Children/toArray";

@Component({
    name: "MSelect",
    components: {}, emits: ['change']
})
export default class Select extends Vue {

    @Model({required: true, name: "value"})
    value: any;

    value_: any = null;

    @Prop({required: false, type: String})
    mode: 'multiple' | 'combobox';

    @Prop({required: false, type: String})
    placeholder: string;

    @Prop({required: false, type: Boolean, default: true})
    showSearch: boolean;

    @Prop({required: false, type: Boolean, default: false})
    allowClear: boolean;

    @Prop({required: false, type: Boolean, default: true})
    bordered: boolean;

    @Prop({required: false, type: Boolean, default: true})
    showArrow: boolean;

    @Prop({required: false, default: true})
    dropdownMatchSelectWidth: number | boolean;

    @Prop({required: true, type: Array})
    options: [];

    @Prop({required: false, type: String})
    optionFilterProp: string;

    @Prop({required: false, type: String, default: 'default'})
    size: 'large' | 'default' | 'small';

    optionFilterProp_: string;

    @Prop({required: false, type: Object})
    fieldNames: any;

    created(): void {
        if (this.optionFilterProp) {
            this.optionFilterProp_ = this.optionFilterProp;
        } else if (this.fieldNames) {
            this.optionFilterProp_ = this.fieldNames.label;
        }
    }

    onChange(value, option: Option | Array<Option>): void {
        if (value) {
            if (this.fieldNames) {
                if (this.mode === 'multiple') {
                    const newArray= value.map(item=>{
                        const newObj = {};
                        newObj[this.fieldNames.label] = item.label;
                        newObj[this.fieldNames.value] = item.value;
                        return newObj;
                    });
                    this.$emit('update:value', newArray);
                }else{
                    const newObj = {};
                    newObj[this.fieldNames.label] = value.label;
                    newObj[this.fieldNames.value] = value.value;
                    this.$emit('update:value', newObj);
                }
            } else {
                this.$emit('update:value', value);
            }
        } else {
            this.$emit('update:value', null);
        }
        this.$emit('change', value, option);
    }

    /**
     * 给下拉控件赋值
     * @param newValue
     * @param oldValue
     */
    @Watch('value', {deep: true, immediate: true})
    watchValue(newValue: any, oldValue: any): void {
        if (newValue) {
            if (this.fieldNames) {
                const valueName=this.fieldNames.value;
                if (this.mode === 'multiple') {
                    this.value_ = newValue.map(item => {
                        return {key: item[valueName], value: item[valueName]};
                    });
                } else {
                    //如果值是一样的,不需要再进行赋值操作
                    if (this.value_ != null && this.value_.value === newValue[valueName]) {
                        return;
                    }
                }
                this.value_ = {key: newValue[valueName], value: newValue[valueName]};
            } else {
                this.value_ = this.value;
            }
        }
    }

}
</script>

<style lang="less" scoped>

</style>