react-hook-form 7.15.2 → 7.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -132,38 +132,10 @@ function useFormState(props) {
132
132
  return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
133
133
  }
134
134
 
135
- var isKey = (value) => /^\w*$/.test(value);
136
-
137
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
138
-
139
- function set(object, path, value) {
140
- let index = -1;
141
- const tempPath = isKey(path) ? [path] : stringToPath(path);
142
- const length = tempPath.length;
143
- const lastIndex = length - 1;
144
- while (++index < length) {
145
- const key = tempPath[index];
146
- let newValue = value;
147
- if (index !== lastIndex) {
148
- const objValue = object[key];
149
- newValue =
150
- isObject(objValue) || Array.isArray(objValue)
151
- ? objValue
152
- : !isNaN(+tempPath[index + 1])
153
- ? []
154
- : {};
155
- }
156
- object[key] = newValue;
157
- object = object[key];
158
- }
159
- return object;
160
- }
161
-
162
135
  function useController(props) {
163
136
  const methods = useFormContext();
164
137
  const { name, control = methods.control, shouldUnregister } = props;
165
138
  const [value, setInputStateValue] = React.useState(get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)));
166
- set(control._formValues, name, value);
167
139
  const formState = useFormState({
168
140
  control: control || methods.control,
169
141
  name,
@@ -244,6 +216,33 @@ const Controller = (props) => props.render(useController(props));
244
216
  var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
245
217
  ? Object.assign(Object.assign({}, errors[name]), { types: Object.assign(Object.assign({}, (errors[name] && errors[name].types ? errors[name].types : {})), { [type]: message || true }) }) : {};
246
218
 
219
+ var isKey = (value) => /^\w*$/.test(value);
220
+
221
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
222
+
223
+ function set(object, path, value) {
224
+ let index = -1;
225
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
226
+ const length = tempPath.length;
227
+ const lastIndex = length - 1;
228
+ while (++index < length) {
229
+ const key = tempPath[index];
230
+ let newValue = value;
231
+ if (index !== lastIndex) {
232
+ const objValue = object[key];
233
+ newValue =
234
+ isObject(objValue) || Array.isArray(objValue)
235
+ ? objValue
236
+ : !isNaN(+tempPath[index + 1])
237
+ ? []
238
+ : {};
239
+ }
240
+ object[key] = newValue;
241
+ object = object[key];
242
+ }
243
+ return object;
244
+ }
245
+
247
246
  const focusFieldBy = (fields, callback, fieldsNames) => {
248
247
  for (const key of fieldsNames || Object.keys(fields)) {
249
248
  const field = get(fields, key);
@@ -1394,7 +1393,7 @@ function createFormControl(props = {}) {
1394
1393
  return isValid;
1395
1394
  };
1396
1395
  const getValues = (fieldNames) => {
1397
- const values = Object.assign(Object.assign({}, _defaultValues), _formValues);
1396
+ const values = Object.assign(Object.assign({}, _defaultValues), (_isMounted ? _formValues : {}));
1398
1397
  return isUndefined(fieldNames)
1399
1398
  ? values
1400
1399
  : isString(fieldNames)
@@ -1475,7 +1474,7 @@ function createFormControl(props = {}) {
1475
1474
  set(_fields, name, {
1476
1475
  _f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
1477
1476
  });
1478
- if (options.value) {
1477
+ if (!isUndefined(options.value)) {
1479
1478
  set(_formValues, name, options.value);
1480
1479
  }
1481
1480
  if (isBoolean(options.disabled) && field) {
@@ -1556,7 +1555,9 @@ function createFormControl(props = {}) {
1556
1555
  const reset = (formValues, keepStateOptions = {}) => {
1557
1556
  const updatedValues = formValues || _defaultValues;
1558
1557
  const values = cloneObject(updatedValues);
1559
- _formValues = values;
1558
+ if (!keepStateOptions.keepValues) {
1559
+ _formValues = props.shouldUnregister ? {} : values;
1560
+ }
1560
1561
  if (isWeb && !keepStateOptions.keepValues) {
1561
1562
  for (const name of _names.mount) {
1562
1563
  const field = get(_fields, name);
@@ -1619,7 +1620,7 @@ function createFormControl(props = {}) {
1619
1620
  isSubmitting: false,
1620
1621
  isSubmitSuccessful: false,
1621
1622
  });
1622
- _isMounted = !!keepStateOptions.keepIsValid;
1623
+ _isMounted = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
1623
1624
  };
1624
1625
  const setFocus = (name) => get(_fields, name)._f.ref.focus();
1625
1626
  const _removeFields = () => {