-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(select): prevent default browser error UI from appearing #4281
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1179,6 +1179,181 @@ describe("validation", () => { | |
| user = userEvent.setup(); | ||
| }); | ||
|
|
||
| describe("validationBehavior=native", () => { | ||
| it("supports isRequired", async () => { | ||
| const {getByTestId} = render( | ||
| <Form data-testid="form"> | ||
| <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> | ||
| </Form>, | ||
| ); | ||
|
|
||
| const select = getByTestId("select"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=select]"); | ||
|
|
||
| expect(input).toHaveAttribute("required"); | ||
| expect(input?.validity.valid).toBe(false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to check |
||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
|
||
| act(() => { | ||
| (getByTestId("form") as HTMLFormElement).checkValidity(); | ||
| }); | ||
|
|
||
| expect(input?.validity.valid).toBe(false); | ||
| expect(select).toHaveAttribute("aria-describedby"); | ||
| expect(document.getElementById(select.getAttribute("aria-describedby")!)).toHaveTextContent( | ||
| "Constraints not satisfied", | ||
| ); | ||
|
|
||
| await user.click(select); | ||
| await user.click(document.querySelectorAll("[role='option']")[0]); | ||
| expect(input?.validity.valid).toBe(true); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| }); | ||
|
|
||
| it("supports validate function", async () => { | ||
| const {getByTestId} = render( | ||
| <form> | ||
| <Select | ||
| data-testid="select" | ||
| defaultSelectedKeys={["two"]} | ||
| label="Test" | ||
| name="select" | ||
| validate={(value) => (value.includes("two") ? "Invalid value" : null)} | ||
| validationBehavior="native" | ||
| > | ||
| <SelectItem key="one">One</SelectItem> | ||
| <SelectItem key="two">Two</SelectItem> | ||
| <SelectItem key="three">Three</SelectItem> | ||
| </Select> | ||
| <button data-testid="submit" type="submit"> | ||
| Submit | ||
| </button> | ||
| </form>, | ||
| ); | ||
|
|
||
| const select = getByTestId("select"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=select]"); | ||
|
|
||
| expect(input?.validity.valid).toBe(false); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
|
||
| await user.click(getByTestId("submit")); | ||
| expect(select).toHaveAttribute("aria-describedby"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check for |
||
| expect(document.getElementById(select.getAttribute("aria-describedby")!)).toHaveTextContent( | ||
| "Invalid value", | ||
| ); | ||
|
|
||
| await user.click(select); | ||
| await user.click(document.querySelectorAll("[role='option']")[0]); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| }); | ||
|
|
||
| it("supports server validation", async () => { | ||
| function Test() { | ||
| const [serverErrors, setServerErrors] = React.useState({}); | ||
| const onSubmit = (e) => { | ||
| e.preventDefault(); | ||
| setServerErrors({ | ||
| select: "Invalid value.", | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <Form validationErrors={serverErrors} 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="submit" type="submit"> | ||
| Submit | ||
| </button> | ||
| </Form> | ||
| ); | ||
| } | ||
|
|
||
| const {getByTestId} = render(<Test />); | ||
|
|
||
| const button = getByTestId("submit"); | ||
| const select = getByTestId("select"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=select]"); | ||
|
|
||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
|
||
| await user.click(button); | ||
| expect(select).toHaveAttribute("aria-describedby"); | ||
| expect(document.getElementById(select.getAttribute("aria-describedby")!)).toHaveTextContent( | ||
| "Invalid value.", | ||
| ); | ||
| expect(input?.validity.valid).toBe(false); | ||
|
|
||
| await user.click(select); | ||
| await user.click(document.querySelectorAll("[role='option']")[0]); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| expect(input?.validity.valid).toBe(true); | ||
| }); | ||
|
|
||
| it("clears validation on reset", async () => { | ||
| const {getByTestId} = render( | ||
| <Form data-testid="form"> | ||
| <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="reset" type="reset"> | ||
| Reset | ||
| </button> | ||
| </Form>, | ||
| ); | ||
|
|
||
| const select = getByTestId("select"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=select]"); | ||
|
|
||
| expect(input).toHaveAttribute("required"); | ||
| expect(input?.validity.valid).toBe(false); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
|
||
| act(() => { | ||
| (getByTestId("form") as HTMLFormElement).checkValidity(); | ||
| }); | ||
|
|
||
| expect(select).toHaveAttribute("aria-describedby"); | ||
| expect(document.getElementById(select.getAttribute("aria-describedby")!)).toHaveTextContent( | ||
| "Constraints not satisfied", | ||
| ); | ||
|
|
||
| await user.click(select); | ||
| await user.click(document.querySelectorAll("[role='option']")[0]); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
|
||
| await user.click(getByTestId("reset")); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
|
Comment on lines
+1350
to
+1353
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't actually tell us that the reset worked does it |
||
| }); | ||
| }); | ||
|
|
||
| describe("validationBehavior=aria", () => { | ||
| it("supports isRequired", async () => { | ||
| function FormRender() { | ||
|
|
@@ -1223,7 +1398,7 @@ describe("validation", () => { | |
| const {getByTestId} = render(<FormRender />); | ||
|
|
||
| const select = getByTestId("select"); | ||
| const input = document.querySelector("input"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=animal]"); | ||
|
|
||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| const button = getByTestId("button"); | ||
|
|
@@ -1255,6 +1430,7 @@ describe("validation", () => { | |
| data-testid="select" | ||
| defaultSelectedKeys={["penguin"]} | ||
| label="Favorite Animal" | ||
| name="animal" | ||
| validate={(v) => (v.includes("penguin") ? "Invalid value" : null)} | ||
| validationBehavior="aria" | ||
| > | ||
|
|
@@ -1269,7 +1445,7 @@ describe("validation", () => { | |
| ); | ||
|
|
||
| const select = getByTestId("select"); | ||
| const input = document.querySelector("input"); | ||
| const input = document.querySelector<HTMLSelectElement>("[name=animal]"); | ||
| const button = getByTestId("button"); | ||
|
|
||
| expect(select).toHaveAttribute("aria-describedby"); | ||
|
|
@@ -1292,5 +1468,28 @@ describe("validation", () => { | |
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| expect(select).not.toHaveAttribute("aria-invalid"); | ||
| }); | ||
|
|
||
| it("supports server validation", async () => { | ||
| let {getByTestId} = render( | ||
| <Form validationErrors={{select: "Invalid value"}}> | ||
| <Select data-testid="select" label="Test" name="select" validationBehavior="aria"> | ||
| <SelectItem key="one">One</SelectItem> | ||
| <SelectItem key="two">Two</SelectItem> | ||
| <SelectItem key="three">Three</SelectItem> | ||
| </Select> | ||
| </Form>, | ||
| ); | ||
|
|
||
| const select = getByTestId("select"); | ||
|
|
||
| expect(select).toHaveAttribute("aria-describedby"); | ||
| expect(document.getElementById(select.getAttribute("aria-describedby")!)).toHaveTextContent( | ||
| "Invalid value", | ||
| ); | ||
|
|
||
| await user.click(select); | ||
| await user.click(document.querySelectorAll("[role='option']")[0]); | ||
| expect(select).not.toHaveAttribute("aria-describedby"); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,19 +99,16 @@ export function useHiddenSelect<T>( | |
| ["data-a11y-ignore"]: "aria-hidden-focus", | ||
| }, | ||
| inputProps: { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably delete the hidden input element, see adobe/react-spectrum#7200 |
||
| ...commonProps, | ||
| type: "text", | ||
| tabIndex: modality == null || state.isFocused || state.isOpen ? -1 : 0, | ||
| value: [...state.selectedKeys].join(",") ?? "", | ||
| style: {fontSize: 16}, | ||
| onFocus: () => triggerRef.current?.focus(), | ||
| onChange: () => {}, // The onChange is handled by the `select` element | ||
| disabled: isDisabled, | ||
| }, | ||
| selectProps: { | ||
| ...commonProps, | ||
| name, | ||
| tabIndex: -1, | ||
| size: state.collection.size, | ||
| value: | ||
| selectionMode === "multiple" | ||
| ? [...state.selectedKeys].map((k) => String(k)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,8 @@ export function useMultiSelectState<T extends object>({ | |
| if (props.selectionMode === "single") { | ||
| triggerState.close(); | ||
| } | ||
|
|
||
| validationState.commitValidation(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential reference error: The call to To resolve this issue, consider initializing |
||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -124,7 +126,6 @@ export function useMultiSelectState<T extends object>({ | |
|
|
||
| setFocusStrategy(focusStrategy); | ||
| triggerState.toggle(); | ||
| validationState.commitValidation(); | ||
| }, | ||
| isFocused, | ||
| setFocused, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Select is a
<button>and input is a<select>, please consider renaming the variables in the these tests