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(select): close select by pressing selector button #3374

Merged
merged 5 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-students-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

fixed select closing issue with selector button (#3276)
59 changes: 46 additions & 13 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,12 @@ describe("Select", () => {
console.log(JSON.stringify(Object.fromEntries(formData)));
}}
>
<Select data-testid="select" label="test select" name="select" size="sm">
<Select
data-testid="select"
defaultSelectedKeys={["foo"]}
label="test select"
name="select"
>
<SelectItem key="foo">foo</SelectItem>
<SelectItem key="bar">bar</SelectItem>
</Select>
Expand All @@ -574,6 +579,14 @@ describe("Select", () => {
</form>,
);

const submitButton = wrapper.getByTestId("submit-button");

await act(async () => {
await user.click(submitButton);
});

expect(logSpy).toHaveBeenCalledWith(JSON.stringify({select: "foo"}));

const select = wrapper.getByTestId("select");

expect(select).not.toBeNull();
Expand All @@ -582,39 +595,59 @@ describe("Select", () => {
await user.click(select);
});

let listbox = wrapper.getByRole("listbox");
const listbox = wrapper.getByRole("listbox");

expect(listbox).toBeTruthy();

let listboxItems = wrapper.getAllByRole("option");
const listboxItems = wrapper.getAllByRole("option");

expect(listboxItems.length).toBe(2);

await act(async () => {
await user.click(listboxItems[1]);
await user.click(listboxItems[0]);
});

let submitButton = wrapper.getByTestId("submit-button");

await act(async () => {
await user.click(submitButton);
});

expect(logSpy).toHaveBeenCalledWith(JSON.stringify({select: "bar"}));
expect(logSpy).toHaveBeenCalledWith(JSON.stringify({select: ""}));
});

await act(async () => {
await user.click(select);
});
it("should close listbox by clicking selector button again", async () => {
const wrapper = render(
<Select aria-label="Favorite Animal" data-testid="select" label="Favorite Animal">
<SelectItem key="penguin" value="penguin">
Penguin
</SelectItem>
<SelectItem key="zebra" value="zebra">
Zebra
</SelectItem>
<SelectItem key="shark" value="shark">
Shark
</SelectItem>
</Select>,
);

const select = wrapper.getByTestId("select");

expect(select).not.toBeNull();

// open the select listbox by clicking selector button
await act(async () => {
await user.click(listboxItems[1]);
await userEvent.click(select);
});

// assert that the select listbox is open
expect(select).toHaveAttribute("aria-expanded", "true");

// open the select listbox by clicking selector button
await act(async () => {
await user.click(submitButton);
await userEvent.click(select);
});

expect(logSpy).toHaveBeenCalledWith(JSON.stringify({select: ""}));
// assert that the select listbox is closed
expect(select).toHaveAttribute("aria-expanded", "false");
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/components/select/src/use-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
: slotsProps.popoverProps?.offset,
shouldCloseOnInteractOutside: popoverProps?.shouldCloseOnInteractOutside
? popoverProps.shouldCloseOnInteractOutside
: (element: Element) => ariaShouldCloseOnInteractOutside(element, triggerRef, state),
: (element: Element) => ariaShouldCloseOnInteractOutside(element, domRef, state),
} as PopoverProps;
},
[
Expand All @@ -544,7 +544,7 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
"data-open": dataAttr(state.isOpen),
className: slots.selectorIcon({class: classNames?.selectorIcon}),
}),
[slots, classNames?.selectorIcon, state?.isOpen],
[slots, classNames?.selectorIcon, state.isOpen],
);

const getInnerWrapperProps: PropGetter = useCallback(
Expand Down