Closed ivanbrizuela closed 1 year ago
Does the same behavior occur outside of React Hook Form?
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
OK cool just wanted to clarify! Thank you for the reproducer.
@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?
I agree with @omarsaade React Hook Form is doing the right thing. It sees a blank value and resets it to defaultValues
.
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
defaultValue
label "New York" is still displayedA 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.
I made a quick solution by removing the
defaultValues
and making the dropdown default to NY using auseEffect
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.
Describe the bug
Using
Dropdown
component and setting adefaultValue
to initialize thehookform.useForm
hook, when clicking the clear button (showClear
option enabled) the component value is cleared but it continues to display thedefaultValue
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
Dropdown
with a non-empty defaultValue (e.g. New York)clear icon
Expected behavior
Value is cleared and the placeholder text (Select a City) is displayed.
Actual behavior:
hookForm
: submitting the form shows data is cleared.hookForm
.defaultValue
keeps displaying the default value label, but no data is sent.onChange
is not triggered?