nextui: [BUG] - react-hook-form
NextUI Version
2.2.9
Describe the bug
`
<Input type=“text” placeholder=“0” classNames={{ input: “text-right”, }} radius=“none” onValueChange={(value: string) => inputChange(value, “amount”)} size=“sm” {…register(“amount”, { required: true, })} /> `
It uses React-hook-form and next ui input, and is initialized when the value is entered with setValue.
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
“”
Expected behavior
“”
Screenshots or Videos
No response
Operating System Version
masOs
Browser
Chrome
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 16
- Comments: 34 (3 by maintainers)
I also got it working using the Controller component. For anyone else needing an example, with working default values, values, and error messages, here you go:
Usage example:
The ref to the input is not exposed. The forward ref is passed to the top most div element.
Dear maintainers, please either fix the documentation (because it no longer reflects reality): https://nextui.org/docs/components/input#controlled
or: have it fixed so that ref is passed down again to the input.
Current issues that I’ve discovered so far, when trying to pass
{...register("field")}:Reproduction sandbox: https://codesandbox.io/p/devbox/nextui-v2-react-hook-form-forked-rm9dz8
This used to work in
@nextui-org/react@2.1.3, as you can see in the original sandbox: https://codesandbox.io/p/devbox/nextui-v2-react-hook-form-xnwxt8 Updating to@nextui-org/react@2.1.4breaks the integration with react-hook-form.I couldn’t make this work in my local Next.js 14 app router project, even when downgrading to
@nextui-org/react@2.1.3. Reproduction sandbox Next.js 14: https://codesandbox.io/p/devbox/nextjs-nextui-react-hook-form-ck4vdjI had the same issue after upgrading to the latest Nextui version. For me works wrapping the Nextui Input inside react-hook-form Controller.
Did you try using this way @kamzata https://react-hook-form.com/get-started#IntegratingwithUIlibraries ?
Hey guys, thanks for sharing your issues, it seems the
refis not working properly in theInputcomponent therefore thedefaultValuesare not working, as a workaround please use theControllercomponent fromreact-hook-formhere’s an example:https://stackblitz.com/edit/vitejs-vite-xdeajz?file=src%2FApp.tsx
This does fix it but it sort of defeats the purpose of using react hook form and embracing uncontrolled components.
@christo9090
Here is my
UncontrolledTextarea.tsx(tweaked from NextUItextarea.tsx):It does what I need it to do, but I didn’t test in in all the edge cases. I have an
UncontrolledInput.tsxtoo if you want.For me, wrapping the NextUI Input with a Controller fixes the default value problem, however, the errorMessage is not shown! The input is red, the errorMessage is passed, but it is not displayed… I am also using RHF and Input version 2.1.16. Downgrading to Input version 2.1.9 solves the issue for now.
If I replace the NextUI Input Field with a standard HTML input element it works right away.
Same here! I’m using NextUI 2.2.9 and RHF 7.48.2 and it’s like the NextUI’s input field ignores some props like
onChange. Furthermore, NextUI’s input field doesn’t show anymore the default values passed on RHF. It worked before upgrading.I tested on Chrome and Firefox on MacOS and Linux.