Skip to content

Commit

Permalink
[!!!] Restore up/down key navigation behavior
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
cee-chen committed May 5, 2022
1 parent 40dc5c4 commit 4b397b0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/components/context_menu/context_menu_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ export class EuiContextMenuPanel extends Component<Props, State> {
}
};

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

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

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

this.setState({
focusedItemIndex: nextFocusedItemIndex,
});
this.setState({ focusedItemIndex: nextFocusedItemIndex });
this.state.menuItems[nextFocusedItemIndex]?.focus();
};

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

case cascadingMenuKeys.ARROW_UP:
event.preventDefault();
this.incrementFocusedItemIndex(-1);
this.focusMenuItem('up');

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

case cascadingMenuKeys.ARROW_DOWN:
event.preventDefault();
this.incrementFocusedItemIndex(1);
this.focusMenuItem('down');

if (this.props.onUseKeyboardToNavigate) {
this.props.onUseKeyboardToNavigate();
Expand Down

0 comments on commit 4b397b0

Please sign in to comment.