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

[EuiContextMenuPanelDescriptor] Add initialFocusedItemIndex support #4223

Merged
merged 7 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `initialFocusedItemIndex` support to `EuiContextMenuPanelDescriptor` ([#4223](https://github.com/elastic/eui/pull/4223))

**Bug fixes**

- Fixed a condition in `EuiInMemoryTable` to avoid mistaken assignment of `sortName` ([#4138](https://github.com/elastic/eui/pull/4138))
Expand Down
1 change: 1 addition & 0 deletions src-docs/src/views/context_menu/context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default () => {
},
{
id: 1,
initialFocusedItemIndex: 1,
title: 'Nest panels',
items: [
{
Expand Down
10 changes: 6 additions & 4 deletions src/components/context_menu/context_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface EuiContextMenuPanelDescriptor {
items?: EuiContextMenuPanelItemDescriptor[];
content?: ReactNode;
width?: number;
initialFocusedItemIndex?: number;
}

export type EuiContextMenuProps = CommonProps &
Expand Down Expand Up @@ -227,9 +228,10 @@ export class EuiContextMenu extends Component<EuiContextMenuProps, State> {

if (nextPanelId) {
if (this.state.isUsingKeyboardToNavigate) {
this.setState({
focusedItemIndex: 0,
});
this.setState(({ idToPanelMap }) => ({
focusedItemIndex:
idToPanelMap[nextPanelId].initialFocusedItemIndex ?? 0,
}));
}

this.showPanel(nextPanelId, 'next');
Expand Down Expand Up @@ -388,7 +390,7 @@ export class EuiContextMenu extends Component<EuiContextMenuProps, State> {
initialFocusedItemIndex={
this.state.isUsingKeyboardToNavigate
? this.state.focusedItemIndex
: undefined
: panel.initialFocusedItemIndex
}
onUseKeyboardToNavigate={this.onUseKeyboardToNavigate}
showNextPanel={this.showNextPanel}
Expand Down
10 changes: 10 additions & 0 deletions src/components/context_menu/context_menu_panel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ describe('EuiContextMenuPanel', () => {
document.activeElement
);
});

it('sets focus on the panel when set to `-1`', async () => {
const component = mount(
<EuiContextMenuPanel items={items} initialFocusedItemIndex={-1} />
);

await tick(20);

expect(component.getDOMNode()).toBe(document.activeElement);
});
});

describe('onUseKeyboardToNavigate', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/components/context_menu/context_menu_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ export class EuiContextMenuPanel extends Component<Props, State> {
return;
}

// `focusedItemIndex={-1}` specifies that the panel itself should be focused.
// This should only be used when the panel does not have `item`s
// and preventing autofocus is desired, which is an uncommon case.
if (this.panel && this.state.focusedItemIndex === -1) {
this.panel.focus();
return;
}

// If there aren't any items then this is probably a form or something.
if (!this.state.menuItems.length) {
// If we've already focused on something inside the panel, everything's fine.
Expand Down