-
-
Notifications
You must be signed in to change notification settings - Fork 218
[field] Prevent defaultValue reset on focus for uncontrolled inputs #2543
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
Conversation
commit: |
Bundle size report
|
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
if (value != null) { | ||
if (isControlled) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the tests still pass without changing this? The useControlled
hook should fire onValueChange
for both defaultValue
and value
when it gets changed (not via ref mutation though of course)
const [value, setValueUnwrapped] = useControlled({
controlled: valueProp,
default: defaultValue,
name: 'FieldControl',
state: 'value',
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do! Adjusted
Fixes #2530
Problem:
Inputs wrapped in Field.Root were incorrectly resetting to their defaultValue when focused after being programmatically changed via ref (e.g., inputRef.current.value = ""). This behavior differed from plain HTML inputs and unwrapped Input components.
Cause:
The FieldControl component was always passing a value prop to the input element, making React treat it as controlled even when only defaultValue was provided. When the DOM value was changed directly, React's internal state wasn't updated, causing re-renders to reset the input.
My Solution:
Modified FieldControl to conditionally pass either value (for controlled) or defaultValue (for uncontrolled) props to the input element, matching standard behavior.
Note: Before implementing the fix, I created some failing tests that reproduced the behaviour mentioned in the issue.