primefaces / primereact

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

Dropdown - showClear option not working with reactHookForm #4113

Closed ivanbrizuela closed 1 year ago

ivanbrizuela commented 1 year ago

Describe the bug

Using Dropdown component and setting a defaultValue to initialize the hookform.useForm hook, when clicking the clear button (showClear option enabled) the component value is cleared but it continues to display the defaultValue label, instead of the placeholder text. Therefore, the end user can't tell if the value was cleared or not.

Reproducer

https://codesandbox.io/s/primereact-demo-forked-snd6xu?file=/src/demo.js

PrimeReact version

9.2.0

React version

18.x

Language

ALL

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

Expected behavior

Value is cleared and the placeholder text (Select a City) is displayed.

Actual behavior:

melloware commented 1 year ago

Does the same behavior occur outside of React Hook Form?

ivanbrizuela commented 1 year ago

Does the same behavior occur outside of React Hook Form?

No, it does not seem to happen if React Hook Form is not involved (e.g. using state only):

https://codesandbox.io/s/primereact-demo-forked-2ijj5u?file=/src/demo.js

melloware commented 1 year ago

OK cool just wanted to clarify! Thank you for the reproducer.

omarsaade commented 1 year ago

@melloware @ivanbrizuela It seems that showClear button is resetting the form field's value even though it is supposed to clear it.

I made a quick solution by removing the defaultValues and making the dropdown default to NY using a useEffect hook on render.

Here's my approach: https://codesandbox.io/s/primereact-demo-forked-76kh1d

Is this fix enough or should we fix it by changing the package's code?

melloware commented 1 year ago

I agree with @omarsaade React Hook Form is doing the right thing. It sees a blank value and resets it to defaultValues.

ivanbrizuela commented 1 year ago

I agree with @omarsaade React Hook Form is doing the right thing. It sees a blank value and resets it to defaultValues.

@melloware, I don't think this what the hook is doing:

If a blank value is a valid input, and defaultValue is a non-blank value, always resetting blank values to defaultValue would make it impossible to select the blank value (which, as said, is a valid input).

If you see the reproducer fork: https://codesandbox.io/s/primereact-demo-forked-snd6xu?file=/src/demo.js

A default non-blank value can be cleared and submitted through the hook. React Hook Form is (correctly) not resetting the form with the defaultValue.

The problem is that the Dropdown, after the value is cleared, incorrectly displays the label of the defaultValue, which is not what the underlying form and React Hook Form will finally use on submission.

I hope this helps clarify the problem, which I think still exists.

ivanbrizuela commented 1 year ago

I made a quick solution by removing the defaultValues and making the dropdown default to NY using a useEffect hook on render.

@omarsaade as per my comment above, I don't think React Hook Form resetting the field's value: It accepts a blank value without a problem on submission. The glitch seems on what Dropdown component displays.

Not using defaultValues in your example seems to be a valid workaround, but I can't get it to work on my actual code base, which is a bit more convoluted. I will look at it again later in the week.