MCPcopy
hub / github.com/react-hook-form/react-hook-form / updateValidAndValue

Function updateValidAndValue

src/logic/createFormControl.ts:364–419  ·  view source on GitHub ↗
(
    name: InternalFieldName,
    shouldSkipSetValueAs: boolean,
    value?: unknown,
    ref?: Ref,
  )

Source from the content-addressed store, hash-verified

362 };
363
364 const updateValidAndValue = (
365 name: InternalFieldName,
366 shouldSkipSetValueAs: boolean,
367 value?: unknown,
368 ref?: Ref,
369 ) => {
370 const field: Field = get(_fields, name);
371
372 if (field) {
373 if (hasExplicitNullIntermediate(name)) {
374 return;
375 }
376
377 const wasUnsetInFormValues = isUndefined(get(_formValues, name));
378 const defaultValue = get(
379 _formValues,
380 name,
381 isUndefined(value) ? get(_defaultValues, name) : value,
382 );
383
384 isUndefined(defaultValue) ||
385 (ref && (ref as HTMLInputElement).defaultChecked) ||
386 shouldSkipSetValueAs
387 ? set(
388 _formValues,
389 name,
390 shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f),
391 )
392 : setFieldValue(name, defaultValue);
393
394 if (_state.mount && !_state.action) {
395 _setValid();
396
397 if (
398 wasUnsetInFormValues &&
399 _formState.isDirty &&
400 (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty)
401 ) {
402 const isDirty = _getDirty();
403 if (!isDirty) {
404 _formState.isDirty = false;
405 _subjects.state.next({ ..._formState });
406 }
407 }
408
409 if (
410 props.shouldUnregister &&
411 wasUnsetInFormValues &&
412 !isUndefined(get(_formValues, name)) &&
413 isWatched(name, _names)
414 ) {
415 _state.watch = true;
416 }
417 }
418 }
419 };
420
421 const updateTouchAndDirty = (

Callers 1

registerFunction · 0.85

Calls 5

getFieldValueFunction · 0.85
setFieldValueFunction · 0.85
_setValidFunction · 0.85
_getDirtyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…