|
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$')}>
|
|
165 | 173 | let matches: any[] = [];
|
166 | 174 | let focusedIndex = -1;
|
167 | 175 | let focusedItem: SMUIListItemAccessor | undefined = undefined;
|
168 |
| - let resetText: boolean = false; |
| 176 | + let itemHasBeenSelected: boolean = false; |
| 177 | + let hideMenu: boolean = false; |
169 | 178 |
|
170 | 179 | $: menuOpen =
|
171 | 180 | focused &&
|
| 181 | + !hideMenu && |
172 | 182 | (text !== '' || showMenuWithNoInput) &&
|
173 | 183 | (loading ||
|
174 | 184 | (!combobox && !(matches.length === 1 && matches[0] === value)) ||
|
|
178 | 188 |
|
179 | 189 | let previousText: string | undefined = undefined;
|
180 | 190 | $: if (previousText !== text) {
|
| 191 | + if (!itemHasBeenSelected) { |
| 192 | + hideMenu = false; |
| 193 | + } |
181 | 194 | if (!combobox && value != null && getOptionLabel(value) !== text) {
|
182 | 195 | deselectOption(value, false);
|
183 | 196 | }
|
|
204 | 217 | loading = false;
|
205 | 218 | })();
|
206 | 219 |
|
207 |
| - if (resetText) { |
208 |
| - text = ''; |
209 |
| - resetText = false; |
| 220 | + if (itemHasBeenSelected) { |
| 221 | + if (resetTextWhenSelected) { |
| 222 | + text = ''; |
| 223 | + focus(); |
| 224 | + } |
| 225 | + itemHasBeenSelected = false; |
210 | 226 | }
|
211 | 227 | previousText = text;
|
212 | 228 | }
|
|
216 | 232 | // If the value changes from outside, update the text.
|
217 | 233 | text = getOptionLabel(value);
|
218 | 234 | previousValue = value;
|
219 |
| - if (resetTextWhenSelected) { |
220 |
| - resetText = true; // We will set text = '' in the reactive block `$: if (previousText !== text)` |
| 235 | + itemHasBeenSelected = true; |
| 236 | + if (!resetTextWhenSelected) { |
| 237 | + hideMenu = true; |
221 | 238 | }
|
222 | 239 | } else if (combobox) {
|
223 | 240 | // If the text changes, update value if we're a combobox.
|
|
0 commit comments