primefaces / primereact

The Most Complete React UI Component Library
https://primereact.org
MIT License
6.61k stars 1k forks source link

Password: Submit with React Hook Form #3645

Closed cbaum86 closed 1 year ago

cbaum86 commented 1 year ago

Describe the bug

When using Password component with React Hook Form, on submit, if the password is the only field to fail validation then React Hook Form attempts to put a focus on the password field but cannot and throws an error.

Standard input text and password fields work as expected, as does a password input wrapped in a div as generated by Prime React.

Reproducer

https://codesandbox.io/s/blissful-butterfly-pc6xhk

PrimeReact version

8.7.2

React version

18.x

Language

ES6

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

  1. Create a react hook form with at least one PrimeReact Password component (form can have other fields/inputs too).
  2. Make sure the PrimeReact Password component has some kind of validation - for example, field is required.
  3. Fill in all fields, except for the PrimeReact Password (or otherwise make this field invalid)
  4. Submit the form to see e.focus / elm.focus error.

Expected behavior

With all other fields types, standard html text or password, PrimeReact InputText then when submitting the form, if there are errors the first field with an error is focused on. I would expect that, if the PrimeReact Password component is the first invalid field then the form should focus on this field too.

melloware commented 1 year ago

I will investigate this.

melloware commented 1 year ago

I fixed your Code Sandbox: https://codesandbox.io/s/white-glade-i26wqu?file=/src/App.js:4459-4626

The issue is Password has an inputRef the main ref points to the wrapper DIV of the password.

              <Password
                {...field}
                inputRef={field.ref}
                type="password"
                placeholder="password input"
              />
hugomf commented 1 year ago

could you please update your example code using this fix on the react-form-hook example? it took me a while to figure it out: https://www.primefaces.org/primereact/reacthookform/

melloware commented 1 year ago

@hugomf I updated the showcase for the next major deployment with a Password React Hook Example.