Select.vue
3.86 KB
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
<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>