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): prevent default browser error UI from appearing #3984

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .changeset/quick-buses-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/select": patch
"@nextui-org/use-aria-multiselect": patch
---

Prevent default browser error UI from appearing (#3913).
55 changes: 55 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,58 @@ describe("Select with React Hook Form", () => {
expect(onSubmit).toHaveBeenCalledTimes(1);
});
});

describe("validationBehavior=native", () => {
let user: UserEvent;

beforeEach(() => {
user = userEvent.setup();
});

it("supports server validation", async () => {
const onSubmit = jest.fn();

const {getByTestId} = render(
<form onSubmit={onSubmit}>
<Select
isRequired
data-testid="select"
label="Test"
name="select"
// 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 button = getByTestId("button");
const select = getByTestId("select");
const input = document.querySelector("[name=select]");

expect(input).toHaveAttribute("required");
expect(select).not.toHaveAttribute("aria-describedby");

await user.click(button);

expect(select).toHaveAttribute("aria-describedby");
expect(input.validity.valid).toBe(false);
expect(onSubmit).toHaveBeenCalledTimes(0);

expect(document.activeElement).toBe(select);

await user.keyboard("[ArrowRight]");

expect(select).not.toHaveAttribute("aria-describedby");
expect(input.validity.valid).toBe(true);

await user.click(button);

expect(onSubmit).toHaveBeenCalledTimes(1);
});
});
8 changes: 1 addition & 7 deletions packages/components/select/src/hidden-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,18 @@ export function useHiddenSelect<T>(
inputProps: {
type: "text",
tabIndex: modality == null || state.isFocused || state.isOpen ? -1 : 0,
autoComplete,
value: [...state.selectedKeys].join(",") ?? "",
required: isRequired,
style: {fontSize: 16},
onFocus: () => triggerRef.current?.focus(),
disabled: isDisabled,
// The onChange is handled by the `select` element. This avoids the `form` with input `value`
// and no `onChange` warning.
onChange: () => {},
},
selectProps: {
name,
tabIndex: -1,
autoComplete,
// TODO: Address validation for cases where an option is selected and then deselected.
// required: validationBehavior === "native" && isRequired,
required: isRequired,
disabled: isDisabled,
size: state.collection.size,
value:
selectionMode === "multiple"
? [...state.selectedKeys].map((k) => String(k))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function useMultiSelectState<T extends {}>(props: MultiSelectProps<T>): M
if (props.selectionMode === "single") {
triggerState.close();
}

validationState.commitValidation();
},
});

Expand Down Expand Up @@ -101,7 +103,6 @@ export function useMultiSelectState<T extends {}>(props: MultiSelectProps<T>): M
if (listState.collection.size !== 0) {
setFocusStrategy(focusStrategy);
triggerState.toggle();
validationState.commitValidation();
}
},
isFocused,
Expand Down