Skip to content

Commit

Permalink
fix(input-otp): form validation hook missing behavior prop (#4417)
Browse files Browse the repository at this point in the history
* fix(input-otp): form validation hook missing behavior

* test(input-otp): isRequired form test

* chore(changeset): fixed input-otp isRequired error message
  • Loading branch information
Peterl561 authored Dec 22, 2024
1 parent f7e1b17 commit 93c9df1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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

0 comments on commit 93c9df1

Please sign in to comment.