e355dd49
梁灏
add Select Component
|
1
|
<template>
|
2fbe636b
梁灏
Select support a ...
|
2
|
<div
|
2fbe636b
梁灏
Select support a ...
|
3
|
:class="classes"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
4
|
v-click-outside.capture="onClickOutside"
|
4a9974f6
Graham Fairweather
Normalise v-ckick...
|
5
|
v-click-outside:mousedown.capture="onClickOutside"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
6
|
>
|
e355dd49
梁灏
add Select Component
|
7
|
<div
|
4aec6a66
梁灏
support Select
|
8
|
ref="reference"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
9
10
11
12
13
14
15
16
17
18
|
:class="selectionCls"
:tabindex="selectTabindex"
@blur="toggleHeaderFocus"
@focus="toggleHeaderFocus"
@click="toggleMenu"
@keydown.esc="handleKeydown"
@keydown.enter="handleKeydown"
|
66f807ed
梁灏
update Select
|
19
20
|
@keydown.up.prevent="handleKeydown"
@keydown.down.prevent="handleKeydown"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
21
22
23
24
25
26
27
28
|
@keydown.tab="handleKeydown"
@keydown.delete="handleKeydown"
@mouseenter="hasMouseHoverHead = true"
@mouseleave="hasMouseHoverHead = false"
>
|
fed3e09d
梁灏
add AutoComplete ...
|
29
|
<slot name="input">
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
30
31
32
33
34
35
|
<input type="hidden" :name="name" :value="publicValue">
<select-head
:filterable="filterable"
:multiple="multiple"
:values="values"
:clearable="canBeCleared"
|
fed3e09d
梁灏
add AutoComplete ...
|
36
|
:disabled="disabled"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
37
38
39
40
41
42
43
44
45
|
:remote="remote"
:input-element-id="elementId"
:initial-label="initialLabel"
:placeholder="placeholder"
:query-prop="query"
@on-query-change="onQueryChange"
@on-input-focus="isFocused = true"
@on-input-blur="isFocused = false"
|
7dbde804
Sergio Crisostomo
add on-clear event
|
46
|
@on-clear="clearSingleSelect"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
47
|
/>
|
fed3e09d
梁灏
add AutoComplete ...
|
48
|
</slot>
|
e355dd49
梁灏
add Select Component
|
49
|
</div>
|
e09b07b7
huanghong
解决drop弹出动画异常
|
50
|
<transition name="transition-drop">
|
595cfa72
梁灏
fixed #1187 #844 ...
|
51
|
<Drop
|
ecaf8d51
梁灏
Date add transfer...
|
52
|
:class="dropdownCls"
|
595cfa72
梁灏
fixed #1187 #844 ...
|
53
54
55
56
|
v-show="dropVisible"
:placement="placement"
ref="dropdown"
:data-transfer="transfer"
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
57
58
59
60
|
v-transfer-dom
>
<ul v-show="showNotFoundLabel" :class="[prefixCls + '-not-found']"><li>{{ localeNotFoundText }}</li></ul>
<ul :class="prefixCls + '-dropdown-list'">
|
220161f5
Sergio Crisostomo
Clean up empty/nu...
|
61
62
63
64
65
66
|
<functional-options
v-if="(!remote) || (remote && !loading)"
:options="selectOptions"
:slot-update-hook="updateSlotOptions"
:slot-options="slotOptions"
></functional-options>
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
67
|
</ul>
|
01b54e30
梁灏
Select support re...
|
68
|
<ul v-show="loading" :class="[prefixCls + '-loading']">{{ localeLoadingText }}</ul>
|
4aec6a66
梁灏
support Select
|
69
70
|
</Drop>
</transition>
|
e355dd49
梁灏
add Select Component
|
71
72
73
74
|
</div>
</template>
<script>
import Icon from '../icon';
|
4aec6a66
梁灏
support Select
|
75
|
import Drop from './dropdown.vue';
|
8624560f
Graham Fairweather
Fix: Webpack is n...
|
76
|
import vClickOutside from 'v-click-outside-x';
|
595cfa72
梁灏
fixed #1187 #844 ...
|
77
|
import TransferDom from '../../directives/transfer-dom';
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
78
|
import { oneOf } from '../../utils/assist';
|
4aec6a66
梁灏
support Select
|
79
|
import Emitter from '../../mixins/emitter';
|
e5337c81
梁灏
fixed some compon...
|
80
|
import Locale from '../../mixins/locale';
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
81
82
|
import SelectHead from './select-head.vue';
import FunctionalOptions from './functional-options.vue';
|
e355dd49
梁灏
add Select Component
|
83
84
|
const prefixCls = 'ivu-select';
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
85
|
const optionRegexp = /^i-option$|^Option$/i;
|
523e2c81
Sergio Crisostomo
correct match log...
|
86
|
const optionGroupRegexp = /option-?group/i;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
87
88
89
90
91
92
93
94
95
96
|
const findChild = (instance, checkFn) => {
let match = checkFn(instance);
if (match) return instance;
for (let i = 0, l = instance.$children.length; i < l; i++){
const child = instance.$children[i];
match = findChild(child, checkFn);
if (match) return match;
}
};
|
e355dd49
梁灏
add Select Component
|
97
|
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
98
99
|
const findOptionsInVNode = (node) => {
const opts = node.componentOptions;
|
523e2c81
Sergio Crisostomo
correct match log...
|
100
|
if (opts && opts.tag.match(optionRegexp)) return [node];
|
7d14e70c
Sergio Crisostomo
Include both node...
|
101
|
if (!node.children && (!opts || !opts.children)) return [];
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
102
|
const children = [...(node.children || []), ...(opts && opts.children || [])];
|
7d14e70c
Sergio Crisostomo
Include both node...
|
103
|
const options = children.reduce(
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
104
105
106
107
108
109
110
111
112
|
(arr, el) => [...arr, ...findOptionsInVNode(el)], []
).filter(Boolean);
return options.length > 0 ? options : [];
};
const extractOptions = (options) => options.reduce((options, slotEntry) => {
return options.concat(findOptionsInVNode(slotEntry));
}, []);
|
aa21cdf9
Sergio Crisostomo
Process also shal...
|
113
114
115
116
117
118
119
120
121
122
123
124
125
|
const applyProp = (node, propName, value) => {
return {
...node,
componentOptions: {
...node.componentOptions,
propsData: {
...node.componentOptions.propsData,
[propName]: value,
}
}
};
};
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
126
127
128
129
130
131
132
133
134
135
136
137
|
const getNestedProperty = (obj, path) => {
const keys = path.split('.');
return keys.reduce((o, key) => o && o[key] || null, obj);
};
const getOptionLabel = option => {
const textContent = (option.componentOptions.children || []).reduce((str, child) => str + (child.text || ''), '');
const innerHTML = getNestedProperty(option, 'data.domProps.innerHTML');
return option.componentOptions.propsData.label || textContent || (typeof innerHTML === 'string' ? innerHTML : '');
};
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
138
139
|
const ANIMATION_TIMEOUT = 300;
|
e355dd49
梁灏
add Select Component
|
140
|
export default {
|
8f5b1686
梁灏
fixed #196
|
141
|
name: 'iSelect',
|
e5337c81
梁灏
fixed some compon...
|
142
|
mixins: [ Emitter, Locale ],
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
143
144
|
components: { FunctionalOptions, Drop, Icon, SelectHead },
directives: { clickOutside: vClickOutside.directive, TransferDom },
|
e355dd49
梁灏
add Select Component
|
145
|
props: {
|
4aec6a66
梁灏
support Select
|
146
|
value: {
|
e355dd49
梁灏
add Select Component
|
147
148
149
|
type: [String, Number, Array],
default: ''
},
|
98bf25b3
梁灏
fixed #1286
|
150
|
// 使用时,也得设置 value 才行
|
ddc35c9a
梁灏
fixed #952
|
151
152
153
154
|
label: {
type: [String, Number, Array],
default: ''
},
|
e355dd49
梁灏
add Select Component
|
155
156
157
158
159
160
161
162
163
164
165
166
167
|
multiple: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
clearable: {
type: Boolean,
default: false
},
placeholder: {
|
e5337c81
梁灏
fixed some compon...
|
168
|
type: String
|
e355dd49
梁灏
add Select Component
|
169
170
171
172
173
174
175
176
|
},
filterable: {
type: Boolean,
default: false
},
filterMethod: {
type: Function
},
|
01b54e30
梁灏
Select support re...
|
177
178
179
180
181
182
183
184
185
186
|
remoteMethod: {
type: Function
},
loading: {
type: Boolean,
default: false
},
loadingText: {
type: String
},
|
e355dd49
梁灏
add Select Component
|
187
188
|
size: {
validator (value) {
|
6932b4d7
梁灏
update Page compo...
|
189
|
return oneOf(value, ['small', 'large', 'default']);
|
e355dd49
梁灏
add Select Component
|
190
191
192
193
194
|
}
},
labelInValue: {
type: Boolean,
default: false
|
294e2412
梁灏
update Select com...
|
195
196
|
},
notFoundText: {
|
e5337c81
梁灏
fixed some compon...
|
197
|
type: String
|
f89dd9c2
梁灏
Paeg、Select add p...
|
198
199
200
201
202
203
|
},
placement: {
validator (value) {
return oneOf(value, ['top', 'bottom']);
},
default: 'bottom'
|
595cfa72
梁灏
fixed #1187 #844 ...
|
204
205
206
207
|
},
transfer: {
type: Boolean,
default: false
|
fed3e09d
梁灏
add AutoComplete ...
|
208
209
210
211
212
|
},
// Use for AutoComplete
autoComplete: {
type: Boolean,
default: false
|
0460a1e8
梁灏
fixed #812
|
213
214
215
|
},
name: {
type: String
|
acb79ba3
梁灏
fixed #433
|
216
217
218
|
},
elementId: {
type: String
|
e355dd49
梁灏
add Select Component
|
219
220
|
}
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
221
222
223
224
|
mounted(){
this.$on('on-select-selected', this.onOptionClick);
// set the initial values if there are any
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
225
226
227
228
229
|
if (!this.remote && this.selectOptions.length > 0){
this.values = this.getInitialValue().map(value => {
if (typeof value !== 'number' && !value) return null;
return this.getOptionData(value);
}).filter(Boolean);
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
230
|
}
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
231
232
233
234
|
if (this.values.length > 0 && this.selectOptions.length === 0){
this.hasExpectedValue = this.values;
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
235
|
},
|
e355dd49
梁灏
add Select Component
|
236
|
data () {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
237
|
|
e355dd49
梁灏
add Select Component
|
238
239
|
return {
prefixCls: prefixCls,
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
240
|
values: [],
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
241
|
dropDownWidth: 0,
|
e355dd49
梁灏
add Select Component
|
242
|
visible: false,
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
243
244
|
focusIndex: -1,
isFocused: false,
|
e355dd49
梁灏
add Select Component
|
245
|
query: '',
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
246
247
248
249
250
|
initialLabel: this.label,
hasMouseHoverHead: false,
slotOptions: this.$slots.default,
caretPosition: -1,
lastRemoteQuery: '',
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
251
|
unchangedQuery: true,
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
252
|
hasExpectedValue: false,
|
45bcc14d
Sergio Crisostomo
prevent calling r...
|
253
|
preventRemoteCall: false,
|
b0893113
jingsam
add eslint
|
254
|
};
|
e355dd49
梁灏
add Select Component
|
255
256
257
258
|
},
computed: {
classes () {
return [
|
4b7138b9
梁灏
fixed some bugs
|
259
|
`${prefixCls}`,
|
e355dd49
梁灏
add Select Component
|
260
|
{
|
4b7138b9
梁灏
fixed some bugs
|
261
262
263
264
265
266
|
[`${prefixCls}-visible`]: this.visible,
[`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-multiple`]: this.multiple,
[`${prefixCls}-single`]: !this.multiple,
[`${prefixCls}-show-clear`]: this.showCloseIcon,
[`${prefixCls}-${this.size}`]: !!this.size
|
e355dd49
梁灏
add Select Component
|
267
|
}
|
b0893113
jingsam
add eslint
|
268
|
];
|
e355dd49
梁灏
add Select Component
|
269
|
},
|
ecaf8d51
梁灏
Date add transfer...
|
270
271
272
|
dropdownCls () {
return {
[prefixCls + '-dropdown-transfer']: this.transfer,
|
fed3e09d
梁灏
add AutoComplete ...
|
273
274
275
276
277
278
|
[prefixCls + '-multiple']: this.multiple && this.transfer,
['ivu-auto-complete']: this.autoComplete,
};
},
selectionCls () {
return {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
279
280
|
[`${prefixCls}-selection`]: !this.autoComplete,
[`${prefixCls}-selection-focused`]: this.isFocused
|
ecaf8d51
梁灏
Date add transfer...
|
281
282
|
};
},
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
283
284
|
queryStringMatchesSelectedOption(){
const selectedOptions = this.values[0];
|
5266c905
Sergio Crisostomo
Trim label so we ...
|
285
286
287
|
if (!selectedOptions) return false;
const [query, label] = [this.query, selectedOptions.label].map(str => (str || '').trim());
return !this.multiple && this.unchangedQuery && query === label;
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
288
|
},
|
e5337c81
梁灏
fixed some compon...
|
289
|
localeNotFoundText () {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
290
|
if (typeof this.notFoundText === 'undefined') {
|
e5337c81
梁灏
fixed some compon...
|
291
292
293
294
|
return this.t('i.select.noMatch');
} else {
return this.notFoundText;
}
|
f89dd9c2
梁灏
Paeg、Select add p...
|
295
|
},
|
01b54e30
梁灏
Select support re...
|
296
|
localeLoadingText () {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
297
|
if (typeof this.loadingText === 'undefined') {
|
01b54e30
梁灏
Select support re...
|
298
299
300
301
302
|
return this.t('i.select.loading');
} else {
return this.loadingText;
}
},
|
f89dd9c2
梁灏
Paeg、Select add p...
|
303
304
|
transitionName () {
return this.placement === 'bottom' ? 'slide-up' : 'slide-down';
|
ec98f3c3
梁灏
update Select
|
305
306
307
|
},
dropVisible () {
let status = true;
|
bc348e7e
Sergio Crisostomo
adapt to auto-com...
|
308
309
|
const noOptions = !this.selectOptions || this.selectOptions.length === 0;
if (!this.loading && this.remote && this.query === '' && noOptions) status = false;
|
fed3e09d
梁灏
add AutoComplete ...
|
310
|
|
bc348e7e
Sergio Crisostomo
adapt to auto-com...
|
311
|
if (this.autoComplete && noOptions) status = false;
|
fed3e09d
梁灏
add AutoComplete ...
|
312
|
|
ec98f3c3
梁灏
update Select
|
313
|
return this.visible && status;
|
29264399
梁灏
update Select
|
314
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
315
316
|
showNotFoundLabel () {
const {loading, remote, selectOptions} = this;
|
bc348e7e
Sergio Crisostomo
adapt to auto-com...
|
317
|
return selectOptions && selectOptions.length === 0 && (!remote || (remote && !loading));
|
e355dd49
梁灏
add Select Component
|
318
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
319
320
321
|
publicValue(){
if (this.labelInValue){
return this.multiple ? this.values : this.values[0];
|
e355dd49
梁灏
add Select Component
|
322
|
} else {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
323
|
return this.multiple ? this.values.map(option => option.value) : (this.values[0] || {}).value;
|
e355dd49
梁灏
add Select Component
|
324
325
|
}
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
326
327
328
329
330
331
332
|
canBeCleared(){
const uiStateMatch = this.hasMouseHoverHead || this.active;
const qualifiesForClear = !this.multiple && this.clearable;
return uiStateMatch && qualifiesForClear && this.reset; // we return a function
},
selectOptions() {
const selectOptions = [];
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
333
|
const slotOptions = (this.slotOptions || []);
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
334
335
|
let optionCounter = -1;
const currentIndex = this.focusIndex;
|
220161f5
Sergio Crisostomo
Clean up empty/nu...
|
336
|
const selectedValues = this.values.filter(Boolean).map(({value}) => value);
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
337
338
339
340
341
342
343
344
345
346
|
if (this.autoComplete) {
const copyChildren = (node, fn) => {
return {
...node,
children: (node.children || []).map(fn).map(child => copyChildren(child, fn))
};
};
const autoCompleteOptions = extractOptions(slotOptions);
const selectedSlotOption = autoCompleteOptions[currentIndex];
|
aa21cdf9
Sergio Crisostomo
Process also shal...
|
347
348
349
350
351
352
353
|
return slotOptions.map(node => {
if (node === selectedSlotOption) return applyProp(node, 'isFocused', true);
return copyChildren(node, (child) => {
if (child !== selectedSlotOption) return child;
return applyProp(child, 'isFocused', true);
});
});
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
354
|
}
|
bc348e7e
Sergio Crisostomo
adapt to auto-com...
|
355
|
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
356
|
for (let option of slotOptions) {
|
e355dd49
梁灏
add Select Component
|
357
|
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
358
359
|
const cOptions = option.componentOptions;
if (!cOptions) continue;
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
360
361
|
if (cOptions.tag.match(optionGroupRegexp)){
let children = cOptions.children;
|
e355dd49
梁灏
add Select Component
|
362
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
363
364
365
366
367
368
|
// remove filtered children
if (this.filterable){
children = children.filter(
({componentOptions}) => this.validateOption(componentOptions)
);
}
|
e355dd49
梁灏
add Select Component
|
369
|
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
370
|
cOptions.children = children.map(opt => {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
371
372
373
|
optionCounter = optionCounter + 1;
return this.processOption(opt, selectedValues, optionCounter === currentIndex);
});
|
3e855e34
梁灏
fixed #46
|
374
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
375
|
// keep the group if it still has children
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
376
|
if (cOptions.children.length > 0) selectOptions.push({...option});
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
377
378
|
} else {
// ignore option if not passing filter
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
379
|
const optionPassesFilter = this.filterable ? this.validateOption(cOptions) : option;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
380
|
if (!optionPassesFilter) continue;
|
3e855e34
梁灏
fixed #46
|
381
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
382
383
|
optionCounter = optionCounter + 1;
selectOptions.push(this.processOption(option, selectedValues, optionCounter === currentIndex));
|
3e855e34
梁灏
fixed #46
|
384
|
}
|
e355dd49
梁灏
add Select Component
|
385
386
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
387
|
return selectOptions;
|
e355dd49
梁灏
add Select Component
|
388
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
389
|
flatOptions(){
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
390
|
return extractOptions(this.selectOptions);
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
},
selectTabindex(){
return this.disabled || this.filterable ? -1 : 0;
},
remote(){
return typeof this.remoteMethod === 'function';
}
},
methods: {
setQuery(query){ // PUBLIC API
if (query) {
this.onQueryChange(query);
return;
}
if (query === null) {
this.onQueryChange('');
this.values = [];
|
e355dd49
梁灏
add Select Component
|
408
409
|
}
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
410
|
clearSingleSelect(){ // PUBLIC API
|
7dbde804
Sergio Crisostomo
add on-clear event
|
411
|
this.$emit('on-clear');
|
c3304bce
Sergio Crisostomo
correct unchanged...
|
412
|
this.hideMenu();
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
413
414
415
416
|
if (this.clearable) this.values = [];
},
getOptionData(value){
const option = this.flatOptions.find(({componentOptions}) => componentOptions.propsData.value === value);
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
417
|
if (!option) return null;
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
418
|
const label = getOptionLabel(option);
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
419
420
421
422
423
424
|
return {
value: value,
label: label,
};
},
getInitialValue(){
|
31e4380d
luffyzhao
还是要保留multiple判断
|
425
|
const {multiple, value} = this;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
426
|
let initialValue = Array.isArray(value) ? value : [value];
|
31e4380d
luffyzhao
还是要保留multiple判断
|
427
|
if (!multiple && (typeof initialValue[0] === 'undefined' || (String(initialValue[0]).trim() === '' && !Number.isFinite(initialValue[0])))) initialValue = [];
|
b4138675
luffyzhao
select-binding-0
|
428
|
return initialValue.filter((item) => {
|
31e4380d
luffyzhao
还是要保留multiple判断
|
429
|
return Boolean(item) || item === 0
|
b4138675
luffyzhao
select-binding-0
|
430
|
});
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
431
432
433
434
435
436
437
438
439
440
441
442
443
|
},
processOption(option, values, isFocused){
if (!option.componentOptions) return option;
const optionValue = option.componentOptions.propsData.value;
const disabled = option.componentOptions.propsData.disabled;
const isSelected = values.includes(optionValue);
const propsData = {
...option.componentOptions.propsData,
selected: isSelected,
isFocused: isFocused,
disabled: typeof disabled === 'undefined' ? false : disabled !== false,
};
|
e355dd49
梁灏
add Select Component
|
444
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
445
446
447
448
449
|
return {
...option,
componentOptions: {
...option.componentOptions,
propsData: propsData
|
e355dd49
梁灏
add Select Component
|
450
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
451
452
|
};
},
|
e355dd49
梁灏
add Select Component
|
453
|
|
db5110c2
Sergio Crisostomo
Allow wider searc...
|
454
|
validateOption({children, elm, propsData}){
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
455
|
if (this.queryStringMatchesSelectedOption) return true;
|
db5110c2
Sergio Crisostomo
Allow wider searc...
|
456
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
457
458
|
const value = propsData.value;
const label = propsData.label || '';
|
db5110c2
Sergio Crisostomo
Allow wider searc...
|
459
460
461
462
|
const textContent = (elm && elm.textContent) || (children || []).reduce((str, node) => {
const nodeText = node.elm ? node.elm.textContent : node.text;
return `${str} ${nodeText}`;
}, '') || '';
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
463
|
const stringValues = JSON.stringify([value, label, textContent]);
|
5266c905
Sergio Crisostomo
Trim label so we ...
|
464
|
const query = this.query.toLowerCase().trim();
|
c3304bce
Sergio Crisostomo
correct unchanged...
|
465
|
return stringValues.toLowerCase().includes(query);
|
e355dd49
梁灏
add Select Component
|
466
|
},
|
d87ce40a
梁灏
update Select
|
467
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
468
469
470
|
toggleMenu (e, force) {
if (this.disabled || this.autoComplete) {
return false;
|
d87ce40a
梁灏
update Select
|
471
|
}
|
d87ce40a
梁灏
update Select
|
472
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
473
474
475
|
this.visible = typeof force !== 'undefined' ? force : !this.visible;
if (this.visible){
this.dropDownWidth = this.$el.getBoundingClientRect().width;
|
cf753854
Sergio Crisostomo
Corrections after...
|
476
|
this.broadcast('Drop', 'on-update-popper');
|
e4ce9917
梁灏
update Select com...
|
477
|
}
|
e355dd49
梁灏
add Select Component
|
478
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
479
480
|
hideMenu () {
this.toggleMenu(null, false);
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
481
|
setTimeout(() => this.unchangedQuery = true, ANIMATION_TIMEOUT);
|
e355dd49
梁灏
add Select Component
|
482
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
483
484
|
onClickOutside(event){
if (this.visible) {
|
4a9974f6
Graham Fairweather
Normalise v-ckick...
|
485
486
487
488
|
if (event.type === 'mousedown') {
event.preventDefault();
return;
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
489
490
|
if (this.filterable) {
|
ae7579e9
Sergio Crisostomo
Fix input getters...
|
491
|
const input = this.$el.querySelector('input[type="text"]');
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
492
493
494
495
|
this.caretPosition = input.selectionStart;
this.$nextTick(() => {
const caretPosition = this.caretPosition === -1 ? input.value.length : this.caretPosition;
input.setSelectionRange(caretPosition, caretPosition);
|
e355dd49
梁灏
add Select Component
|
496
497
498
|
});
}
|
ae7579e9
Sergio Crisostomo
Fix input getters...
|
499
|
if (!this.autoComplete) event.stopPropagation();
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
500
501
502
503
504
505
|
event.preventDefault();
this.hideMenu();
this.isFocused = true;
} else {
this.caretPosition = -1;
this.isFocused = false;
|
e355dd49
梁灏
add Select Component
|
506
507
|
}
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
508
|
reset(){
|
31788df3
Sergio Crisostomo
Normalise behavio...
|
509
|
this.unchangedQuery = true;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
510
|
this.values = [];
|
e355dd49
梁灏
add Select Component
|
511
512
|
},
handleKeydown (e) {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
513
514
515
516
|
if (e.key === 'Backspace'){
return; // so we don't call preventDefault
}
|
e355dd49
梁灏
add Select Component
|
517
|
if (this.visible) {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
518
519
520
521
522
|
e.preventDefault();
if (e.key === 'Tab'){
e.stopPropagation();
}
|
e355dd49
梁灏
add Select Component
|
523
|
// Esc slide-up
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
524
|
if (e.key === 'Escape') {
|
16fc6361
Sergio Crisostomo
stop propagation ...
|
525
|
e.stopPropagation();
|
e355dd49
梁灏
add Select Component
|
526
527
528
|
this.hideMenu();
}
// next
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
529
530
|
if (e.key === 'ArrowUp') {
this.navigateOptions(-1);
|
e355dd49
梁灏
add Select Component
|
531
532
|
}
// prev
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
533
534
|
if (e.key === 'ArrowDown') {
this.navigateOptions(1);
|
e355dd49
梁灏
add Select Component
|
535
536
|
}
// enter
|
7e3fc4a5
Sergio Crisostomo
close the menu if...
|
537
538
|
if (e.key === 'Enter') {
if (this.focusIndex === -1) return this.hideMenu();
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
539
540
541
|
const optionComponent = this.flatOptions[this.focusIndex];
const option = this.getOptionData(optionComponent.componentOptions.propsData.value);
this.onOptionClick(option);
|
e355dd49
梁灏
add Select Component
|
542
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
543
544
545
|
} else {
const keysThatCanOpenSelect = ['ArrowUp', 'ArrowDown'];
if (keysThatCanOpenSelect.includes(e.key)) this.toggleMenu(null, true);
|
e355dd49
梁灏
add Select Component
|
546
547
|
}
|
e355dd49
梁灏
add Select Component
|
548
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
549
550
551
|
},
navigateOptions(direction){
const optionsLength = this.flatOptions.length - 1;
|
e4ebd304
梁灏
update Select com...
|
552
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
553
554
555
|
let index = this.focusIndex + direction;
if (index < 0) index = optionsLength;
if (index > optionsLength) index = 0;
|
e355dd49
梁灏
add Select Component
|
556
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
557
558
559
560
561
562
563
|
// find nearest option in case of disabled options in between
if (direction > 0){
let nearestActiveOption = -1;
for (let i = 0; i < this.flatOptions.length; i++){
const optionIsActive = !this.flatOptions[i].componentOptions.propsData.disabled;
if (optionIsActive) nearestActiveOption = i;
if (nearestActiveOption >= index) break;
|
e355dd49
梁灏
add Select Component
|
564
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
565
566
567
568
569
570
571
|
index = nearestActiveOption;
} else {
let nearestActiveOption = this.flatOptions.length;
for (let i = optionsLength; i >= 0; i--){
const optionIsActive = !this.flatOptions[i].componentOptions.propsData.disabled;
if (optionIsActive) nearestActiveOption = i;
if (nearestActiveOption <= index) break;
|
e4ebd304
梁灏
update Select com...
|
572
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
573
|
index = nearestActiveOption;
|
e355dd49
梁灏
add Select Component
|
574
|
}
|
e355dd49
梁灏
add Select Component
|
575
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
576
|
this.focusIndex = index;
|
e4ebd304
梁灏
update Select com...
|
577
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
578
579
580
581
582
583
|
onOptionClick(option) {
if (this.multiple){
// keep the query for remote select
if (this.remote) this.lastRemoteQuery = this.lastRemoteQuery || this.query;
else this.lastRemoteQuery = '';
|
e4ebd304
梁灏
update Select com...
|
584
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
585
586
587
|
const valueIsSelected = this.values.find(({value}) => value === option.value);
if (valueIsSelected){
this.values = this.values.filter(({value}) => value !== option.value);
|
e4ebd304
梁灏
update Select com...
|
588
|
} else {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
589
|
this.values = this.values.concat(option);
|
e4ebd304
梁灏
update Select com...
|
590
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
591
592
593
|
this.isFocused = true; // so we put back focus after clicking with mouse on option elements
} else {
|
5266c905
Sergio Crisostomo
Trim label so we ...
|
594
|
this.query = String(option.label).trim();
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
595
596
597
598
599
|
this.values = [option];
this.lastRemoteQuery = '';
this.hideMenu();
}
|
52cfcd66
Sergio Crisostomo
Keep last selecte...
|
600
601
602
603
604
|
this.focusIndex = this.flatOptions.findIndex((opt) => {
if (!opt || !opt.componentOptions) return false;
return opt.componentOptions.propsData.value === option.value;
});
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
605
|
if (this.filterable){
|
ae7579e9
Sergio Crisostomo
Fix input getters...
|
606
607
|
const inputField = this.$el.querySelector('input[type="text"]');
if (!this.autoComplete) this.$nextTick(() => inputField.focus());
|
e4ce9917
梁灏
update Select com...
|
608
|
}
|
88ef37f5
Aresn
fixed in multiple...
|
609
|
this.broadcast('Drop', 'on-update-popper');
|
3e855e34
梁灏
fixed #46
|
610
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
611
|
onQueryChange(query) {
|
c3304bce
Sergio Crisostomo
correct unchanged...
|
612
|
if (query.length > 0 && query !== this.query) this.visible = true;
|
2f0b086d
梁灏
fixed #116
|
613
|
this.query = query;
|
c3304bce
Sergio Crisostomo
correct unchanged...
|
614
|
this.unchangedQuery = this.visible;
|
9c3a3e7d
YikaJ
更新 Select 组件
|
615
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
616
617
618
|
toggleHeaderFocus({type}){
if (this.disabled) {
return;
|
15b72d31
梁灏
fixed #566
|
619
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
620
|
this.isFocused = type === 'focus';
|
98bf25b3
梁灏
fixed #1286
|
621
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
622
623
|
updateSlotOptions(){
this.slotOptions = this.$slots.default;
|
e355dd49
梁灏
add Select Component
|
624
625
|
}
},
|
e355dd49
梁灏
add Select Component
|
626
|
watch: {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
627
628
629
630
631
|
value(value){
const {getInitialValue, getOptionData, publicValue} = this;
if (value === '') this.values = [];
else if (JSON.stringify(value) !== JSON.stringify(publicValue)) {
|
220161f5
Sergio Crisostomo
Clean up empty/nu...
|
632
|
this.$nextTick(() => this.values = getInitialValue().map(getOptionData).filter(Boolean));
|
e355dd49
梁灏
add Select Component
|
633
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
634
635
636
637
|
},
values(now, before){
const newValue = JSON.stringify(now);
const oldValue = JSON.stringify(before);
|
9366c9a7
Sergio Crisostomo
Select improvemen...
|
638
639
640
641
642
|
// v-model is always just the value, event with labelInValue === true
const vModelValue = (this.publicValue && this.labelInValue) ?
(this.multiple ? this.publicValue.map(({value}) => value) : this.publicValue.value) :
this.publicValue;
const shouldEmitInput = newValue !== oldValue && vModelValue !== this.value;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
643
|
if (shouldEmitInput) {
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
644
645
646
|
this.$emit('input', vModelValue); // to update v-model
this.$emit('on-change', this.publicValue);
this.dispatch('FormItem', 'on-form-change', this.publicValue);
|
219e5c92
梁灏
fixed #957
|
647
|
}
|
e355dd49
梁灏
add Select Component
|
648
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
649
650
651
652
|
query (query) {
this.$emit('on-query-change', query);
const {remoteMethod, lastRemoteQuery} = this;
const hasValidQuery = query !== '' && (query !== lastRemoteQuery || !lastRemoteQuery);
|
45bcc14d
Sergio Crisostomo
prevent calling r...
|
653
654
|
const shouldCallRemoteMethod = remoteMethod && hasValidQuery && !this.preventRemoteCall;
this.preventRemoteCall = false; // remove the flag
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
655
656
657
658
659
660
661
662
663
|
if (shouldCallRemoteMethod){
this.focusIndex = -1;
const promise = this.remoteMethod(query);
this.initialLabel = '';
if (promise && promise.then){
promise.then(options => {
if (options) this.options = options;
});
|
b7cf983e
梁灏
update Select com...
|
664
|
}
|
e355dd49
梁灏
add Select Component
|
665
|
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
666
|
if (query !== '' && this.remote) this.lastRemoteQuery = query;
|
e4ebd304
梁灏
update Select com...
|
667
|
},
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
668
669
670
671
672
673
|
loading(state){
if (state === false){
this.updateSlotOptions();
}
},
isFocused(focused){
|
ae7579e9
Sergio Crisostomo
Fix input getters...
|
674
|
const el = this.filterable ? this.$el.querySelector('input[type="text"]') : this.$el;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
675
|
el[this.isFocused ? 'focus' : 'blur']();
|
d8bb1771
windywany
let select compon...
|
676
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
677
678
679
|
// restore query value in filterable single selects
const [selectedOption] = this.values;
if (selectedOption && this.filterable && !this.multiple && !focused){
|
5266c905
Sergio Crisostomo
Trim label so we ...
|
680
|
const selectedLabel = String(selectedOption.label || selectedOption.value).trim();
|
9ca6671c
Sergio Crisostomo
Check for selecte...
|
681
|
if (selectedLabel && this.query !== selectedLabel) {
|
45bcc14d
Sergio Crisostomo
prevent calling r...
|
682
683
684
|
this.preventRemoteCall = true;
this.query = selectedLabel;
}
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
685
686
687
|
}
},
focusIndex(index){
|
06a74f9e
Sergio Crisostomo
Allow select to n...
|
688
|
if (index < 0 || this.autoComplete) return;
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
689
690
691
692
693
|
// update scroll
const optionValue = this.flatOptions[index].componentOptions.propsData.value;
const optionInstance = findChild(this, ({$options}) => {
return $options.componentName === 'select-item' && $options.propsData.value === optionValue;
});
|
e4ce9917
梁灏
update Select com...
|
694
|
|
c9b86944
Sergio Crisostomo
Refactor Select!
|
695
696
697
698
699
700
701
|
let bottomOverflowDistance = optionInstance.$el.getBoundingClientRect().bottom - this.$refs.dropdown.$el.getBoundingClientRect().bottom;
let topOverflowDistance = optionInstance.$el.getBoundingClientRect().top - this.$refs.dropdown.$el.getBoundingClientRect().top;
if (bottomOverflowDistance > 0) {
this.$refs.dropdown.$el.scrollTop += bottomOverflowDistance;
}
if (topOverflowDistance < 0) {
this.$refs.dropdown.$el.scrollTop += topOverflowDistance;
|
01b54e30
梁灏
Select support re...
|
702
|
}
|
cf753854
Sergio Crisostomo
Corrections after...
|
703
704
705
|
},
dropVisible(open){
this.broadcast('Drop', open ? 'on-update-popper' : 'on-destroy-popper');
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
706
|
},
|
f7f65c84
Sergio Crisostomo
reset query only ...
|
707
|
selectOptions(){
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
708
|
if (this.hasExpectedValue){
|
220161f5
Sergio Crisostomo
Clean up empty/nu...
|
709
|
this.values = this.values.map(this.getOptionData).filter(Boolean);
|
7f63e58c
Sergio Crisostomo
Make possible for...
|
710
711
|
this.hasExpectedValue = false;
}
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
712
|
|
f7f65c84
Sergio Crisostomo
reset query only ...
|
713
|
if (this.slotOptions && this.slotOptions.length === 0){
|
b6c069ca
Sergio Crisostomo
reset query if op...
|
714
715
|
this.query = '';
}
|
1376a01a
Sergio Crisostomo
Emit on-open-chan...
|
716
717
718
|
},
visible(state){
this.$emit('on-open-change', state);
|
e355dd49
梁灏
add Select Component
|
719
720
|
}
}
|
b0893113
jingsam
add eslint
|
721
|
};
|
d6342fe1
jingsam
fixed ie bug
|
722
|
</script>
|