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

Refactor some controlled component stuff #26573

Merged
merged 14 commits into from
Apr 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/react-dom-bindings/src/client/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ export function postInitInput(
return;
}

const initialValue = toString(node._wrapperState.initialValue);
const defaultValue =
props.defaultValue != null
? toString(getToStringValue(props.defaultValue))
: '';
const initialValue =
props.value != null
? toString(getToStringValue(props.value))
: defaultValue;

// Do not assign value if it is already set. This prevents user text input
// from being lost during SSR hydration.
Expand Down Expand Up @@ -280,9 +287,8 @@ export function postInitInput(
if (disableInputAttributeSyncing) {
// When not syncing the value attribute, assign the value attribute
// directly from the defaultValue React property (when present)
const defaultValue = getToStringValue(props.defaultValue);
if (defaultValue != null) {
node.defaultValue = toString(defaultValue);
if (props.defaultValue != null) {
node.defaultValue = defaultValue;
}
} else {
// Otherwise, the value attribute is synchronized to the property,
Expand All @@ -302,13 +308,20 @@ export function postInitInput(
node.name = '';
}

const defaultChecked =
props.checked != null ? props.checked : props.defaultChecked;
Comment on lines +247 to +248
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems a little confusing that your defaultValue and defaultChecked are not parallel (the latter here incorporates props.checked) but I guess it's fine

const initialChecked =
typeof defaultChecked !== 'function' &&
typeof defaultChecked !== 'symbol' &&
!!defaultChecked;

// The checked property never gets assigned. It must be manually set.
// We don't want to do this when hydrating so that existing user input isn't
// modified
// TODO: I'm pretty sure this is a bug because initialValueTracking won't be
// correct for the hydration case then.
if (!isHydrating) {
node.checked = !!node._wrapperState.initialChecked;
node.checked = !!initialChecked;
}

if (disableInputAttributeSyncing) {
Expand All @@ -327,7 +340,7 @@ export function postInitInput(
// 2. The defaultChecked React property when present
// 3. Otherwise, false
node.defaultChecked = !node.defaultChecked;
node.defaultChecked = !!node._wrapperState.initialChecked;
node.defaultChecked = !!initialChecked;
}

if (name !== '') {
Expand Down