Skip to content

Commit

Permalink
Close the Popover when an item is clicked (#1124)
Browse files Browse the repository at this point in the history
* Close the Popover when an item is clicked

* Add changeset
  • Loading branch information
Robin Métral authored Aug 16, 2021
1 parent 33086c3 commit 785878d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eight-papayas-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/circuit-ui': patch
---

Changed the Popover behavior to close when one of the items is clicked.
12 changes: 12 additions & 0 deletions packages/circuit-ui/components/Popover/Popover.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ describe('Popover', () => {
expect(baseProps.onToggle).toHaveBeenCalledTimes(1);
});

it('should close the popover when clicking a popover item', () => {
const { getAllByRole } = renderPopover(baseProps);

const popoverItems = getAllByRole('menuitem');

act(() => {
userEvent.click(popoverItems[0]);
});

expect(baseProps.onToggle).toHaveBeenCalledTimes(1);
});

it('should move focus to the first popover item after opening', () => {
const { getAllByRole, rerender } = renderPopover({
...baseProps,
Expand Down
19 changes: 18 additions & 1 deletion packages/circuit-ui/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,16 @@ export const Popover = ({
}
};

const handlePopoverItemClick = (
event: ClickEvent,
onClick: BaseProps['onClick'],
) => {
if (onClick) {
onClick(event);
onToggle(false);
}
};

return (
<Fragment>
<TriggerElement ref={triggerEl}>
Expand Down Expand Up @@ -419,7 +429,14 @@ export const Popover = ({
isDivider(action) ? (
<Hr css={dividerStyles} key={index} />
) : (
<PopoverItem key={index} {...action} {...focusProps} />
<PopoverItem
key={index}
{...action}
{...focusProps}
onClick={(event) =>
handlePopoverItemClick(event, action.onClick)
}
/>
),
)}
</PopoverMenu>
Expand Down

0 comments on commit 785878d

Please sign in to comment.