-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.vue
More file actions
514 lines (467 loc) · 13 KB
/
index.vue
File metadata and controls
514 lines (467 loc) · 13 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<template lang="pug">
.c-select(
role="combobox"
aria-autocomplete="list"
aria-haspopup="true"
:aria-expanded="isOpen"
aria-disabled="disabled"
:tabindex="disabled ? -1 : 0"
:class="classNames"
@keydown="onKeyDown"
@click="toggleOpen"
)
i.c-select__caret
.c-select__selection
.c-select__placeholder(
v-if="showPlaceholder"
) {{ placeholder }}
.c-select__value(
v-if="!multiple && selectedOptions.length"
) {{ selectedOptions[0].label }}
.c-chip(
v-if="multiple && index <= maxChipCount"
v-for="(option, index) in selectedOptions"
:key="index"
:class=`{
'is-disabled': option.disabled,
'is-closeable': index < maxChipCount
}`
)
slot(name="selection" :option="option")
span(v-if="index < maxChipCount") {{ option.label }}
span(v-else) {{ maxChipText }}
.c-chip__close(
@click.stop="unselectOption(option)"
v-if="index < maxChipCount"
)
c-icon(name="x" valign="middle")
.c-select__input(
v-show="showInput"
:class="multiple ? 'is-multiple' : 'is-single'"
)
input(
v-model="query"
autocomplete="off"
@click.stop="noop"
@blur="$el.focus()"
@keydown.delete="onDeleteKey"
@input="onSearchInput"
)
c-portal(:aria-hidden="'' + !isOpen")
transition(name="fade-in-down")
.c-select__menu(
role="menu"
aria-activedescendant
v-show="isOpen"
:style="menuStyle"
:class="size ? 'is-'+size : ''"
ref="menu"
)
slot(
name="no-match"
v-if="autocomplete && !filteredOptions.length"
)
.c-select__empty 无匹配选项
c-option(
ref="$options"
v-for="(option, index) in filteredOptions"
:key="index"
:label="option.label"
:isActive="activeOption == option"
:isSelected="selectedOptions.indexOf(option) > -1"
:disabled="option.disabled"
:option="option"
)
slot(
name="menu-item"
:label="option.label"
:isActive="activeOption == option"
:isSelected="selectedOptions.indexOf(option) > -1"
:disabled="option.disabled"
:index="index"
:option="option"
)
</template>
<script>
import throttle from 'lodash/throttle'
import './index.css'
import zIndex from '../../scripts/utils/zIndexManager'
import { getPosition, POSITION } from './position'
import resettable from '../../scripts/mixins/resettable'
import validatable from '../../scripts/mixins/validatable'
// ensure each option has label and value
const normalizeOptions = (options = []) => {
return options.map(option => {
if (typeof option === 'string') return { label: option, value: option }
return option
})
}
export default {
name: 'c-select',
mixins: [resettable, validatable],
props: {
value: [Number, String, Object, Array],
options: Array,
disabled: Boolean,
placeholder: {
type: String,
default: '请选择...'
},
multiple: Boolean,
combobox: Boolean,
autocomplete: Boolean,
size: String,
width: String,
filter: {
type: Function,
default: (options, query) => {
const q = query.trim().toLowerCase()
if (!q) return options
return options
.filter(option => option.label.toLowerCase().indexOf(q) > -1)
}
},
filterThrottle: {
type: Number,
default: 0
},
maxChipCount: {
type: Number,
default: Infinity
},
maxChipPlaceholder: {
type: [String, Function],
default: ommittedCount => `和其它${ommittedCount}个选项`
}
},
model: {
event: 'change'
},
provide () {
return { $select: this }
},
inject: {
$form: { default: null }
},
data () {
return {
isOpen: false,
menuStyle: {
top: 'auto',
left: 'auto',
minWidth: 0
},
activeOption: null,
selectedOptions: [],
filteredOptions: [],
selectionEl: null,
menuEl: null,
query: '',
promiseId: 0
}
},
computed: {
normalizedOptions () {
return normalizeOptions(this.options)
},
canInput () {
return this.combobox || this.autocomplete
},
showInput () {
return this.canInput && this.isOpen
},
classNames () {
const classNames = [
{
'is-open': this.isOpen,
'is-disabled': this.disabled
}
]
const { size, width, $form } = this
const actualSize = size || ($form && $form.size)
const actualWidth = width || ($form && $form.width)
if (actualSize) classNames.push(`is-${actualSize}`)
if (actualWidth) classNames.push(`is-${actualWidth}`)
return classNames
},
selectedValues () {
return this.selectedOptions.map(option => option.value)
},
selectedLabel () {
return this.selectedOptions.map(option => option.label)
},
showPlaceholder () {
const empty = !this.selectedOptions.length
return empty && !this.isOpen
},
exceedMaxChipCount () {
return this.selectedOptions.length > this.maxChipCount
},
maxChipText () {
if (!this.exceedMaxChipCount) return
const ommittedCount = this.selectedOptions.length - this.maxChipCount
const { maxChipPlaceholder } = this
if (typeof maxChipPlaceholder === 'function') {
return maxChipPlaceholder(ommittedCount)
}
return maxChipPlaceholder
}
},
watch: {
isOpen () {
if (this.isOpen) {
this.menuStyle.minWidth = `${this.$el.offsetWidth}px`
this.positionMenu()
window.addEventListener('click', this.onBodyClick, true)
} else {
window.removeEventListener('click', this.onBodyClick, true)
}
},
value: {
immediate: true,
handler: function () {
this.updateSelectedOptions()
}
},
options: {
immediate: true,
handler: function () {
this.updateSelectedOptions()
}
},
selectedOptions: function () {
if (!this.multiple || this.$isServer) return
this.$nextTick(function () {
this.positionMenu()
})
}
},
created () {
// filter function throttled
this.filterFunction = throttle((options, query) => {
const filtered = this.filter(options, query)
if (typeof filtered.then === 'function') {
const promiseId = Date.now()
this.promiseId = promiseId
filtered.then(options => {
if (this.promiseId > promiseId) return
this.filteredOptions = normalizeOptions(options)
})
} else {
this.filteredOptions = normalizeOptions(filtered)
}
}, this.filterThrottle, {
leading: true,
trailing: true
})
},
mounted () {
this.menuEl = this.$refs.menu
// hover the option
this.$on('option-activated', option => {
this.activeOption = option
})
// reset position on window.resize
this.__onresize = throttle(
() => {
if (this.isOpen) {
this.positionMenu()
}
},
this.$clair.defaultThrottleTime
)
window.addEventListener('resize', this.__onresize)
// select the option
this.$on('option-clicked', option => this.selectOption(option))
// watch options, query to filter options
this.$watch(
function () {
return [this.normalizedOptions, this.query, this.isOpen]
},
function filterOptions () {
const { autocomplete, query } = this
if (!autocomplete) {
this.filteredOptions = this.normalizedOptions
return
}
this.filterFunction(this.normalizedOptions, query)
}
)
},
beforeDestroy () {
window.removeEventListener('resize', this.__onresize)
},
methods: {
toggleOpen () {
if (this.disabled) return
if (this.isOpen) {
this.close()
} else {
this.open()
}
},
getOption (value) {
const fn = option => option.value === value
return this.filteredOptions.find(fn) ||
this.normalizedOptions.find(fn) ||
this.selectedOptions.find(fn)
},
updateSelectedOptions () {
const { value, multiple } = this
const isEmpty = value === void 0 || value === null
if (isEmpty) {
this.selectedOptions = []
return
}
if (multiple) {
const isArray = Array.isArray(value)
const isEmptyArray = isArray && value.length === 0
if (isEmptyArray) return
const valueArr = isArray ? value : [value]
this.selectedOptions = valueArr
.map(v => this.getOption(v))
.filter(option => option)
} else {
const option = this.getOption(value)
this.selectedOptions = option ? [option] : []
}
},
open () {
this.isOpen = true;
[this.activeOption] = this.filteredOptions
if (this.showInput) {
this.query = ''
this.$nextTick(_ => {
this.$el.querySelector('input').focus()
})
}
},
close () {
this.isOpen = false
},
getNextOption (current) {
const currentIndex = this.filteredOptions.indexOf(current)
const next = this.filteredOptions.find((option, index) => {
return index > currentIndex && !option.disabled
})
return next || current
},
getPreviousOption (current) {
let prev = null
const currentIndex = this.filteredOptions.indexOf(current)
for (let i = currentIndex - 1; i >= 0; i--) {
if (!this.filteredOptions[i].disabled) {
prev = this.filteredOptions[i]
break
}
}
return prev || current
},
activateNext () {
const next = this.getNextOption(this.activeOption)
this.activeOption = next
},
activatePrevious () {
const prev = this.getPreviousOption(this.activeOption)
this.activeOption = prev
},
selectPrevious () {
const prev = this.getPreviousOption(this.selectedOptions[0])
this.selectOption(prev)
},
selectNext () {
const next = this.getNextOption(this.selectedOptions[0])
this.selectOption(next)
},
selectOption (option) {
if (this.multiple) {
if (this.autocomplete) this.query = ''
const isSelected = this.selectedOptions.includes(option)
if (isSelected) return this.unselectOption(option)
this.selectedOptions.push(option)
} else {
this.selectedOptions = [option]
this.close()
}
this.emitChange()
},
unselectOption (option) {
const index = this.selectedOptions.indexOf(option)
this.selectedOptions.splice(index, 1)
this.emitChange()
},
positionMenu () {
const pos = this.canInput ? POSITION.BOTTOM : POSITION.TOP
const { top, left } = getPosition(this.menuEl, this.$el, pos)
const { style } = this.menuEl
style.top = `${top}px`
style.left = `${left}px`
style.zIndex = zIndex.next()
},
onBodyClick (e) {
const isInSelect = this.$el.contains(e.target)
const isInMenu = this.menuEl.contains(e.target)
if (!isInSelect && !isInMenu) {
this.close()
this.$el.focus()
}
},
onDeleteKey (e) {
if (!this.query) this.selectedOptions.pop()
},
onKeyDown (e) {
const keys = {
ENTER: 13,
ESC: 27,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40
}
const { keyCode } = e
const {
isOpen,
multiple,
open,
close,
selectOption,
selectPrevious,
selectNext,
activeOption,
activateNext,
activatePrevious
} = this
if (Object.values(keys).includes(keyCode)) e.preventDefault()
// open menu on space, up, down key
const openTrigger = [
keys.SPACE,
keys.ENTER,
keys.UP,
keys.DOWN
].includes(keyCode)
if (openTrigger && !isOpen) return open()
// close menu on escape
if (keyCode === keys.ESC && isOpen) return close()
// press enter to select
if (keyCode === keys.ENTER && isOpen) return selectOption(activeOption)
// use left, right to navigate on closed state of non-multiple select
const canSelect = !isOpen && !multiple
if (canSelect && keyCode === keys.LEFT) return selectPrevious()
if (canSelect && keyCode === keys.RIGHT) return selectNext()
// use up, down to navigate on open state
if (isOpen && keyCode === keys.UP) return activatePrevious()
if (isOpen && keyCode === keys.DOWN) return activateNext()
},
onSearchInput (e) {
this.$emit('searchinput', e.target.value)
},
emitChange () {
const value = this.multiple ? this.selectedValues : this.selectedValues[0]
const label = this.multiple ? this.selectedLabel : this.selectedLabel[0]
console.log(value, label)
this.$emit('change', value, label)
}
}
}
</script>