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

fix(dropdown): respect closeOnSelect prop on DropdownItem #2598

Merged
merged 10 commits into from
Aug 16, 2024
5 changes: 5 additions & 0 deletions .changeset/warm-planets-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/menu": patch
---

fix(dropdown): respect closeOnSelect prop on DropdownItem(#2290)
gtoxlili marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 36 additions & 0 deletions packages/components/dropdown/__tests__/dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,40 @@ describe("Dropdown", () => {

spy.mockRestore();
});

it("should respect closeOnSelect setting of DropdownItem", async () => {
const onOpenChange = jest.fn();
const wrapper = render(
<Dropdown onOpenChange={onOpenChange}>
<DropdownTrigger>
<Button data-testid="trigger-test">Trigger</Button>
</DropdownTrigger>
<DropdownMenu aria-label="Actions">
<DropdownItem key="new" closeOnSelect={false}>
New file
</DropdownItem>
<DropdownItem key="copy">Copy link</DropdownItem>
</DropdownMenu>
</Dropdown>,
);

let triggerButton = wrapper.getByTestId("trigger-test");

act(() => {
triggerButton.click();
});
expect(onOpenChange).toBeCalledTimes(1);

let menuItems = wrapper.getAllByRole("menuitem");

await act(async () => {
await userEvent.click(menuItems[0]);
expect(onOpenChange).toBeCalledTimes(1);
});

await act(async () => {
await userEvent.click(menuItems[1]);
expect(onOpenChange).toBeCalledTimes(2);
});
});
});
43 changes: 43 additions & 0 deletions packages/components/dropdown/stories/dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,41 @@ const Template = ({color, variant, ...args}: DropdownProps & DropdownMenuProps)
</Dropdown>
);

const ItemCloseOnSelectTemplate = ({
color,
variant,
...args
}: DropdownProps & DropdownMenuProps) => (
<>
<Dropdown {...args}>
<DropdownTrigger>
<Button>Trigger</Button>
</DropdownTrigger>
<DropdownMenu aria-label="Actions" color={color} variant={variant}>
<DropdownItem key="new">New file</DropdownItem>
<DropdownItem key="copy">Copy link</DropdownItem>
<DropdownItem key="edit">Edit file</DropdownItem>
<DropdownItem key="delete" className="text-danger" closeOnSelect={false} color="danger">
Delete file
</DropdownItem>
</DropdownMenu>
</Dropdown>
<Dropdown {...args}>
<DropdownTrigger>
<Button>Trigger</Button>
</DropdownTrigger>
<DropdownMenu aria-label="Actions" closeOnSelect={false} color={color} variant={variant}>
<DropdownItem key="new">New file</DropdownItem>
<DropdownItem key="copy">Copy link</DropdownItem>
<DropdownItem key="edit">Edit file</DropdownItem>
<DropdownItem key="delete" className="text-danger" color="danger">
Delete file
</DropdownItem>
</DropdownMenu>
</Dropdown>
</>
);

const DynamicTemplate = ({color, variant, ...args}: DropdownProps & DropdownMenuProps) => (
<Dropdown {...args}>
<DropdownTrigger>
Expand Down Expand Up @@ -740,3 +775,11 @@ export const DisableAnimation = {
disableAnimation: true,
},
};

export const ItemCloseOnSelect = {
render: ItemCloseOnSelectTemplate,

args: {
...defaultProps,
},
};
2 changes: 1 addition & 1 deletion packages/hooks/use-aria-menu/src/use-menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function useMenuItem<T>(
let onAction = isTrigger ? () => {} : props.onAction || data.onAction;
let router = useRouter();
let performAction = (e: PressEvent) => {
if (onAction) {
if (onAction && closeOnSelect) {
// @ts-ignore
onAction(key);
}
Expand Down
Loading