Skip to content

Commit

Permalink
fix(select): improve form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
chirokas committed Nov 5, 2024
1 parent b33b892 commit b477f74
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,33 @@ describe("Select with React Hook Form", () => {
expect(onSubmit).toHaveBeenCalledTimes(1);
});
});

describe("validationBehavior=native", () => {
it("should not submit form when required field is empty", async () => {
const onSubmit = jest.fn();

const {getByTestId} = render(
<form onSubmit={onSubmit}>
<Select
isRequired
data-testid="select"
label="Test"
// validationBehavior="native"
>
<SelectItem key="one">One</SelectItem>
<SelectItem key="two">Two</SelectItem>
<SelectItem key="three">Three</SelectItem>
</Select>
<button data-testid="button" type="submit">
Submit
</button>
</form>,
);

const user = userEvent.setup();

await user.click(getByTestId("button"));

expect(onSubmit).toHaveBeenCalledTimes(0);
});
});
1 change: 0 additions & 1 deletion packages/components/select/src/hidden-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export function useHiddenSelect<T>(
// required: validationBehavior === "native" && isRequired,
required: isRequired,
disabled: isDisabled,
size: state.collection.size,
value:
selectionMode === "multiple"
? [...state.selectedKeys].map((k) => String(k))
Expand Down

0 comments on commit b477f74

Please sign in to comment.