dropdown.vue
10.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<template>
<dl :class="['v-dropdown',sizeClass]" v-click-outside="clickOutside">
<dt class="v-dropdown-dt">
<a :class="[isSelect ? 'v-dropdown-selected' :'']" @click.stop.prevent="toggleItems()" :style="{'width':width+'px'}">
<slot></slot>
</a>
</dt>
<dd v-show="visible" class="v-dropdown-dd">
<ul class="v-dropdown-items" :style="{'min-width':width+'px','max-width':getMaxWidth+'px'}">
<template v-if="isMultiple">
<v-checkbox-group is-vertical-show
:min="min"
:max="max"
@change="checkboxGroupChange"
v-model="checkboxGroupList"
>
<li v-for="item in internalOptions"
:class="['v-dropdown-items-multiple',getTextAlignClass()]"
>
<v-checkbox :key="item.label" :label="item.label"
:showLine="item.showLine"></v-checkbox>
</li>
</v-checkbox-group>
</template>
<template v-else>
<li v-for="item in internalOptions" @click.stop="selectOptionClick(item)"
:class="['v-dropdown-items-li',item.selected ? 'active' : '']">
<a :class="['v-dropdown-items-li-a',getTextAlignClass()]" href="javascript:void(0);">{{item.label}}</a>
</li>
</template>
<li v-if="showOperation" class="v-dropdown-operation">
<a @click.stop="confirm" class="v-dropdown-operation-item" href="javascript:void(0)">确认</a>
<a @click.stop="rest" class="v-dropdown-operation-item" href="javascript:void(0)">重置</a>
</li>
</ul>
</dd>
</dl>
</template>
<script>
import settings from '../../src/settings/settings.js'
import clickoutside from '../../src/directives/clickoutside.js'
import VCheckboxGroup from '../../v-checkbox-group/index'
import VCheckbox from '../../v-checkbox/index'
import layerAdjustment from '../../src/mixins/layerAdjustment.js'
export default {
name: 'v-dropdown',
components: {
VCheckboxGroup, VCheckbox
},
mixins: [layerAdjustment],
directives: {
'click-outside': clickoutside
},
data(){
return {
visible: false,
internalOptions: [],
// checkboxGroup 选中的项
checkboxGroupList: [],
// 样式前缀
textAlignPrefix: 'v-dropdown-items-li-a-',
inputValue: '',
// 是否有选项被改变(初始值为null 为了区分首次internalOptions 赋值的问题)
isOperationChange:null
}
},
props: {
// 如果是select 组件将特殊处理
isSelect:{
type:Boolean,
default:false
},
showOperation:{
type:Boolean,
default:false
},
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
}
},
computed: {
sizeClass(){
let size = settings.sizeMaps[this.size] || settings.sizeMapDefault
return size === settings.sizeMaps['large'] ? ' v-dropdown--large' : (size === settings.sizeMaps['middle'] ? ' v-dropdown--middle' : ' v-dropdown--small')
},
// 获取最大宽度(不设置则是无穷大)
getMaxWidth(){
var result = Infinity,
maxWidth = this.maxWidth,
width = this.width;
if (maxWidth && maxWidth > 0 && maxWidth > width) {
result = maxWidth;
}
return result;
}
},
methods: {
// 初始化
init(){
this.internalOptions = Object.assign([], this.value);
this.checkboxGroupList = this.selectedLabels();
if (this.isInput) {
this.setInputValue();
}
},
// operation filter confirm
confirm(){
if (this.isOperationChange){
this.$emit('on-filter-method',this.internalOptions);
this.isOperationChange = false;
}
this.hideDropDown();
},
// operation filter reset
rest(){
if (this.internalOptions.some(x=>x.selected)){
this.internalOptions.map(x=>{
if (x.selected){
x.selected = false;
}
return x;
})
this.checkboxGroupList = [];
// 使用户传入的v-model 生效
this.$emit('input', this.internalOptions);
this.$emit('change');
// 修复执行两次的bug
/*this.$emit('on-filter-method',this.internalOptions);
this.isOperationChange = false;*/
}
setTimeout(x=>{
this.hideDropDown();
},50)
},
hideDropDown(){
if (this.showOperation && this.isOperationChange){
this.$emit('on-filter-method',this.internalOptions);
this.isOperationChange = false;
}
this.visible = false;
},
showDropDown(){
this.visible = true;
},
// 设置文本框的值
setInputValue(){
var result, labels;
labels = this.selectedLabels();
if (Array.isArray(labels) && labels.length > 0) {
result = labels.join();
}
this.inputValue = result;
},
// checkbox 选中改变事件
checkboxGroupChange(){
this.selectOptionClick();
},
toggleItems(){
//this.visible = !this.visible;
if (this.visible) {
this.hideDropDown();
}else{
this.showDropDown();
this.$nextTick(x => {
this.dropDownClick()
})
}
},
selectOptionClick(item){
if (!this.isMultiple) {
this.internalOptions.map((x) => {
if (item.label === x.label) {
x.selected = true;
} else {
x.selected = false;
}
return x;
})
} else { // 多选
this.internalOptions.map((x) => {
if (this.checkboxGroupList.includes(x.label)) {
x.selected = true;
} else {
x.selected = false;
}
return x;
})
}
if (!this.isMultiple) {
this.toggleItems();
}
if (this.isInput) {
this.setInputValue();
}
// 使用户传入的v-model 生效
this.$emit('input', this.internalOptions);
this.$emit('change');
},
// 获取样式名称
getTextAlignClass(){
return this.textAlignPrefix + this.textAlign;
},
// 当前选中项的label
selectedLabels(){
return this.internalOptions.filter(x => x.selected).map(x => {
if (x.selected) {
return x.label;
}
});
},
clickOutside(){
this.hideDropDown();
//this.visible = false
},
// 下拉点击显示
dropDownClick(){
var dtEle = this.$el.querySelector('.v-dropdown-dt'),
ddItem = this.$el.querySelector('.v-dropdown-items');
this.layerAdjustmentOnce(ddItem, dtEle, 2);
return false;
},
// 确定下拉框的位置
dropdownAdjust(){
var dtEle = this.$el.querySelector('.v-dropdown-dt'),
ddItem = this.$el.querySelector('.v-dropdown-items');
this.layerAdjustmentBind(ddItem, dtEle, 2);
}
},
created(){
this.init();
},
mounted(){
this.dropdownAdjust();
},
watch: {
'value': function (val) {
this.init();
},
'internalOptions':function (val) {
this.isOperationChange = (this.showOperation && this.isOperationChange !== null) ? true :false;
}
}
}
</script>