Skip to content

Commit 6ff06e3

Browse files
committed
Fix pasted values in Autocomplete
1 parent 8646e04 commit 6ff06e3

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/components/Autocomplete.vue

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@keydown="keyDown"
1414
@keyup="keyUp"
1515
@click="update"
16-
@onpaste="onPaste"
16+
@paste="onPaste"
1717
v-bind="remaining">
1818

1919
<button type="button" @click="toggle" class="absolute inset-y-0 right-0 flex items-center rounded-r-md px-2 focus:outline-none" tabindex="-1">
@@ -107,15 +107,10 @@ const take = ref(props.viewCount)
107107
const filteredValues = ref<any[]>([])
108108
109109
const filteredOptions = computed(() => {
110-
try {
111-
let ret = !inputValue.value
112-
? props.options
113-
: props.options.filter(x => props.match(x, inputValue.value)).slice(0,take.value)
114-
return ret
115-
} catch(e) {
116-
console.log('filteredOptions error', e)
117-
}
118-
return []
110+
let ret = !inputValue.value
111+
? props.options
112+
: props.options.filter(x => props.match(x, inputValue.value)).slice(0,take.value)
113+
return ret
119114
})
120115
121116
const navKeys = ['Tab', 'Escape', 'ArrowDown', 'ArrowUp', 'Enter', 'PageUp', 'PageDown', 'Home', 'End']
@@ -147,12 +142,23 @@ function handlePastedText(txt?:string) {
147142
focusNextElement()
148143
}
149144
} else if (multipleValues) {
150-
const re = new RegExp(`\\n|\\t|,`)
145+
const re = new RegExp(`\\r|\\n|\\t|,`)
151146
const values = txt.split(re).filter(x => x.trim())
152147
const matches = values.map(value => props.options.find(x => props.match(x,value))).filter(x => !!x)
148+
console.log('values', values, matches)
153149
if (matches.length > 0) {
154-
matches.forEach(select)
150+
inputValue.value = ''
155151
showPopup.value = false
152+
active.value = null
153+
let newValues = Array.from(props.modelValue || [])
154+
matches.forEach(option => {
155+
if (hasOption(option)) {
156+
newValues = newValues.filter(x => x != option)
157+
} else {
158+
newValues.push(option)
159+
}
160+
})
161+
emit('update:modelValue', newValues)
156162
focusNextElement()
157163
}
158164
}
@@ -252,8 +258,6 @@ function update() {
252258
}
253259
254260
function select(option:any) {
255-
console.log('select', option)
256-
257261
inputValue.value = ''
258262
showPopup.value = false
259263

src/demo/App.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@
156156
<span class="block truncate">{{ displayName }}</span>
157157
</template>
158158
</Autocomplete>
159-
160159
<div class="mt-2 flex justify-end">
161160
<p>
162161
<b class="text-gray-500">Single:</b>
@@ -174,7 +173,7 @@
174173
placeholder="Select Contact">
175174
<template #item="{ displayName, profileUrl }">
176175
<div class="flex items-center">
177-
<Icon class="h-6 w-6 flex-shrink-0 rounded-full" :src="profileUrl" />
176+
<Icon class="h-6 w-6 flex-shrink-0 rounded-full" :src="profileUrl" loading="lazy" />
178177
<span class="ml-3 truncate">{{ displayName }}</span>
179178
</div>
180179
</template>
@@ -191,16 +190,16 @@
191190
</div>
192191

193192
<div class="mb-3">
194-
<Autocomplete id="contacts" :options="allContacts" v-model="contacts" multiple="true" label="Single Contact with Icon"
193+
<Autocomplete id="contacts" :options="allContacts" v-model="contacts" multiple label="Single Contact with Icon"
195194
:match="(x:any, value:string) => x!.displayName.toLowerCase().includes(value.toLowerCase())"
196195
placeholder="Select Contact">
197196
<template #item="{ displayName, profileUrl }">
198197
<div class="flex items-center">
199-
<Icon class="h-6 w-6 flex-shrink-0 rounded-full" :src="profileUrl" />
198+
<Icon class="h-6 w-6 flex-shrink-0 rounded-full" :src="profileUrl" loading="lazy" />
200199
<span class="ml-3 truncate">{{ displayName }}</span>
201200
</div>
202201
</template>
203-
</Autocomplete>
202+
</Autocomplete>
204203
<div class="mt-2">
205204
<div class="text-right"><b class="text-gray-500">Multiple with Icon:</b></div>
206205
<p>

0 commit comments

Comments
 (0)