Skip to content

Commit 4b397b0

Browse files
committed
[!!!] Restore up/down key navigation behavior
- rename `incrementFocusedItemIndex` to `focusMenuItem` and change args to be a bit more human readable - instead of having the previous `updateFocus` handle up/down nav, we can simply call `.focus()` from within this method, and arrow navigation works as before - note `?.focus();` - this is important to keep as users can start mashing up/down before `tabbable` is done running and there are any menu items to focus - no specific E2E tests for this, tests should simply not be failing
1 parent 40dc5c4 commit 4b397b0

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/components/context_menu/context_menu_panel.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ export class EuiContextMenuPanel extends Component<Props, State> {
128128
}
129129
};
130130

131-
incrementFocusedItemIndex = (amount: number) => {
131+
focusMenuItem = (direction: 'up' | 'down') => {
132+
const indexOffset = direction === 'up' ? -1 : 1;
132133
let nextFocusedItemIndex;
133134

134135
if (this.state.focusedItemIndex === undefined) {
135136
// If this is the beginning of the user's keyboard navigation of the menu, then we'll focus
136137
// either the first or last item.
137-
nextFocusedItemIndex = amount < 0 ? this.state.menuItems.length - 1 : 0;
138+
nextFocusedItemIndex =
139+
direction === 'up' ? this.state.menuItems.length - 1 : 0;
138140
} else {
139-
nextFocusedItemIndex = this.state.focusedItemIndex + amount;
141+
nextFocusedItemIndex = this.state.focusedItemIndex + indexOffset;
140142

141143
if (nextFocusedItemIndex < 0) {
142144
nextFocusedItemIndex = this.state.menuItems.length - 1;
@@ -145,9 +147,8 @@ export class EuiContextMenuPanel extends Component<Props, State> {
145147
}
146148
}
147149

148-
this.setState({
149-
focusedItemIndex: nextFocusedItemIndex,
150-
});
150+
this.setState({ focusedItemIndex: nextFocusedItemIndex });
151+
this.state.menuItems[nextFocusedItemIndex]?.focus();
151152
};
152153

153154
onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
@@ -198,7 +199,7 @@ export class EuiContextMenuPanel extends Component<Props, State> {
198199

199200
case cascadingMenuKeys.ARROW_UP:
200201
event.preventDefault();
201-
this.incrementFocusedItemIndex(-1);
202+
this.focusMenuItem('up');
202203

203204
if (this.props.onUseKeyboardToNavigate) {
204205
this.props.onUseKeyboardToNavigate();
@@ -207,7 +208,7 @@ export class EuiContextMenuPanel extends Component<Props, State> {
207208

208209
case cascadingMenuKeys.ARROW_DOWN:
209210
event.preventDefault();
210-
this.incrementFocusedItemIndex(1);
211+
this.focusMenuItem('down');
211212

212213
if (this.props.onUseKeyboardToNavigate) {
213214
this.props.onUseKeyboardToNavigate();

0 commit comments

Comments
 (0)