MCPcopy
hub / github.com/facebook/react / initInput

Function initInput

packages/react-dom-bindings/src/client/ReactDOMInput.js:206–345  ·  view source on GitHub ↗
(
  element: Element,
  value: ?string,
  defaultValue: ?string,
  checked: ?boolean,
  defaultChecked: ?boolean,
  type: ?string,
  name: ?string,
  isHydrating: boolean,
)

Source from the content-addressed store, hash-verified

204}
205
206export function initInput(
207 element: Element,
208 value: ?string,
209 defaultValue: ?string,
210 checked: ?boolean,
211 defaultChecked: ?boolean,
212 type: ?string,
213 name: ?string,
214 isHydrating: boolean,
215) {
216 const node: HTMLInputElement = (element: any);
217
218 if (
219 type != null &&
220 typeof type !== 'function' &&
221 typeof type !== 'symbol' &&
222 typeof type !== 'boolean'
223 ) {
224 if (__DEV__) {
225 checkAttributeStringCoercion(type, 'type');
226 }
227 node.type = type;
228 }
229
230 if (value != null || defaultValue != null) {
231 const isButton = type === 'submit' || type === 'reset';
232
233 // Avoid setting value attribute on submit/reset inputs as it overrides the
234 // default value provided by the browser. See: #12872
235 if (isButton && (value === undefined || value === null)) {
236 // We track the value just in case it changes type later on.
237 track((element: any));
238 return;
239 }
240
241 const defaultValueStr =
242 defaultValue != null ? toString(getToStringValue(defaultValue)) : '';
243 const initialValue =
244 value != null ? toString(getToStringValue(value)) : defaultValueStr;
245
246 // Do not assign value if it is already set. This prevents user text input
247 // from being lost during SSR hydration.
248 if (!isHydrating || enableHydrationChangeEvent) {
249 if (disableInputAttributeSyncing) {
250 // When not syncing the value attribute, the value property points
251 // directly to the React prop. Only assign it if it exists.
252 if (value != null) {
253 // Always assign on buttons so that it is possible to assign an
254 // empty string to clear button text.
255 //
256 // Otherwise, do not re-assign the value property if is empty. This
257 // potentially avoids a DOM write and prevents Firefox (~60.0.1) from
258 // prematurely marking required inputs as invalid. Equality is compared
259 // to the current value in case the browser provided value is not an
260 // empty string.
261 if (isButton || toString(getToStringValue(value)) !== node.value) {
262 node.value = toString(getToStringValue(value));
263 }

Callers 2

setInitialPropertiesFunction · 0.90
hydratePropertiesFunction · 0.90

Calls 4

trackFunction · 0.90
toStringFunction · 0.90
getToStringValueFunction · 0.90

Tested by

no test coverage detected