Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiContextMenuPanel] Fix popover toggle focus restoration issues and remove need for watchedItemProps #5880

Merged
merged 10 commits into from
May 10, 2022
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') => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

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