-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
InputControl: avoid uncontrolled to controlled warning #47250
Conversation
The test failures don't seem related to this PR, especially since they seem to happen across many other PRs as well. |
// Check if this was broken and at what point | ||
// in particular on commit actions, when `min` is not `undefined` | ||
export const Uncontrolled: ComponentStory< typeof NumberControl > = ( { |
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.
Please ignore the code comment, it's a left over from a copy-paste.
The whole Storybook example will be removed before merging, I'm just leaving around for ease of testing
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.
While reviewing this PR, I also briefly thought about whether there are more appropriate places to put the fallback. For example, at the prop destructure point or the useInputControlStateReducer()
call point. But I still think the place we currently have it is the most appropriate, because the string fallback is only necessary where our value interfaces with React <input>
👍
3dfdc96
to
6dbc4c0
Compare
What?
Make sure that the underlying
<input />
inInputControl
doesn't switch inadvertently from uncontrolled to controlled mode.Why?
While working on #45982 and on #47246, I noticed that the "uncontrolled to controlled" warning was being logged even when it shouldn't have.
After some investigation with @mirka, we realized that
InputControl
always passes an explicitvalue
prop to the underlying<input />
element, because of the component's internal state reducer.This causes the "uncontrolled to controlled" warning to be logged when value of the
input
goes from empty to any value.I will also note that this error doesn't probably get logged when using the editor because in most cases a fallback to an empty string is already provided by the component using
InputControl
(or any related component).How?
Since React interprets an
undefined
value as a sign of an uncontrolled component, the fix here is to pass an empty string to<input />
instead ofundefined
(this is achieved via the?? ''
code).Testing Instructions
About the error message:
packages/components/src/input-control/input-field.tsx
filepackages/components/src/input-control/input-field.tsx
fileCheck that the component behaves as expected:
InputControl
and its derivatives (NumberControl
,UnitControl
,RangeControl
, ....)Screenshots or screencast
Screencast of the error message logged to console that happens without the fix from this PR:
number-control-controlled-warning.mp4