Skip to content

Commit 4fa5e2a

Browse files
author
edu-rosado
committed
Add resetTextWhenSelected to Autocomplete
1 parent 9431254 commit 4fa5e2a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

packages/autocomplete/src/Autocomplete.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
export let selectOnExactMatch = true;
125125
export let showMenuWithNoInput = true;
126126
export let noMatchesActionDisabled = true;
127+
export let resetTextWhenSelected = false;
127128
export let search: (input: string) => Promise<any[] | false> = async (
128129
input: string
129130
) => {
@@ -164,6 +165,7 @@
164165
let matches: any[] = [];
165166
let focusedIndex = -1;
166167
let focusedItem: SMUIListItemAccessor | undefined = undefined;
168+
let resetText: boolean = false;
167169
168170
$: menuOpen =
169171
focused &&
@@ -202,6 +204,10 @@
202204
loading = false;
203205
})();
204206
207+
if (resetText) {
208+
text = '';
209+
resetText = false;
210+
}
205211
previousText = text;
206212
}
207213
@@ -210,6 +216,9 @@
210216
// If the value changes from outside, update the text.
211217
text = getOptionLabel(value);
212218
previousValue = value;
219+
if (resetTextWhenSelected) {
220+
resetText = true; // We will set text = '' in the reactive block `$: if (previousText !== text)`
221+
}
213222
} else if (combobox) {
214223
// If the text changes, update value if we're a combobox.
215224
value = text;

0 commit comments

Comments
 (0)