Skip to content

Commit

Permalink
[EuiContextMenu] Added optional onPanelChange callback hook. (#6767)
Browse files Browse the repository at this point in the history
* Adding optional onPanelChange callback hook.

* Added changelog entry.

* Update src/components/context_menu/context_menu.tsx

Co-authored-by: Cee Chen <[email protected]>

* Update src/components/context_menu/context_menu.tsx

Co-authored-by: Bree Hall <[email protected]>

* Update src/components/context_menu/context_menu.test.tsx

Co-authored-by: Cee Chen <[email protected]>

---------

Co-authored-by: Cee Chen <[email protected]>
Co-authored-by: Bree Hall <[email protected]>
  • Loading branch information
3 people authored May 11, 2023
1 parent f610ca3 commit 4a9724d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/components/context_menu/context_menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,13 @@ describe('EuiContextMenu', () => {
});

it('allows you to click the title button to go back to the previous panel', async () => {
const onPanelChange = jest.fn();
const component = mount(
<EuiContextMenu panels={panels} initialPanelId={2} />
<EuiContextMenu
panels={panels}
initialPanelId={2}
onPanelChange={onPanelChange}
/>
);

await tick(20);
Expand All @@ -155,6 +160,10 @@ describe('EuiContextMenu', () => {
await tick(20);

expect(takeMountedSnapshot(component)).toMatchSnapshot();
expect(onPanelChange).toHaveBeenCalledWith({
panelId: 1,
direction: 'previous',
});
});
});

Expand Down
19 changes: 18 additions & 1 deletion src/components/context_menu/context_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export const SIZES = keysOf(sizeToClassNameMap);
export type EuiContextMenuProps = CommonProps &
Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {
panels?: EuiContextMenuPanelDescriptor[];
/**
* Optional callback that fires on every panel change. Passes back
* the new panel ID and whether its direction was `next` or `previous`.
*/
onPanelChange?: (panelDetails: {
panelId: EuiContextMenuPanelId;
direction?: EuiContextMenuPanelTransitionDirection;
}) => void;
initialPanelId?: EuiContextMenuPanelId;
/**
* Alters the size of the items and the title
Expand Down Expand Up @@ -221,6 +229,8 @@ export class EuiContextMenu extends Component<EuiContextMenuProps, State> {
transitionDirection: direction,
isOutgoingPanelVisible: true,
});

this.props.onPanelChange?.({ panelId, direction });
}

showNextPanel = (itemIndex?: number) => {
Expand Down Expand Up @@ -407,7 +417,14 @@ export class EuiContextMenu extends Component<EuiContextMenuProps, State> {
}

render() {
const { panels, className, initialPanelId, size, ...rest } = this.props;
const {
panels,
onPanelChange,
className,
initialPanelId,
size,
...rest
} = this.props;

const incomingPanel = this.renderPanel(this.state.incomingPanelId!, 'in');
let outgoingPanel;
Expand Down
1 change: 1 addition & 0 deletions upcoming_changelogs/6767.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added `onPanelChange` callback to `EuiContextMenu` to provide consumer access to `panelId` and `direction`.

0 comments on commit 4a9724d

Please sign in to comment.