select.vue
4.16 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
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
<template>
<v-dropdown class="v-select" v-model="internalOptions"
is-select
:size="size"
:style="{width:width}"
:width="width"
:maxWidth="maxWidth"
:isMultiple="isMultiple"
:textAlign="textAlign"
:min="min"
:max="max"
:isInput="isInput"
@change="dropdownChange"
>
<span>
<template v-if="isInput">
<input class="v-select-input" :placeholder="placeholder" type="text" v-model="inputValue"/>
</template>
<template v-else>
<span class="v-select-selected-span">{{showSelectInfo()}}</span>
</template>
<i class="v-select-selected-i v-icon-down-dir"></i>
</span>
</v-dropdown>
</template>
<script>
import utils from '../../src/utils/utils.js'
import settings from '../../src/settings/settings.js'
import layerAdjustment from '../../src/mixins/layerAdjustment.js'
import VDropdown from '../../v-dropdown/index'
export default {
name: 'v-select',
components: {
VDropdown
},
mixins: [layerAdjustment],
data(){
return {
visible: false,
internalOptions: [],
// 样式前缀
textAlignPrefix: 'v-select-items-li-a-',
inputValue: ''
}
},
props: {
size: {
type: String
},
width: {
type: Number,
default: 90
},
// select的最大宽度(超出隐藏)
maxWidth: {
type: Number
},
// 如果为true 会包含 checkbox
isMultiple: {
type: Boolean,
default: false
},
// 用户传入v-model 的值 [{value/label/selected}]
value: [Object, Array],
// 占位符
placeholder: {
type: String,
default: '请选择',
validator: function (value) {
return value.length > 0
}
},
// 文本居中方式 left|center|right
textAlign: {
type: String,
default: 'left'
},
// 最小选中数量
min: {
type: Number,
default: 0
},
// 最大选中数量
max: {
type: Number,
default: 999
},
// 是否支持输入input
isInput: {
type: Boolean,
default: false
}
},
methods: {
// 初始化
init(){
this.internalOptions = Object.assign([], this.value);
if (this.isInput) {
this.setInputValue();
}
},
// 显示选中的信息
showSelectInfo(){
var result, labels;
labels = this.selectedLabels();
if (Array.isArray(labels) && labels.length > 0) {
result = labels.join();
} else {
result = this.placeholder;
}
return result;
},
// 当前选中项的label
selectedLabels(){
return this.internalOptions.filter(x => x.selected).map(x => {
if (x.selected) {
return x.label;
}
});
},
// dropdown change event
dropdownChange(){
// 使用户传入的v-model 生效
this.$emit('input', this.internalOptions);
this.$emit('change');
}
},
created(){
this.init();
},
watch: {
'value': function (val) {
this.init();
}
}
}
</script>