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(input-otp): form validation hook missing behavior prop #4417

Merged
merged 3 commits into from
Dec 22, 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/nasty-dolls-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/input-otp": patch
---

fixed isRequired not showing error when validationBehavior=native is not explicitly set
40 changes: 40 additions & 0 deletions packages/components/input-otp/__tests__/input-otp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from "react";
import {render, renderHook, screen} from "@testing-library/react";
import {Controller, useForm} from "react-hook-form";
import userEvent, {UserEvent} from "@testing-library/user-event";
import {Form} from "@nextui-org/form";

import {InputOtp} from "../src";

Expand Down Expand Up @@ -185,6 +186,45 @@ describe("InputOtp Component", () => {
});
});

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

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

it("supports isRequired when validationBehavior=native", async () => {
const {getByTestId} = render(
<Form validationBehavior="native">
<InputOtp isRequired data-testid="base" length={4} />
<button data-testid="submit" type="submit" />
<button data-testid="reset" type="reset" />
</Form>,
);

const inputOtpBase = getByTestId("base");

expect(inputOtpBase).toHaveAttribute("aria-required", "true");
expect(inputOtpBase).not.toHaveAttribute("data-invalid");

const submitButton = getByTestId("submit");

await user.click(submitButton);

expect(inputOtpBase).toHaveAttribute("data-invalid", "true");
const errorMessage = document.querySelector("[data-slot='error-message']");

expect(errorMessage).toBeInTheDocument();

const resetButton = getByTestId("reset");

await user.click(resetButton);

expect(inputOtpBase).not.toHaveAttribute("data-invalid");
expect(errorMessage).not.toBeInTheDocument();
});
});

describe("InputOtp with react-hook-form", () => {
let user: UserEvent;

Expand Down
2 changes: 1 addition & 1 deletion packages/components/input-otp/src/use-input-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
});

useFormReset(inputRef, value, setValue);
useFormValidation(props, validationState, inputRef);
useFormValidation({...props, validationBehavior}, validationState, inputRef);

const {
isInvalid: isAriaInvalid,
Expand Down
Loading