Skip to content

Commit

Permalink
[EuiContextMenuPanelDescriptor] Add initialFocusedItemIndex support (
Browse files Browse the repository at this point in the history
…elastic#4223)

* respect initialFocusedItemIndex in EuiContextMenuPanelDescriptor

* add initialFocusedItemIndex example

* nullish coalescing operator

* CL

* -1 focuses the panel
  • Loading branch information
thompsongl authored and cchaos committed Nov 4, 2020
1 parent a97f1cb commit 17d6823
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
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

0 comments on commit 17d6823

Please sign in to comment.