Skip to content

Commit

Permalink
Merge branch 'canary' into pr/3000
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Aug 20, 2024
2 parents 3600022 + 4f8ae50 commit c02fa15
Show file tree
Hide file tree
Showing 7 changed files with 14,677 additions and 11,568 deletions.
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/dropdown": patch
---

fix(dropdown): respect closeOnSelect prop on DropdownItem (#2290)
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.x
20.x
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
},
"engines": {
"node": ">=16.x",
"pnpm": ">=8.x"
"pnpm": ">=9.x"
},
"packageManager": "pnpm@8.7.0"
"packageManager": "pnpm@9.6.0"
}
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 @@ -795,4 +795,40 @@ describe("Keyboard interactions", () => {

logSpy.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);
});
});
});
12 changes: 11 additions & 1 deletion packages/components/dropdown/src/use-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,17 @@ export function useDropdown(props: UseDropdownProps) {
menuProps,
closeOnSelect,
...mergeProps(props, {
onAction: () => onMenuAction(props?.closeOnSelect),
onAction: (key: any) => {
// @ts-ignore
const item = props?.children?.find((item) => item.key === key);

if (item?.props?.closeOnSelect === false) {
onMenuAction(false);

return;
}
onMenuAction(props?.closeOnSelect);
},
onClose: state.close,
}),
} as MenuProps;
Expand Down
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 @@ -153,6 +153,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 @@ -739,3 +774,11 @@ export const DisableAnimation = {
disableAnimation: true,
},
};

export const ItemCloseOnSelect = {
render: ItemCloseOnSelectTemplate,

args: {
...defaultProps,
},
};
Loading

0 comments on commit c02fa15

Please sign in to comment.