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): unset form value after unselecting an item #3157

Merged
merged 2 commits into from
Jun 7, 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/thirty-timers-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

set empty string instead of undefined for unsetting value (#3156)
69 changes: 69 additions & 0 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,75 @@ describe("Select", () => {

expect(select).toHaveTextContent("Select an animal");
});

it("should unset form value", async () => {
const logSpy = jest.spyOn(console, "log");

const user = userEvent.setup();

const wrapper = render(
<form
className="w-full max-w-xs items-end flex flex-col gap-4"
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);

/* eslint-disable no-console */
console.log(JSON.stringify(Object.fromEntries(formData)));
}}
>
<Select data-testid="select" label="test select" name="select" size="sm">
<SelectItem key="foo">foo</SelectItem>
<SelectItem key="bar">bar</SelectItem>
</Select>
<button data-testid="submit-button" type="submit">
Submit
</button>
</form>,
);

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

expect(select).not.toBeNull();

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

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

expect(listbox).toBeTruthy();

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

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

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

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

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

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

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

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

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

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

describe("Select with React Hook Form", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/select/src/hidden-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function useHiddenSelect<T>(
value:
selectionMode === "multiple"
? [...state.selectedKeys].map((k) => String(k))
: [...state.selectedKeys][0],
: [...state.selectedKeys][0] ?? "",
multiple: selectionMode === "multiple",
onChange: (e: React.ChangeEvent<HTMLSelectElement>) => {
state.setSelectedKeys(e.target.value);
Expand Down
Loading