Skip to content

Commit 09cd127

Browse files
author
edu-rosado
committed
Autocomplete: fix issues with the menu (open/close)
1 parent 4fa5e2a commit 09cd127

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

packages/autocomplete/src/Autocomplete.svelte

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@
3535
'smui-autocomplete__menu': true,
3636
})}
3737
managed
38-
open={menuOpen}
38+
bind:open={menuOpen}
3939
bind:anchorElement={element}
4040
anchor={menu$anchor}
4141
anchorCorner={menu$anchorCorner}
4242
on:SMUIList:mount={handleListAccessor}
43+
on:SMUIMenu:closedProgrammatically={() => {
44+
if (resetTextWhenSelected) {
45+
text = '';
46+
focus();
47+
} else {
48+
hideMenu = true;
49+
}
50+
}}
4351
{...prefixFilter($$restProps, 'menu$')}
4452
>
4553
<List {...prefixFilter($$restProps, 'list$')}>
@@ -165,10 +173,12 @@
165173
let matches: any[] = [];
166174
let focusedIndex = -1;
167175
let focusedItem: SMUIListItemAccessor | undefined = undefined;
168-
let resetText: boolean = false;
176+
let itemHasBeenSelected: boolean = false;
177+
let hideMenu: boolean = false;
169178
170179
$: menuOpen =
171180
focused &&
181+
!hideMenu &&
172182
(text !== '' || showMenuWithNoInput) &&
173183
(loading ||
174184
(!combobox && !(matches.length === 1 && matches[0] === value)) ||
@@ -178,6 +188,9 @@
178188
179189
let previousText: string | undefined = undefined;
180190
$: if (previousText !== text) {
191+
if (!itemHasBeenSelected) {
192+
hideMenu = false;
193+
}
181194
if (!combobox && value != null && getOptionLabel(value) !== text) {
182195
deselectOption(value, false);
183196
}
@@ -204,9 +217,12 @@
204217
loading = false;
205218
})();
206219
207-
if (resetText) {
208-
text = '';
209-
resetText = false;
220+
if (itemHasBeenSelected) {
221+
if (resetTextWhenSelected) {
222+
text = '';
223+
focus();
224+
}
225+
itemHasBeenSelected = false;
210226
}
211227
previousText = text;
212228
}
@@ -216,8 +232,9 @@
216232
// If the value changes from outside, update the text.
217233
text = getOptionLabel(value);
218234
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;
221238
}
222239
} else if (combobox) {
223240
// If the text changes, update value if we're a combobox.

packages/menu/src/Menu.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@
7272
listAccessor.getAttributeFromElementIndex(index, attr),
7373
elementContainsClass: (element, className) =>
7474
element.classList.contains(className),
75-
closeSurface: (skipRestoreFocus) =>
76-
menuSurfaceAccessor.closeProgrammatic(skipRestoreFocus),
75+
closeSurface: (skipRestoreFocus) => {
76+
menuSurfaceAccessor.closeProgrammatic(skipRestoreFocus);
77+
dispatch(getElement(), 'SMUIMenu:closedProgrammatically', {});
78+
},
7779
getElementIndex: (element) =>
7880
listAccessor
7981
.getOrderedList()

0 commit comments

Comments
 (0)