Skip to content

Commit da4e918

Browse files
committed
Fix autocomplete click + edit behavior
1 parent c4ec768 commit da4e918

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/components/Autocomplete.vue

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
<div class="relative mt-1">
66

7-
<input ref="txtInput" :id="`${id}-text`"
8-
type="text" role="combobox" aria-controls="options" aria-expanded="false" autocomplete="off" spellcheck="false"
7+
<input ref="txtInput" :id="`${id}-text`"
8+
type="text" role="combobox" aria-controls="options" aria-expanded="false" autocomplete="off" spellcheck="false"
99
v-model="inputValue"
1010
:class="cls"
1111
:placeholder="multiple || !modelValue ? placeholder : ''"
12-
@focus="update"
13-
@keydown="keyDown"
14-
@keyup="keyUp"
15-
@click="update"
12+
:readonly="!multiple && !!modelValue && !expanded"
13+
@keydown="keyDown"
14+
@keyup="keyUp"
15+
@click="onInputClick"
1616
@paste="onPaste"
1717
:required="false"
1818
v-bind="$attrs">
@@ -39,7 +39,7 @@
3939
</span>
4040
</li>
4141
</ul>
42-
<div v-else-if="!multiple && modelValue" @keydown="keyDown" @click="toggle(!expanded)" class="h-8 -mt-8 ml-3 pt-0.5">
42+
<div v-else-if="!multiple && modelValue" @keydown="keyDown" class="h-8 -mt-8 ml-3 pt-0.5 pointer-events-none">
4343
<slot v-if="typeof modelValue === 'string'" name="item" v-bind="{ key:modelValue, value:modelValue }"></slot>
4444
<slot v-else name="item" v-bind="modelValue"></slot>
4545
</div>
@@ -240,13 +240,25 @@ function toggle(expand:boolean) {
240240
expanded.value = expand
241241
if (!expand)
242242
return
243-
update()
243+
refresh()
244244
txtInput.value?.focus()
245245
}
246246
247+
function onInputClick() {
248+
// When in single-select mode with a value, toggle the dropdown
249+
if (!props.multiple && props.modelValue) {
250+
expanded.value = !expanded.value
251+
if (expanded.value) {
252+
refresh()
253+
}
254+
} else {
255+
update()
256+
}
257+
}
258+
247259
function update() {
248260
expanded.value = true
249-
refresh()
261+
refresh()
250262
}
251263
252264
function select(option:any) {

0 commit comments

Comments
 (0)