diff --git a/.changeset/silent-jars-grow.md b/.changeset/silent-jars-grow.md new file mode 100644 index 0000000000..fd977ba035 --- /dev/null +++ b/.changeset/silent-jars-grow.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/input-otp": patch +--- + +Change ensures that the input value does not accept any disallowed characters when the value prop contains them (#4329) diff --git a/packages/components/input-otp/__tests__/input-otp.test.tsx b/packages/components/input-otp/__tests__/input-otp.test.tsx index 36f967f183..c1ad664d05 100644 --- a/packages/components/input-otp/__tests__/input-otp.test.tsx +++ b/packages/components/input-otp/__tests__/input-otp.test.tsx @@ -2,7 +2,7 @@ import "@testing-library/jest-dom"; import * as React from "react"; import {render, renderHook, screen} from "@testing-library/react"; -import {useForm} from "react-hook-form"; +import {Controller, useForm} from "react-hook-form"; import userEvent, {UserEvent} from "@testing-library/user-event"; import {InputOtp} from "../src"; @@ -250,4 +250,38 @@ describe("InputOtp with react-hook-form", () => { expect(onSubmit).toHaveBeenCalledTimes(1); }); + + it("should work correctly wiht react-hook-form controller", async () => { + const {result} = renderHook(() => + useForm({ + defaultValues: { + withController: "", + }, + }), + ); + + const {control} = result.current; + + render( +
, + ); + + const inputOtp = screen.getByTestId("input-otp"); + const input = inputOtp.querySelector("input"); + + if (!input) { + throw new Error("Input not found"); + } + + await user.click(input); + await user.type(input, "1nj23aa4"); + + expect(input).toHaveAttribute("value", "1234"); + }); }); diff --git a/packages/components/input-otp/src/use-input-otp.ts b/packages/components/input-otp/src/use-input-otp.ts index 2604ba6a63..0206c4565d 100644 --- a/packages/components/input-otp/src/use-input-otp.ts +++ b/packages/components/input-otp/src/use-input-otp.ts @@ -119,6 +119,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) { pasteTransformer, containerClassName, noScriptCSSFallback, + onChange, ...otherProps } = props; @@ -209,6 +210,14 @@ export function useInputOtp(originalProps: UseInputOtpProps) { }), filterDOMProps(props), ), + onChange: (e: React.ChangeEvent