Skip to content

Commit fe6a3a8

Browse files
edcarrollmcosta74
authored andcommitted
fix(dropdown): Fixed error on keyboard navigation of empty dropdown
1 parent 35b7d86 commit fe6a3a8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/modules/dropdown/directives/dropdown-menu.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,39 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
180180

181181
switch (e.keyCode) {
182182
// Escape : close the entire dropdown.
183-
case KeyCode.Escape:
183+
case KeyCode.Escape: {
184184
this._service.setOpenState(false);
185185
break;
186+
}
186187
// Down : select the next item below the current one, or the 1st if none selected.
187188
case KeyCode.Down:
188189
// Up : select the next item above the current one, or the 1st if none selected.
189-
case KeyCode.Up:
190+
case KeyCode.Up: {
190191
this.selectedItems.pop();
191192
this.selectedItems.push(selectedContainer.updateSelection(selected, e.keyCode));
192193
// Prevent default regardless of whether we are in an input, to stop jumping to the start or end of the query string.
193194
e.preventDefault();
194195
break;
196+
}
195197
// Enter : if the item doesn't contain a nested dropdown, 'click' it. Otherwise, fall through to 'Right' action.
196-
case KeyCode.Enter:
198+
case KeyCode.Enter: {
197199
if (selected && !selected.hasChildDropdown) {
198200
selected.performClick();
199201
break;
200202
}
203+
}
201204
// falls through
202205
// Right : if the selected item contains a nested dropdown, open the dropdown & select the 1st item.
203-
case KeyCode.Right:
206+
case KeyCode.Right: {
204207
if (selected && selected.hasChildDropdown) {
205208
selected.childDropdownMenu.service.setOpenState(true);
206209

207210
this.selectedItems.push(selected.childDropdownMenu.updateSelection(selected, e.keyCode));
208211
}
209212
break;
213+
}
210214
// Left : if the selected item is in a nested dropdown, close it and select the containing item.
211-
case KeyCode.Left:
215+
case KeyCode.Left: {
212216
if (this.selectedItems.length >= 2) {
213217
this.selectedItems.pop();
214218
const [selectedParent] = this.selectedItems.slice(-1);
@@ -217,6 +221,7 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
217221
selectedParent.isSelected = true;
218222
}
219223
break;
224+
}
220225
}
221226
}
222227
}
@@ -271,9 +276,10 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
271276
if (newSelection) {
272277
// Set the selected status on the newly selected item.
273278
newSelection.isSelected = true;
274-
}
275279

276-
this.scrollToItem(newSelection);
280+
// Set the current scroll position to the location of the newly selected item.
281+
this.scrollToItem(newSelection);
282+
}
277283

278284
return newSelection;
279285
}

0 commit comments

Comments
 (0)