react-hook-form 7.19.0-next.0 → 7.19.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/CHANGELOG.md +4 -0
- package/README.md +12 -0
- package/dist/controller.d.ts +2 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +37 -33
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types/controller.d.ts +10 -10
- package/dist/types/utils.d.ts +0 -3
- package/dist/useController.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -177,6 +177,33 @@ function generateWatchOutput(names, _names, formValues, isGlobal) {
|
|
177
177
|
return formValues;
|
178
178
|
}
|
179
179
|
|
180
|
+
var isKey = (value) => /^\w*$/.test(value);
|
181
|
+
|
182
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
183
|
+
|
184
|
+
function set(object, path, value) {
|
185
|
+
let index = -1;
|
186
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
187
|
+
const length = tempPath.length;
|
188
|
+
const lastIndex = length - 1;
|
189
|
+
while (++index < length) {
|
190
|
+
const key = tempPath[index];
|
191
|
+
let newValue = value;
|
192
|
+
if (index !== lastIndex) {
|
193
|
+
const objValue = object[key];
|
194
|
+
newValue =
|
195
|
+
isObject(objValue) || Array.isArray(objValue)
|
196
|
+
? objValue
|
197
|
+
: !isNaN(+tempPath[index + 1])
|
198
|
+
? []
|
199
|
+
: {};
|
200
|
+
}
|
201
|
+
object[key] = newValue;
|
202
|
+
object = object[key];
|
203
|
+
}
|
204
|
+
return object;
|
205
|
+
}
|
206
|
+
|
180
207
|
function useWatch(props) {
|
181
208
|
const methods = useFormContext();
|
182
209
|
const { control = methods.control, name, defaultValue, disabled, } = props || {};
|
@@ -187,8 +214,10 @@ function useWatch(props) {
|
|
187
214
|
subject: control._subjects.watch,
|
188
215
|
callback: (formState) => {
|
189
216
|
if (shouldSubscribeByName(_name.current, formState.name)) {
|
190
|
-
const fieldValues = generateWatchOutput(_name.current, control._names, control._formValues);
|
191
|
-
updateValue(isObject(fieldValues)
|
217
|
+
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
218
|
+
updateValue(isObject(fieldValues) &&
|
219
|
+
!(isString(_name.current) &&
|
220
|
+
get(control._fields, _name.current, {})._f)
|
192
221
|
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
193
222
|
? [...fieldValues]
|
194
223
|
: fieldValues);
|
@@ -232,7 +261,7 @@ function useController(props) {
|
|
232
261
|
if (isNameInFieldArray(control._names.array, name)
|
233
262
|
? _shouldUnregisterField && !control._stateFlags.action
|
234
263
|
: _shouldUnregisterField) {
|
235
|
-
control.unregister(name);
|
264
|
+
control.unregister(name, { keepDefaultValue: true });
|
236
265
|
}
|
237
266
|
else {
|
238
267
|
updateMounted(name, false);
|
@@ -287,33 +316,6 @@ const Controller = (props) => props.render(useController(props));
|
|
287
316
|
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
288
317
|
? Object.assign(Object.assign({}, errors[name]), { types: Object.assign(Object.assign({}, (errors[name] && errors[name].types ? errors[name].types : {})), { [type]: message || true }) }) : {};
|
289
318
|
|
290
|
-
var isKey = (value) => /^\w*$/.test(value);
|
291
|
-
|
292
|
-
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
293
|
-
|
294
|
-
function set(object, path, value) {
|
295
|
-
let index = -1;
|
296
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
297
|
-
const length = tempPath.length;
|
298
|
-
const lastIndex = length - 1;
|
299
|
-
while (++index < length) {
|
300
|
-
const key = tempPath[index];
|
301
|
-
let newValue = value;
|
302
|
-
if (index !== lastIndex) {
|
303
|
-
const objValue = object[key];
|
304
|
-
newValue =
|
305
|
-
isObject(objValue) || Array.isArray(objValue)
|
306
|
-
? objValue
|
307
|
-
: !isNaN(+tempPath[index + 1])
|
308
|
-
? []
|
309
|
-
: {};
|
310
|
-
}
|
311
|
-
object[key] = newValue;
|
312
|
-
object = object[key];
|
313
|
-
}
|
314
|
-
return object;
|
315
|
-
}
|
316
|
-
|
317
319
|
const focusFieldBy = (fields, callback, fieldsNames) => {
|
318
320
|
for (const key of fieldsNames || Object.keys(fields)) {
|
319
321
|
const field = get(fields, key);
|
@@ -1452,7 +1454,7 @@ function createFormControl(props = {}) {
|
|
1452
1454
|
const field = get(_fields, fieldName);
|
1453
1455
|
return await executeBuildInValidation(field && field._f ? { [fieldName]: field } : field);
|
1454
1456
|
}))).every(Boolean);
|
1455
|
-
_updateValid();
|
1457
|
+
!(!validationResult && !_formState.isValid) && _updateValid();
|
1456
1458
|
}
|
1457
1459
|
else {
|
1458
1460
|
validationResult = isValid = await executeBuildInValidation(_fields);
|
@@ -1526,6 +1528,7 @@ function createFormControl(props = {}) {
|
|
1526
1528
|
});
|
1527
1529
|
_names.mount.add(name);
|
1528
1530
|
!isUndefined(options.value) &&
|
1531
|
+
!options.disabled &&
|
1529
1532
|
set(_formValues, name, get(_formValues, name, options.value));
|
1530
1533
|
field
|
1531
1534
|
? isBoolean(options.disabled) &&
|
@@ -1582,7 +1585,9 @@ function createFormControl(props = {}) {
|
|
1582
1585
|
e.persist && e.persist();
|
1583
1586
|
}
|
1584
1587
|
let hasNoPromiseError = true;
|
1585
|
-
let fieldValues =
|
1588
|
+
let fieldValues = _options.shouldUnregister
|
1589
|
+
? cloneObject(_formValues)
|
1590
|
+
: Object.assign({}, _formValues);
|
1586
1591
|
_subjects.state.next({
|
1587
1592
|
isSubmitting: true,
|
1588
1593
|
});
|
@@ -1840,7 +1845,6 @@ function useForm(props = {}) {
|
|
1840
1845
|
}
|
1841
1846
|
control._removeUnmounted();
|
1842
1847
|
});
|
1843
|
-
React.useEffect(() => () => Object.values(control._subjects).forEach((subject) => subject.unsubscribe()), [control]);
|
1844
1848
|
_formControl.current.formState = getProxyFormState(formState, control._proxyFormState);
|
1845
1849
|
return _formControl.current;
|
1846
1850
|
}
|