-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VCombobox.ts
278 lines (240 loc) Β· 7.75 KB
/
VCombobox.ts
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
// Styles
import '../VAutocomplete/VAutocomplete.sass'
// Extensions
import VSelect from '../VSelect/VSelect'
import VAutocomplete from '../VAutocomplete/VAutocomplete'
// Utils
import { keyCodes } from '../../util/helpers'
// Types
import { PropValidator } from 'vue/types/options'
/* @vue/component */
export default VAutocomplete.extend({
name: 'v-combobox',
props: {
delimiters: {
type: Array,
default: () => ([]),
} as PropValidator<string[]>,
returnObject: {
type: Boolean,
default: true,
},
},
data: () => ({
editingIndex: -1,
}),
computed: {
computedCounterValue (): number {
return this.multiple
? this.selectedItems.length
: (this.internalSearch || '').toString().length
},
hasSlot (): boolean {
return VSelect.options.computed.hasSlot.call(this) || this.multiple
},
isAnyValueAllowed (): boolean {
return true
},
menuCanShow (): boolean {
if (!this.isFocused) return false
return this.hasDisplayedItems ||
(!!this.$slots['no-data'] && !this.hideNoData)
},
searchIsDirty (): boolean {
return this.internalSearch != null
},
},
methods: {
onInternalSearchChanged (val: any) {
if (
val &&
this.multiple &&
this.delimiters.length
) {
const delimiter = this.delimiters.find(d => val.endsWith(d))
if (delimiter != null) {
this.internalSearch = val.slice(0, val.length - delimiter.length)
this.updateTags()
}
}
this.updateMenuDimensions()
},
genInput () {
const input = VAutocomplete.options.methods.genInput.call(this)
delete input.data!.attrs!.name
input.data!.on!.paste = this.onPaste
return input
},
genChipSelection (item: object, index: number) {
const chip = VSelect.options.methods.genChipSelection.call(this, item, index)
// Allow user to update an existing value
if (this.multiple) {
chip.componentOptions!.listeners! = {
...chip.componentOptions!.listeners!,
dblclick: () => {
this.editingIndex = index
this.internalSearch = this.getText(item)
this.selectedIndex = -1
},
}
}
return chip
},
onChipInput (item: object) {
VSelect.options.methods.onChipInput.call(this, item)
this.editingIndex = -1
},
// Requires a manual definition
// to overwrite removal in v-autocomplete
onEnterDown (e: Event) {
e.preventDefault()
// If has menu index, let v-select-list handle
if (this.getMenuIndex() > -1) return
this.$nextTick(this.updateSelf)
},
onKeyDown (e: KeyboardEvent) {
const keyCode = e.keyCode
if (
e.ctrlKey ||
![keyCodes.home, keyCodes.end].includes(keyCode)
) {
VSelect.options.methods.onKeyDown.call(this, e)
}
// If user is at selection index of 0
// create a new tag
if (this.multiple &&
keyCode === keyCodes.left &&
this.$refs.input.selectionStart === 0
) {
this.updateSelf()
} else if (keyCode === keyCodes.enter) {
this.onEnterDown(e)
}
// The ordering is important here
// allows new value to be updated
// and then moves the index to the
// proper location
this.changeSelectedIndex(keyCode)
},
onTabDown (e: KeyboardEvent) {
// When adding tags, if searching and
// there is not a filtered options,
// add the value to the tags list
if (this.multiple &&
this.internalSearch &&
this.getMenuIndex() === -1
) {
e.preventDefault()
e.stopPropagation()
return this.updateTags()
}
VAutocomplete.options.methods.onTabDown.call(this, e)
},
selectItem (item: object) {
// Currently only supports items:<string[]>
if (this.editingIndex > -1) {
this.updateEditing()
} else {
VAutocomplete.options.methods.selectItem.call(this, item)
// if selected item contains search value,
// remove the search string
if (
this.internalSearch &&
this.multiple &&
this.getText(item).toLocaleLowerCase().includes(this.internalSearch.toLocaleLowerCase())
) {
this.internalSearch = null
}
}
},
setSelectedItems () {
if (this.internalValue == null ||
this.internalValue === ''
) {
this.selectedItems = []
} else {
this.selectedItems = this.multiple ? this.internalValue : [this.internalValue]
}
},
setValue (value?: any) {
VSelect.options.methods.setValue.call(this, value === undefined ? this.internalSearch : value)
},
updateEditing () {
const value = this.internalValue.slice()
const index = this.selectedItems.findIndex(item =>
this.getText(item) === this.internalSearch)
// If user enters a duplicate text on chip edit,
// don't add it, move it to the end of the list
if (index > -1) {
const item = typeof value[index] === 'object'
? Object.assign({}, value[index])
: value[index]
value.splice(index, 1)
value.push(item)
} else {
value[this.editingIndex] = this.internalSearch
}
this.setValue(value)
this.editingIndex = -1
this.internalSearch = null
},
updateCombobox () {
// If search is not dirty, do nothing
if (!this.searchIsDirty) return
// The internal search is not matching
// the internal value, update the input
if (this.internalSearch !== this.getText(this.internalValue)) this.setValue()
// Reset search if using slot to avoid a double input
const isUsingSlot = Boolean(this.$scopedSlots.selection) || this.hasChips
if (isUsingSlot) this.internalSearch = null
},
updateSelf () {
this.multiple ? this.updateTags() : this.updateCombobox()
},
updateTags () {
const menuIndex = this.getMenuIndex()
// If the user is not searching
// and no menu item is selected
// or if the search is empty
// do nothing
if ((menuIndex < 0 && !this.searchIsDirty) ||
!this.internalSearch) return
if (this.editingIndex > -1) {
return this.updateEditing()
}
const index = this.selectedItems.findIndex(item =>
this.internalSearch === this.getText(item))
// If the duplicate item is an object,
// copy it, so that it can be added again later
const itemToSelect = index > -1 && typeof this.selectedItems[index] === 'object'
? Object.assign({}, this.selectedItems[index])
: this.internalSearch
// If it already exists, do nothing
// this might need to change to bring
// the duplicated item to the last entered
if (index > -1) {
const internalValue = this.internalValue.slice()
internalValue.splice(index, 1)
this.setValue(internalValue)
}
// If menu index is greater than 1
// the selection is handled elsewhere
// TODO: find out where
if (menuIndex > -1) return (this.internalSearch = null)
this.selectItem(itemToSelect)
this.internalSearch = null
},
onPaste (event: ClipboardEvent) {
if (!this.multiple || this.searchIsDirty) return
const pastedItemText = event.clipboardData?.getData('text/vnd.vuetify.autocomplete.item+plain')
if (pastedItemText && this.findExistingIndex(pastedItemText as any) === -1) {
event.preventDefault()
VSelect.options.methods.selectItem.call(this, pastedItemText as any)
}
},
clearableCallback () {
this.editingIndex = -1
VAutocomplete.options.methods.clearableCallback.call(this)
},
},
})