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

Function _reset

src/logic/createFormControl.ts:1672–1820  ·  view source on GitHub ↗
(
    formValues,
    keepStateOptions = {},
  )

Source from the content-addressed store, hash-verified

1670 };
1671
1672 const _reset: UseFormReset<TFieldValues> = (
1673 formValues,
1674 keepStateOptions = {},
1675 ) => {
1676 const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
1677 const cloneUpdatedValues = cloneObject(updatedValues);
1678 const isEmptyResetValues = isEmptyObject(formValues);
1679 const values = cloneUpdatedValues;
1680
1681 if (!keepStateOptions.keepDefaultValues) {
1682 _defaultValues = updatedValues;
1683 }
1684
1685 if (!keepStateOptions.keepValues) {
1686 if (keepStateOptions.keepDirtyValues) {
1687 const fieldsToCheck = new Set([
1688 ..._names.mount,
1689 ...Object.keys(getDirtyFields(_defaultValues, _formValues)),
1690 ]);
1691 for (const fieldName of Array.from(fieldsToCheck)) {
1692 const isDirty = get(_formState.dirtyFields, fieldName);
1693 const existingValue = get(_formValues, fieldName);
1694 const newValue = get(values, fieldName);
1695
1696 if (isDirty && !isUndefined(existingValue)) {
1697 set(values, fieldName, existingValue);
1698 } else if (!isDirty && !isUndefined(newValue)) {
1699 setValue(fieldName as FieldPath<TFieldValues>, newValue);
1700 }
1701 }
1702 } else {
1703 if (isWeb && isUndefined(formValues)) {
1704 for (const name of _names.mount) {
1705 const field = get(_fields, name);
1706 if (field && field._f) {
1707 const fieldReference = Array.isArray(field._f.refs)
1708 ? field._f.refs[0]
1709 : field._f.ref;
1710
1711 if (isHTMLElement(fieldReference)) {
1712 const form = fieldReference.closest('form');
1713 if (form) {
1714 form.reset();
1715 break;
1716 }
1717 }
1718 }
1719 }
1720 }
1721
1722 if (keepStateOptions.keepFieldsRef) {
1723 for (const fieldName of _names.mount) {
1724 setValue(
1725 fieldName as FieldPath<TFieldValues>,
1726 get(values, fieldName),
1727 );
1728 }
1729 } else {

Callers 1

resetFunction · 0.85

Calls 5

cloneObjectFunction · 0.85
getDirtyFieldsFunction · 0.85
setValueFunction · 0.85
_getDirtyFunction · 0.85
deepEqualFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…