|
35 | 35 | 'smui-autocomplete__menu': true,
|
36 | 36 | })}
|
37 | 37 | managed
|
38 |
| - open={menuOpen} |
| 38 | + bind:open={menuOpen} |
39 | 39 | bind:anchorElement={element}
|
40 | 40 | anchor={menu$anchor}
|
41 | 41 | anchorCorner={menu$anchorCorner}
|
42 | 42 | on:SMUIList:mount={handleListAccessor}
|
| 43 | + on:SMUIMenu:closedProgrammatically={() => { |
| 44 | + if (resetTextWhenSelected) { |
| 45 | + text = ''; |
| 46 | + focus(); |
| 47 | + } else { |
| 48 | + hideMenu = true; |
| 49 | + } |
| 50 | + }} |
43 | 51 | {...prefixFilter($$restProps, 'menu$')}
|
44 | 52 | >
|
45 | 53 | <List {...prefixFilter($$restProps, 'list$')}>
|
|
162 | 170 | export let selectOnExactMatch = true;
|
163 | 171 | export let showMenuWithNoInput = true;
|
164 | 172 | export let noMatchesActionDisabled = true;
|
| 173 | + export let resetTextWhenSelected = false; |
165 | 174 | export let search: (input: string) => Promise<any[] | false> = async (
|
166 | 175 | input: string
|
167 | 176 | ) => {
|
|
202 | 211 | let matches: any[] = [];
|
203 | 212 | let focusedIndex = -1;
|
204 | 213 | let focusedItem: SMUIListItemAccessor | undefined = undefined;
|
| 214 | + let itemHasBeenSelected: boolean = false; |
| 215 | + let hideMenu: boolean = false; |
205 | 216 |
|
206 | 217 | $: menuOpen =
|
207 | 218 | focused &&
|
| 219 | + !hideMenu && |
208 | 220 | (text !== '' || showMenuWithNoInput) &&
|
209 | 221 | (loading ||
|
210 | 222 | (!combobox && !(matches.length === 1 && matches[0] === value)) ||
|
|
214 | 226 |
|
215 | 227 | let previousText: string | undefined = undefined;
|
216 | 228 | $: if (previousText !== text) {
|
| 229 | + if (!itemHasBeenSelected) { |
| 230 | + hideMenu = false; |
| 231 | + } |
217 | 232 | if (!combobox && value != null && getOptionLabel(value) !== text) {
|
218 | 233 | deselectOption(value, false);
|
219 | 234 | }
|
|
240 | 255 | loading = false;
|
241 | 256 | })();
|
242 | 257 |
|
| 258 | + if (itemHasBeenSelected) { |
| 259 | + if (resetTextWhenSelected) { |
| 260 | + text = ''; |
| 261 | + focus(); |
| 262 | + } |
| 263 | + itemHasBeenSelected = false; |
| 264 | + } |
243 | 265 | previousText = text;
|
244 | 266 | }
|
245 | 267 |
|
|
248 | 270 | // If the value changes from outside, update the text.
|
249 | 271 | text = getOptionLabel(value);
|
250 | 272 | previousValue = value;
|
| 273 | + itemHasBeenSelected = true; |
| 274 | + if (!resetTextWhenSelected) { |
| 275 | + hideMenu = true; |
| 276 | + } |
251 | 277 | } else if (combobox) {
|
252 | 278 | // If the text changes, update value if we're a combobox.
|
253 | 279 | value = text;
|
|
0 commit comments