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

Dropdown: Expose focusInputRef for React Hook Form #3662

Closed
melloware opened this issue Nov 16, 2022 · 1 comment · Fixed by #3663
Closed

Dropdown: Expose focusInputRef for React Hook Form #3662

melloware opened this issue Nov 16, 2022 · 1 comment · Fixed by #3663
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@melloware
Copy link
Member

Describe the bug

Similar to #3645

When using Dropdown component with React Hook Form, on submit, if the dropdown is the only field to fail validation then React Hook Form attempts to put a focus on the Dropdown field but cannot.

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 Dropdown component (form can have other fields/inputs too).
  2. Make sure the PrimeReact Dropdown component has some kind of validation - for example, field is required.
  3. Fill in all fields, except for the PrimeReact Dropdown (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 melloware added the Type: Bug Issue contains a defect related to a specific component. label Nov 16, 2022
@melloware melloware self-assigned this Nov 16, 2022
@melloware melloware added this to the 8.7.3 milestone Nov 16, 2022
@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 16, 2022
melloware added a commit to melloware/primereact that referenced this issue Nov 16, 2022
@melloware melloware removed the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 16, 2022
@melloware
Copy link
Member Author

melloware commented Nov 16, 2022

Exposed focusInputRef so we can do this...

<Controller
	name="make"
	control={form.control}
	rules={{ required: 'Make is required.' }}
	render={({ field, fieldState }) => (
		<>
			<label htmlFor={field.name} className={classNames({ 'p-error': errors.make })}>Make*</label>
			<Dropdown
				id={field.name}
				options={manufacturers}
				className={classNames({ 'p-invalid': fieldState.error })}
				{...field}
				focusInputRef={field.ref}
			/>
			{getFormErrorMessage(fieldState, field.name)}
		</>
	)}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant