react-hook-form 7.0.4 → 7.0.7
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 +8 -3
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +47 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/form.d.ts +80 -2
- package/package.json +10 -11
- package/dist/__tests__/logic/appendErrors.test.d.ts +0 -1
- package/dist/__tests__/logic/focusFieldBy.test.d.ts +0 -1
- package/dist/__tests__/logic/generateId.test.d.ts +0 -1
- package/dist/__tests__/logic/getCheckboxValue.test.d.ts +0 -1
- package/dist/__tests__/logic/getControllerValue.test.d.ts +0 -1
- package/dist/__tests__/logic/getFieldValue.test.d.ts +0 -1
- package/dist/__tests__/logic/getFields.test.d.ts +0 -1
- package/dist/__tests__/logic/getFieldsValues.test.d.ts +0 -1
- package/dist/__tests__/logic/getMultipleSelectValue.test.d.ts +0 -1
- package/dist/__tests__/logic/getNodeParentName.test.d.ts +0 -1
- package/dist/__tests__/logic/getRadioValue.test.d.ts +0 -1
- package/dist/__tests__/logic/getValidateError.test.d.ts +0 -1
- package/dist/__tests__/logic/getValueAndMessage.test.d.ts +0 -1
- package/dist/__tests__/logic/isErrorStateChanged.test.d.ts +0 -1
- package/dist/__tests__/logic/isNameInFieldArray.test.d.ts +0 -1
- package/dist/__tests__/logic/setFieldArrayDirtyFields.test.d.ts +0 -1
- package/dist/__tests__/logic/skipValidation.test.d.ts +0 -1
- package/dist/__tests__/logic/validateField.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/append.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/focus.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/insert.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/move.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/prepend.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/remove.test.d.ts +0 -1
- package/dist/__tests__/useFieldArray/swap.test.d.ts +0 -1
- package/dist/__tests__/useForm/clearErrors.test.d.ts +0 -1
- package/dist/__tests__/useForm/formState.test.d.ts +0 -1
- package/dist/__tests__/useForm/getValues.test.d.ts +0 -1
- package/dist/__tests__/useForm/handleSubmit.test.d.ts +0 -1
- package/dist/__tests__/useForm/register.test.d.ts +0 -1
- package/dist/__tests__/useForm/reset.test.d.ts +0 -1
- package/dist/__tests__/useForm/setError.test.d.ts +0 -1
- package/dist/__tests__/useForm/setValue.test.d.ts +0 -1
- package/dist/__tests__/useForm/trigger.test.d.ts +0 -1
- package/dist/__tests__/useForm/unregister.test.d.ts +0 -1
- package/dist/__tests__/useForm/watch.test.d.ts +0 -1
- package/dist/__tests__/utils/Subject.test.d.ts +0 -1
- package/dist/__tests__/utils/compact.test.d.ts +0 -1
- package/dist/__tests__/utils/deepEqual.test.d.ts +0 -1
- package/dist/__tests__/utils/deepMerge.test.d.ts +0 -1
- package/dist/__tests__/utils/fillBooleanArray.test.d.ts +0 -1
- package/dist/__tests__/utils/fillEmptyArray.test.d.ts +0 -1
- package/dist/__tests__/utils/get.test.d.ts +0 -1
- package/dist/__tests__/utils/insert.test.d.ts +0 -1
- package/dist/__tests__/utils/isBoolean.test.d.ts +0 -1
- package/dist/__tests__/utils/isCheckBoxInput.test.d.ts +0 -1
- package/dist/__tests__/utils/isEmptyObject.test.d.ts +0 -1
- package/dist/__tests__/utils/isFileInput.test.d.ts +0 -1
- package/dist/__tests__/utils/isFunction.test.d.ts +0 -1
- package/dist/__tests__/utils/isHTMLElement.test.d.ts +0 -1
- package/dist/__tests__/utils/isKey.test.d.ts +0 -1
- package/dist/__tests__/utils/isMessage.test.d.ts +0 -1
- package/dist/__tests__/utils/isMultipleSelect.test.d.ts +0 -1
- package/dist/__tests__/utils/isNullOrUndefined.test.d.ts +0 -1
- package/dist/__tests__/utils/isObject.test.d.ts +0 -1
- package/dist/__tests__/utils/isPrimitive.test.d.ts +0 -1
- package/dist/__tests__/utils/isRadioInput.test.d.ts +0 -1
- package/dist/__tests__/utils/isRadioOrCheckbox.test.d.ts +0 -1
- package/dist/__tests__/utils/isRegex.test.d.ts +0 -1
- package/dist/__tests__/utils/isSelectInput.test.d.ts +0 -1
- package/dist/__tests__/utils/isString.test.d.ts +0 -1
- package/dist/__tests__/utils/isUndefined.test.d.ts +0 -1
- package/dist/__tests__/utils/move.test.d.ts +0 -1
- package/dist/__tests__/utils/omit.test.d.ts +0 -1
- package/dist/__tests__/utils/prepend.test.d.ts +0 -1
- package/dist/__tests__/utils/remove.test.d.ts +0 -1
- package/dist/__tests__/utils/set.test.d.ts +0 -1
- package/dist/__tests__/utils/stringToPath.test.d.ts +0 -1
- package/dist/__tests__/utils/swap.test.d.ts +0 -1
- package/dist/__tests__/utils/unset.test.d.ts +0 -1
- package/dist/__tests__/utils/validationModeChecker.test.d.ts +0 -1
- package/dist/index.cjs.development.js +0 -1676
- package/dist/index.cjs.development.js.map +0 -1
- package/dist/index.cjs.production.min.js +0 -2
- package/dist/index.cjs.production.min.js.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.umd.development.js +0 -1680
- package/dist/index.umd.development.js.map +0 -1
- package/dist/index.umd.production.min.js +0 -2
- package/dist/index.umd.production.min.js.map +0 -1
- package/dist/logic/isErrorStateChanged.d.ts +0 -9
|
@@ -1,1676 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var React = require('react');
|
|
6
|
-
|
|
7
|
-
function _interopNamespace(e) {
|
|
8
|
-
if (e && e.__esModule) return e;
|
|
9
|
-
var n = Object.create(null);
|
|
10
|
-
if (e) {
|
|
11
|
-
Object.keys(e).forEach(function (k) {
|
|
12
|
-
if (k !== 'default') {
|
|
13
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function () {
|
|
17
|
-
return e[k];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
n['default'] = e;
|
|
24
|
-
return Object.freeze(n);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
28
|
-
|
|
29
|
-
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
30
|
-
|
|
31
|
-
var isNullOrUndefined = (value) => value == null;
|
|
32
|
-
|
|
33
|
-
const isObjectType = (value) => typeof value === 'object';
|
|
34
|
-
var isObject = (value) => !isNullOrUndefined(value) &&
|
|
35
|
-
!Array.isArray(value) &&
|
|
36
|
-
isObjectType(value) &&
|
|
37
|
-
!(value instanceof Date);
|
|
38
|
-
|
|
39
|
-
var getControllerValue = (event) => isObject(event) && event.target
|
|
40
|
-
? isCheckBoxInput(event.target)
|
|
41
|
-
? event.target.checked
|
|
42
|
-
: event.target.value
|
|
43
|
-
: event;
|
|
44
|
-
|
|
45
|
-
var getNodeParentName = (name) => name.substring(0, name.search(/.\d/)) || name;
|
|
46
|
-
|
|
47
|
-
var isNameInFieldArray = (names, name) => [...names].some((current) => getNodeParentName(name) === current);
|
|
48
|
-
|
|
49
|
-
var compact = (value) => value.filter(Boolean);
|
|
50
|
-
|
|
51
|
-
var isUndefined = (val) => val === undefined;
|
|
52
|
-
|
|
53
|
-
var get = (obj = {}, path, defaultValue) => {
|
|
54
|
-
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => (isNullOrUndefined(result) ? result : result[key]), obj);
|
|
55
|
-
return isUndefined(result) || result === obj
|
|
56
|
-
? isUndefined(obj[path])
|
|
57
|
-
? defaultValue
|
|
58
|
-
: obj[path]
|
|
59
|
-
: result;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const EVENTS = {
|
|
63
|
-
BLUR: 'blur',
|
|
64
|
-
CHANGE: 'change',
|
|
65
|
-
};
|
|
66
|
-
const VALIDATION_MODE = {
|
|
67
|
-
onBlur: 'onBlur',
|
|
68
|
-
onChange: 'onChange',
|
|
69
|
-
onSubmit: 'onSubmit',
|
|
70
|
-
onTouched: 'onTouched',
|
|
71
|
-
all: 'all',
|
|
72
|
-
};
|
|
73
|
-
const SELECT = 'select';
|
|
74
|
-
const UNDEFINED = 'undefined';
|
|
75
|
-
const INPUT_VALIDATION_RULES = {
|
|
76
|
-
max: 'max',
|
|
77
|
-
min: 'min',
|
|
78
|
-
maxLength: 'maxLength',
|
|
79
|
-
minLength: 'minLength',
|
|
80
|
-
pattern: 'pattern',
|
|
81
|
-
required: 'required',
|
|
82
|
-
validate: 'validate',
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
var omit = (source, key) => {
|
|
86
|
-
const copy = Object.assign({}, source);
|
|
87
|
-
delete copy[key];
|
|
88
|
-
return copy;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const FormContext = React__namespace.createContext(null);
|
|
92
|
-
FormContext.displayName = 'RHFContext';
|
|
93
|
-
const useFormContext = () => React__namespace.useContext(FormContext);
|
|
94
|
-
const FormProvider = (props) => (React__namespace.createElement(FormContext.Provider, { value: omit(props, 'children') }, props.children));
|
|
95
|
-
|
|
96
|
-
var getProxyFormState = (isProxyEnabled, formState, readFormStateRef, localReadFormStateRef, isRoot = true) => isProxyEnabled
|
|
97
|
-
? new Proxy(formState, {
|
|
98
|
-
get: (obj, prop) => {
|
|
99
|
-
if (prop in obj) {
|
|
100
|
-
if (readFormStateRef.current[prop] !== VALIDATION_MODE.all) {
|
|
101
|
-
readFormStateRef.current[prop] = isRoot
|
|
102
|
-
? VALIDATION_MODE.all
|
|
103
|
-
: true;
|
|
104
|
-
}
|
|
105
|
-
localReadFormStateRef &&
|
|
106
|
-
(localReadFormStateRef.current[prop] = true);
|
|
107
|
-
return obj[prop];
|
|
108
|
-
}
|
|
109
|
-
return undefined;
|
|
110
|
-
},
|
|
111
|
-
})
|
|
112
|
-
: formState;
|
|
113
|
-
|
|
114
|
-
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
115
|
-
|
|
116
|
-
var shouldRenderFormState = (formState, readFormStateRef, isRoot) => isEmptyObject(formState) ||
|
|
117
|
-
Object.keys(formState).length >= Object.keys(readFormStateRef).length ||
|
|
118
|
-
Object.keys(formState).find((key) => readFormStateRef[key] ===
|
|
119
|
-
(isRoot ? VALIDATION_MODE.all : true));
|
|
120
|
-
|
|
121
|
-
var isWeb = typeof window !== UNDEFINED &&
|
|
122
|
-
typeof window.HTMLElement !== UNDEFINED &&
|
|
123
|
-
typeof document !== UNDEFINED;
|
|
124
|
-
|
|
125
|
-
const isProxyEnabled = isWeb ? 'Proxy' in window : typeof Proxy !== UNDEFINED;
|
|
126
|
-
|
|
127
|
-
function useFormState(props) {
|
|
128
|
-
const methods = useFormContext();
|
|
129
|
-
const { formStateRef, formStateSubjectRef, readFormStateRef } = (props && props.control) || methods.control;
|
|
130
|
-
const [formState, updateFormState] = React__namespace.useState(formStateRef.current);
|
|
131
|
-
const readFormState = React__namespace.useRef({
|
|
132
|
-
isDirty: false,
|
|
133
|
-
dirtyFields: false,
|
|
134
|
-
touchedFields: false,
|
|
135
|
-
isValidating: false,
|
|
136
|
-
isValid: false,
|
|
137
|
-
errors: false,
|
|
138
|
-
});
|
|
139
|
-
React__namespace.useEffect(() => {
|
|
140
|
-
const formStateSubscription = formStateSubjectRef.current.subscribe({
|
|
141
|
-
next: (formState) => {
|
|
142
|
-
shouldRenderFormState(formState, readFormState.current) &&
|
|
143
|
-
updateFormState(Object.assign(Object.assign({}, formStateRef.current), formState));
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
return () => formStateSubscription.unsubscribe();
|
|
147
|
-
}, []);
|
|
148
|
-
return getProxyFormState(isProxyEnabled, formState, readFormStateRef, readFormState, false);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function useController({ name, rules, defaultValue, control, }) {
|
|
152
|
-
const methods = useFormContext();
|
|
153
|
-
const { defaultValuesRef, register, fieldsRef, fieldArrayNamesRef, controllerSubjectRef, } = control || methods.control;
|
|
154
|
-
const { onChange, onBlur, ref } = register(name, rules);
|
|
155
|
-
const [value, setInputStateValue] = React__namespace.useState(isUndefined(get(fieldsRef.current, name)._f.value) ||
|
|
156
|
-
isNameInFieldArray(fieldArrayNamesRef.current, name)
|
|
157
|
-
? isUndefined(defaultValue)
|
|
158
|
-
? get(defaultValuesRef.current, name)
|
|
159
|
-
: defaultValue
|
|
160
|
-
: get(fieldsRef.current, name)._f.value);
|
|
161
|
-
const formState = useFormState({
|
|
162
|
-
control: control || methods.control,
|
|
163
|
-
});
|
|
164
|
-
get(fieldsRef.current, name)._f.value = value;
|
|
165
|
-
React__namespace.useEffect(() => {
|
|
166
|
-
const controllerSubscription = controllerSubjectRef.current.subscribe({
|
|
167
|
-
next: (data) => (!data.name || name === data.name) &&
|
|
168
|
-
setInputStateValue(get(data.values, name)),
|
|
169
|
-
});
|
|
170
|
-
ref({
|
|
171
|
-
target: value,
|
|
172
|
-
});
|
|
173
|
-
return () => controllerSubscription.unsubscribe();
|
|
174
|
-
}, [name]);
|
|
175
|
-
return {
|
|
176
|
-
field: {
|
|
177
|
-
onChange: (event) => {
|
|
178
|
-
const value = getControllerValue(event);
|
|
179
|
-
setInputStateValue(value);
|
|
180
|
-
onChange({
|
|
181
|
-
target: {
|
|
182
|
-
value,
|
|
183
|
-
name: name,
|
|
184
|
-
},
|
|
185
|
-
type: EVENTS.CHANGE,
|
|
186
|
-
});
|
|
187
|
-
},
|
|
188
|
-
onBlur: () => {
|
|
189
|
-
onBlur({
|
|
190
|
-
target: {
|
|
191
|
-
name: name,
|
|
192
|
-
},
|
|
193
|
-
type: EVENTS.BLUR,
|
|
194
|
-
});
|
|
195
|
-
},
|
|
196
|
-
name,
|
|
197
|
-
value,
|
|
198
|
-
ref,
|
|
199
|
-
},
|
|
200
|
-
formState,
|
|
201
|
-
fieldState: Object.defineProperties({}, {
|
|
202
|
-
invalid: {
|
|
203
|
-
get() {
|
|
204
|
-
return !!get(formState.errors, name);
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
isDirty: {
|
|
208
|
-
get() {
|
|
209
|
-
return !!get(formState.dirtyFields, name);
|
|
210
|
-
},
|
|
211
|
-
},
|
|
212
|
-
isTouched: {
|
|
213
|
-
get() {
|
|
214
|
-
return !!get(formState.touchedFields, name);
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
error: {
|
|
218
|
-
get() {
|
|
219
|
-
return get(formState.errors, name);
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
}),
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const Controller = (props) => props.render(useController(props));
|
|
227
|
-
|
|
228
|
-
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
|
229
|
-
? Object.assign(Object.assign({}, errors[name]), { types: Object.assign(Object.assign({}, (errors[name] && errors[name].types ? errors[name].types : {})), { [type]: message || true }) }) : {};
|
|
230
|
-
|
|
231
|
-
var isKey = (value) => /^\w*$/.test(value);
|
|
232
|
-
|
|
233
|
-
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
234
|
-
|
|
235
|
-
function set(object, path, value) {
|
|
236
|
-
let index = -1;
|
|
237
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
238
|
-
const length = tempPath.length;
|
|
239
|
-
const lastIndex = length - 1;
|
|
240
|
-
while (++index < length) {
|
|
241
|
-
const key = tempPath[index];
|
|
242
|
-
let newValue = value;
|
|
243
|
-
if (index !== lastIndex) {
|
|
244
|
-
const objValue = object[key];
|
|
245
|
-
newValue =
|
|
246
|
-
isObject(objValue) || Array.isArray(objValue)
|
|
247
|
-
? objValue
|
|
248
|
-
: !isNaN(+tempPath[index + 1])
|
|
249
|
-
? []
|
|
250
|
-
: {};
|
|
251
|
-
}
|
|
252
|
-
object[key] = newValue;
|
|
253
|
-
object = object[key];
|
|
254
|
-
}
|
|
255
|
-
return object;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const focusFieldBy = (fields, callback, fieldsNames) => {
|
|
259
|
-
for (const key of fieldsNames || Object.keys(fields)) {
|
|
260
|
-
const field = get(fields, key);
|
|
261
|
-
if (field) {
|
|
262
|
-
const _f = field._f;
|
|
263
|
-
const current = omit(field, '_f');
|
|
264
|
-
if (_f && callback(_f.name)) {
|
|
265
|
-
if (_f.ref.focus && isUndefined(_f.ref.focus())) {
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
|
-
else if (_f.refs) {
|
|
269
|
-
_f.refs[0].focus();
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
else if (isObject(current)) {
|
|
274
|
-
focusFieldBy(current, callback);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
const getFieldsValues = (fieldsRef, defaultValuesRef = { current: {} }, output = {}) => {
|
|
281
|
-
for (const name in fieldsRef.current) {
|
|
282
|
-
const field = fieldsRef.current[name];
|
|
283
|
-
if (field) {
|
|
284
|
-
const _f = field._f;
|
|
285
|
-
const current = omit(field, '_f');
|
|
286
|
-
set(output, name, _f
|
|
287
|
-
? _f.ref.disabled || (_f.refs && _f.refs.every((ref) => ref.disabled))
|
|
288
|
-
? undefined
|
|
289
|
-
: _f.value
|
|
290
|
-
: Array.isArray(field)
|
|
291
|
-
? []
|
|
292
|
-
: {});
|
|
293
|
-
if (current) {
|
|
294
|
-
getFieldsValues({
|
|
295
|
-
current,
|
|
296
|
-
}, defaultValuesRef, output[name]);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return Object.assign(Object.assign({}, defaultValuesRef.current), output);
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
var generateId = () => {
|
|
304
|
-
const d = typeof performance === UNDEFINED ? Date.now() : performance.now() * 1000;
|
|
305
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
306
|
-
const r = (Math.random() * 16 + d) % 16 | 0;
|
|
307
|
-
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
|
308
|
-
});
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
var mapIds = (values = [], keyName) => values.map((value) => (Object.assign({ [keyName]: (value && value[keyName]) || generateId() }, value)));
|
|
312
|
-
|
|
313
|
-
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
314
|
-
|
|
315
|
-
function deepEqual(object1, object2, isErrorObject) {
|
|
316
|
-
if (isPrimitive(object1) ||
|
|
317
|
-
isPrimitive(object2) ||
|
|
318
|
-
object1 instanceof Date ||
|
|
319
|
-
object2 instanceof Date) {
|
|
320
|
-
return object1 === object2;
|
|
321
|
-
}
|
|
322
|
-
if (!React__namespace.isValidElement(object1)) {
|
|
323
|
-
const keys1 = Object.keys(object1);
|
|
324
|
-
const keys2 = Object.keys(object2);
|
|
325
|
-
if (keys1.length !== keys2.length) {
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
for (const key of keys1) {
|
|
329
|
-
const val1 = object1[key];
|
|
330
|
-
if (!(isErrorObject && key === 'ref')) {
|
|
331
|
-
const val2 = object2[key];
|
|
332
|
-
if ((isObject(val1) || Array.isArray(val1)) &&
|
|
333
|
-
(isObject(val2) || Array.isArray(val2))
|
|
334
|
-
? !deepEqual(val1, val2, isErrorObject)
|
|
335
|
-
: val1 !== val2) {
|
|
336
|
-
return false;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
return true;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function deepMerge(target, source) {
|
|
345
|
-
if (isPrimitive(target) || isPrimitive(source)) {
|
|
346
|
-
return source;
|
|
347
|
-
}
|
|
348
|
-
for (const key in source) {
|
|
349
|
-
const targetValue = target[key];
|
|
350
|
-
const sourceValue = source[key];
|
|
351
|
-
try {
|
|
352
|
-
target[key] =
|
|
353
|
-
(isObject(targetValue) && isObject(sourceValue)) ||
|
|
354
|
-
(Array.isArray(targetValue) && Array.isArray(sourceValue))
|
|
355
|
-
? deepMerge(targetValue, sourceValue)
|
|
356
|
-
: sourceValue;
|
|
357
|
-
}
|
|
358
|
-
catch (_a) { }
|
|
359
|
-
}
|
|
360
|
-
return target;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function setDirtyFields(values, defaultValues, dirtyFields, parentNode, parentName) {
|
|
364
|
-
let index = -1;
|
|
365
|
-
while (++index < values.length) {
|
|
366
|
-
for (const key in values[index]) {
|
|
367
|
-
if (Array.isArray(values[index][key])) {
|
|
368
|
-
!dirtyFields[index] && (dirtyFields[index] = {});
|
|
369
|
-
dirtyFields[index][key] = [];
|
|
370
|
-
setDirtyFields(values[index][key], get(defaultValues[index] || {}, key, []), dirtyFields[index][key], dirtyFields[index], key);
|
|
371
|
-
}
|
|
372
|
-
else {
|
|
373
|
-
deepEqual(get(defaultValues[index] || {}, key), values[index][key])
|
|
374
|
-
? set(dirtyFields[index] || {}, key)
|
|
375
|
-
: (dirtyFields[index] = Object.assign(Object.assign({}, dirtyFields[index]), { [key]: true }));
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
parentNode &&
|
|
379
|
-
!dirtyFields.length &&
|
|
380
|
-
delete parentNode[parentName];
|
|
381
|
-
}
|
|
382
|
-
return dirtyFields;
|
|
383
|
-
}
|
|
384
|
-
var setFieldArrayDirtyFields = (values, defaultValues, dirtyFields) => deepMerge(setDirtyFields(values, defaultValues, dirtyFields.slice(0, values.length)), setDirtyFields(defaultValues, values, dirtyFields.slice(0, values.length)));
|
|
385
|
-
|
|
386
|
-
function append(data, value) {
|
|
387
|
-
return [...data, ...(Array.isArray(value) ? value : [value])];
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
var fillEmptyArray = (value) => Array.isArray(value) ? Array(value.length).fill(undefined) : undefined;
|
|
391
|
-
|
|
392
|
-
function insert(data, index, value) {
|
|
393
|
-
return [
|
|
394
|
-
...data.slice(0, index),
|
|
395
|
-
...(Array.isArray(value) ? value : [value]),
|
|
396
|
-
...data.slice(index),
|
|
397
|
-
];
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
var moveArrayAt = (data, from, to) => {
|
|
401
|
-
if (Array.isArray(data)) {
|
|
402
|
-
if (isUndefined(data[to])) {
|
|
403
|
-
data[to] = undefined;
|
|
404
|
-
}
|
|
405
|
-
data.splice(to, 0, data.splice(from, 1)[0]);
|
|
406
|
-
return data;
|
|
407
|
-
}
|
|
408
|
-
return [];
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
function prepend(data, value) {
|
|
412
|
-
return [...(Array.isArray(value) ? value : [value]), ...data];
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
function removeAtIndexes(data, indexes) {
|
|
416
|
-
let i = 0;
|
|
417
|
-
const temp = [...data];
|
|
418
|
-
for (const index of indexes) {
|
|
419
|
-
temp.splice(index - i, 1);
|
|
420
|
-
i++;
|
|
421
|
-
}
|
|
422
|
-
return compact(temp).length ? temp : [];
|
|
423
|
-
}
|
|
424
|
-
var removeArrayAt = (data, index) => isUndefined(index)
|
|
425
|
-
? []
|
|
426
|
-
: removeAtIndexes(data, (Array.isArray(index) ? index : [index]).sort((a, b) => a - b));
|
|
427
|
-
|
|
428
|
-
var swapArrayAt = (data, indexA, indexB) => {
|
|
429
|
-
data[indexA] = [data[indexB], (data[indexB] = data[indexA])][0];
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
var isBoolean = (value) => typeof value === 'boolean';
|
|
433
|
-
|
|
434
|
-
function baseGet(object, updatePath) {
|
|
435
|
-
const length = updatePath.slice(0, -1).length;
|
|
436
|
-
let index = 0;
|
|
437
|
-
while (index < length) {
|
|
438
|
-
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
439
|
-
}
|
|
440
|
-
return object;
|
|
441
|
-
}
|
|
442
|
-
function unset(object, path) {
|
|
443
|
-
const updatePath = isKey(path) ? [path] : stringToPath(path);
|
|
444
|
-
const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
|
|
445
|
-
const key = updatePath[updatePath.length - 1];
|
|
446
|
-
let previousObjRef;
|
|
447
|
-
if (childObject) {
|
|
448
|
-
delete childObject[key];
|
|
449
|
-
}
|
|
450
|
-
for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
|
|
451
|
-
let index = -1;
|
|
452
|
-
let objectRef;
|
|
453
|
-
const currentPaths = updatePath.slice(0, -(k + 1));
|
|
454
|
-
const currentPathsLength = currentPaths.length - 1;
|
|
455
|
-
if (k > 0) {
|
|
456
|
-
previousObjRef = object;
|
|
457
|
-
}
|
|
458
|
-
while (++index < currentPaths.length) {
|
|
459
|
-
const item = currentPaths[index];
|
|
460
|
-
objectRef = objectRef ? objectRef[item] : object[item];
|
|
461
|
-
if (currentPathsLength === index &&
|
|
462
|
-
((isObject(objectRef) && isEmptyObject(objectRef)) ||
|
|
463
|
-
(Array.isArray(objectRef) &&
|
|
464
|
-
!objectRef.filter((data) => (isObject(data) && !isEmptyObject(data)) || isBoolean(data)).length))) {
|
|
465
|
-
previousObjRef ? delete previousObjRef[item] : delete object[item];
|
|
466
|
-
}
|
|
467
|
-
previousObjRef = objectRef;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
return object;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
const useFieldArray = ({ control, name, keyName = 'id', }) => {
|
|
474
|
-
const methods = useFormContext();
|
|
475
|
-
const focusNameRef = React__namespace.useRef('');
|
|
476
|
-
const { isWatchAllRef, watchFieldsRef, getFormIsDirty, watchSubjectRef, fieldArraySubjectRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, formStateRef, formStateSubjectRef, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, } = control || methods.control;
|
|
477
|
-
const [fields, setFields] = React__namespace.useState(mapIds(get(fieldArrayDefaultValuesRef.current, getNodeParentName(name))
|
|
478
|
-
? get(fieldArrayDefaultValuesRef.current, name, [])
|
|
479
|
-
: get(defaultValuesRef.current, name, []), keyName));
|
|
480
|
-
set(fieldArrayDefaultValuesRef.current, name, [...fields]);
|
|
481
|
-
fieldArrayNamesRef.current.add(name);
|
|
482
|
-
const omitKey = (fields) => fields.map((field) => omit((field || {}), keyName));
|
|
483
|
-
const getCurrentFieldsValues = () => {
|
|
484
|
-
const values = get(getFieldsValues(fieldsRef, defaultValuesRef), name, []);
|
|
485
|
-
return mapIds(get(fieldArrayDefaultValuesRef.current, name, []).map((item, index) => (Object.assign(Object.assign({}, item), values[index]))), keyName);
|
|
486
|
-
};
|
|
487
|
-
const getFocusDetail = (index, options) => options
|
|
488
|
-
? !isUndefined(options.focusIndex)
|
|
489
|
-
? `${name}.${options.focusIndex}`
|
|
490
|
-
: options.focusName
|
|
491
|
-
? options.focusName
|
|
492
|
-
: !options.shouldFocus
|
|
493
|
-
? ''
|
|
494
|
-
: `${name}.${index}`
|
|
495
|
-
: `${name}.${index}`;
|
|
496
|
-
const resetFields = (index) => (Array.isArray(index) ? index : [index]).forEach((currentIndex) => set(fieldsRef.current, `${name}${isUndefined(currentIndex) ? '' : `.${currentIndex}`}`, isUndefined(currentIndex) ? [] : undefined));
|
|
497
|
-
const setFieldsAndNotify = (fieldsValues = []) => setFields(mapIds(fieldsValues, keyName));
|
|
498
|
-
const cleanup = (ref) => !compact(get(ref, name, [])).length && unset(ref, name);
|
|
499
|
-
const updateDirtyFieldsWithDefaultValues = (updatedFieldArrayValues) => updatedFieldArrayValues &&
|
|
500
|
-
set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(omitKey(updatedFieldArrayValues), get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
|
|
501
|
-
const batchStateUpdate = (method, args, updatedFieldArrayValues = [], shouldSet = true) => {
|
|
502
|
-
if (get(fieldsRef.current, name)) {
|
|
503
|
-
const output = method(get(fieldsRef.current, name), args.argA, args.argB);
|
|
504
|
-
shouldSet && set(fieldsRef.current, name, output);
|
|
505
|
-
}
|
|
506
|
-
if (Array.isArray(get(formStateRef.current.errors, name))) {
|
|
507
|
-
const output = method(get(formStateRef.current.errors, name), args.argA, args.argB);
|
|
508
|
-
shouldSet && set(formStateRef.current.errors, name, output);
|
|
509
|
-
cleanup(formStateRef.current.errors);
|
|
510
|
-
}
|
|
511
|
-
if (readFormStateRef.current.touchedFields &&
|
|
512
|
-
get(formStateRef.current.touchedFields, name)) {
|
|
513
|
-
const output = method(get(formStateRef.current.touchedFields, name), args.argA, args.argB);
|
|
514
|
-
shouldSet && set(formStateRef.current.touchedFields, name, output);
|
|
515
|
-
cleanup(formStateRef.current.touchedFields);
|
|
516
|
-
}
|
|
517
|
-
if (readFormStateRef.current.dirtyFields ||
|
|
518
|
-
readFormStateRef.current.isDirty) {
|
|
519
|
-
set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(omitKey(updatedFieldArrayValues), get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
|
|
520
|
-
updateDirtyFieldsWithDefaultValues(updatedFieldArrayValues);
|
|
521
|
-
cleanup(formStateRef.current.dirtyFields);
|
|
522
|
-
}
|
|
523
|
-
if (readFormStateRef.current.isValid) {
|
|
524
|
-
set(validFieldsRef.current, name, method(get(validFieldsRef.current, name, []), args.argA));
|
|
525
|
-
cleanup(validFieldsRef.current);
|
|
526
|
-
set(fieldsWithValidationRef.current, name, method(get(fieldsWithValidationRef.current, name, []), args.argA));
|
|
527
|
-
cleanup(fieldsWithValidationRef.current);
|
|
528
|
-
}
|
|
529
|
-
formStateSubjectRef.current.next({
|
|
530
|
-
isDirty: getFormIsDirty(name, omitKey(updatedFieldArrayValues)),
|
|
531
|
-
errors: formStateRef.current.errors,
|
|
532
|
-
isValid: formStateRef.current.isValid,
|
|
533
|
-
});
|
|
534
|
-
};
|
|
535
|
-
const registerFieldArray = (values, index = 0, parentName = '') => values.forEach((appendValueItem, valueIndex) => !isPrimitive(appendValueItem) &&
|
|
536
|
-
Object.entries(appendValueItem).forEach(([key, value]) => {
|
|
537
|
-
const inputName = `${parentName || name}.${parentName ? valueIndex : index + valueIndex}.${key}`;
|
|
538
|
-
Array.isArray(value)
|
|
539
|
-
? registerFieldArray(value, valueIndex, inputName)
|
|
540
|
-
: set(fieldsRef.current, inputName, {
|
|
541
|
-
_f: {
|
|
542
|
-
ref: {
|
|
543
|
-
name: inputName,
|
|
544
|
-
},
|
|
545
|
-
name: inputName,
|
|
546
|
-
value,
|
|
547
|
-
},
|
|
548
|
-
});
|
|
549
|
-
}));
|
|
550
|
-
const append$1 = (value, options) => {
|
|
551
|
-
const appendValue = Array.isArray(value) ? value : [value];
|
|
552
|
-
const updatedFieldArrayValues = append(getCurrentFieldsValues(), appendValue);
|
|
553
|
-
const currentIndex = updatedFieldArrayValues.length - appendValue.length;
|
|
554
|
-
setFieldsAndNotify(updatedFieldArrayValues);
|
|
555
|
-
batchStateUpdate(append, {
|
|
556
|
-
argA: fillEmptyArray(value),
|
|
557
|
-
}, updatedFieldArrayValues, false);
|
|
558
|
-
registerFieldArray(appendValue, currentIndex);
|
|
559
|
-
focusNameRef.current = getFocusDetail(currentIndex, options);
|
|
560
|
-
};
|
|
561
|
-
const prepend$1 = (value, options) => {
|
|
562
|
-
const prependValue = Array.isArray(value) ? value : [value];
|
|
563
|
-
const updatedFieldArrayValues = prepend(getCurrentFieldsValues(), prependValue);
|
|
564
|
-
setFieldsAndNotify(updatedFieldArrayValues);
|
|
565
|
-
batchStateUpdate(prepend, {
|
|
566
|
-
argA: fillEmptyArray(value),
|
|
567
|
-
}, updatedFieldArrayValues);
|
|
568
|
-
registerFieldArray(prependValue);
|
|
569
|
-
focusNameRef.current = getFocusDetail(0, options);
|
|
570
|
-
};
|
|
571
|
-
const remove = (index) => {
|
|
572
|
-
const updatedFieldArrayValues = removeArrayAt(getCurrentFieldsValues(), index);
|
|
573
|
-
resetFields(index);
|
|
574
|
-
setFieldsAndNotify(updatedFieldArrayValues);
|
|
575
|
-
batchStateUpdate(removeArrayAt, {
|
|
576
|
-
argA: index,
|
|
577
|
-
}, updatedFieldArrayValues);
|
|
578
|
-
};
|
|
579
|
-
const insert$1 = (index, value, options) => {
|
|
580
|
-
const insertValue = Array.isArray(value) ? value : [value];
|
|
581
|
-
const updatedFieldArrayValues = insert(getCurrentFieldsValues(), index, insertValue);
|
|
582
|
-
setFieldsAndNotify(updatedFieldArrayValues);
|
|
583
|
-
batchStateUpdate(insert, {
|
|
584
|
-
argA: index,
|
|
585
|
-
argB: fillEmptyArray(value),
|
|
586
|
-
}, updatedFieldArrayValues);
|
|
587
|
-
registerFieldArray(insertValue, index);
|
|
588
|
-
focusNameRef.current = getFocusDetail(index, options);
|
|
589
|
-
};
|
|
590
|
-
const swap = (indexA, indexB) => {
|
|
591
|
-
const fieldValues = getCurrentFieldsValues();
|
|
592
|
-
swapArrayAt(fieldValues, indexA, indexB);
|
|
593
|
-
batchStateUpdate(swapArrayAt, {
|
|
594
|
-
argA: indexA,
|
|
595
|
-
argB: indexB,
|
|
596
|
-
}, fieldValues, false);
|
|
597
|
-
setFieldsAndNotify(fieldValues);
|
|
598
|
-
};
|
|
599
|
-
const move = (from, to) => {
|
|
600
|
-
const fieldValues = getCurrentFieldsValues();
|
|
601
|
-
moveArrayAt(fieldValues, from, to);
|
|
602
|
-
setFieldsAndNotify(fieldValues);
|
|
603
|
-
batchStateUpdate(moveArrayAt, {
|
|
604
|
-
argA: from,
|
|
605
|
-
argB: to,
|
|
606
|
-
}, fieldValues, false);
|
|
607
|
-
};
|
|
608
|
-
React__namespace.useEffect(() => {
|
|
609
|
-
if (isWatchAllRef.current) {
|
|
610
|
-
formStateSubjectRef.current.next({});
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
for (const watchField of watchFieldsRef.current) {
|
|
614
|
-
if (name.startsWith(watchField)) {
|
|
615
|
-
formStateSubjectRef.current.next({});
|
|
616
|
-
break;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
watchSubjectRef.current.next({
|
|
621
|
-
name,
|
|
622
|
-
value: get(getFieldsValues(fieldsRef, defaultValuesRef), name, []),
|
|
623
|
-
});
|
|
624
|
-
focusNameRef.current &&
|
|
625
|
-
focusFieldBy(fieldsRef.current, (key) => key.startsWith(focusNameRef.current));
|
|
626
|
-
focusNameRef.current = '';
|
|
627
|
-
fieldArraySubjectRef.current.next({
|
|
628
|
-
name,
|
|
629
|
-
fields: omitKey([...fields]),
|
|
630
|
-
});
|
|
631
|
-
}, [fields, name]);
|
|
632
|
-
React__namespace.useEffect(() => {
|
|
633
|
-
const fieldArraySubscription = fieldArraySubjectRef.current.subscribe({
|
|
634
|
-
next({ name: inputFieldArrayName, fields, isReset }) {
|
|
635
|
-
if (isReset) {
|
|
636
|
-
unset(fieldsRef.current, inputFieldArrayName || name);
|
|
637
|
-
inputFieldArrayName
|
|
638
|
-
? set(fieldArrayDefaultValuesRef.current, inputFieldArrayName, fields)
|
|
639
|
-
: (fieldArrayDefaultValuesRef.current = fields);
|
|
640
|
-
setFieldsAndNotify(get(fieldArrayDefaultValuesRef.current, name));
|
|
641
|
-
}
|
|
642
|
-
},
|
|
643
|
-
});
|
|
644
|
-
!get(fieldsRef.current, name) && set(fieldsRef.current, name, []);
|
|
645
|
-
return () => {
|
|
646
|
-
fieldArrayDefaultValuesRef.current = getFieldsValues(fieldsRef);
|
|
647
|
-
fieldArraySubscription.unsubscribe();
|
|
648
|
-
};
|
|
649
|
-
}, []);
|
|
650
|
-
return {
|
|
651
|
-
swap: React__namespace.useCallback(swap, [name]),
|
|
652
|
-
move: React__namespace.useCallback(move, [name]),
|
|
653
|
-
prepend: React__namespace.useCallback(prepend$1, [name]),
|
|
654
|
-
append: React__namespace.useCallback(append$1, [name]),
|
|
655
|
-
remove: React__namespace.useCallback(remove, [name]),
|
|
656
|
-
insert: React__namespace.useCallback(insert$1, [name]),
|
|
657
|
-
fields: fields,
|
|
658
|
-
};
|
|
659
|
-
};
|
|
660
|
-
|
|
661
|
-
function getFields(fieldsNames, fieldsRefs) {
|
|
662
|
-
const currentFields = {};
|
|
663
|
-
for (const name of fieldsNames) {
|
|
664
|
-
const field = get(fieldsRefs, name);
|
|
665
|
-
if (field) {
|
|
666
|
-
!isKey(name)
|
|
667
|
-
? set(currentFields, name, field._f)
|
|
668
|
-
: (currentFields[name] = field._f);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
return currentFields;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
var isFileInput = (element) => element.type === 'file';
|
|
675
|
-
|
|
676
|
-
var isMultipleSelect = (element) => element.type === `${SELECT}-multiple`;
|
|
677
|
-
|
|
678
|
-
var isRadioInput = (element) => element.type === 'radio';
|
|
679
|
-
|
|
680
|
-
const defaultResult = {
|
|
681
|
-
value: false,
|
|
682
|
-
isValid: false,
|
|
683
|
-
};
|
|
684
|
-
const validResult = { value: true, isValid: true };
|
|
685
|
-
var getCheckboxValue = (options) => {
|
|
686
|
-
if (Array.isArray(options)) {
|
|
687
|
-
if (options.length > 1) {
|
|
688
|
-
const values = options
|
|
689
|
-
.filter((option) => option && option.checked && !option.disabled)
|
|
690
|
-
.map((option) => option.value);
|
|
691
|
-
return { value: values, isValid: !!values.length };
|
|
692
|
-
}
|
|
693
|
-
return options[0].checked && !options[0].disabled
|
|
694
|
-
? // @ts-expect-error expected to work in the browser
|
|
695
|
-
options[0].attributes && !isUndefined(options[0].attributes.value)
|
|
696
|
-
? isUndefined(options[0].value) || options[0].value === ''
|
|
697
|
-
? validResult
|
|
698
|
-
: { value: options[0].value, isValid: true }
|
|
699
|
-
: validResult
|
|
700
|
-
: defaultResult;
|
|
701
|
-
}
|
|
702
|
-
return defaultResult;
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => valueAsNumber
|
|
706
|
-
? value === ''
|
|
707
|
-
? NaN
|
|
708
|
-
: +value
|
|
709
|
-
: valueAsDate
|
|
710
|
-
? new Date(value)
|
|
711
|
-
: setValueAs
|
|
712
|
-
? setValueAs(value)
|
|
713
|
-
: value;
|
|
714
|
-
|
|
715
|
-
var getMultipleSelectValue = (options) => [...options]
|
|
716
|
-
.filter(({ selected }) => selected)
|
|
717
|
-
.map(({ value }) => value);
|
|
718
|
-
|
|
719
|
-
const defaultReturn = {
|
|
720
|
-
isValid: false,
|
|
721
|
-
value: null,
|
|
722
|
-
};
|
|
723
|
-
var getRadioValue = (options) => Array.isArray(options)
|
|
724
|
-
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
|
725
|
-
? {
|
|
726
|
-
isValid: true,
|
|
727
|
-
value: option.value,
|
|
728
|
-
}
|
|
729
|
-
: previous, defaultReturn)
|
|
730
|
-
: defaultReturn;
|
|
731
|
-
|
|
732
|
-
function getFieldValue(field) {
|
|
733
|
-
if (field && field._f) {
|
|
734
|
-
const ref = field._f.ref;
|
|
735
|
-
if (ref.disabled) {
|
|
736
|
-
return;
|
|
737
|
-
}
|
|
738
|
-
if (isFileInput(ref)) {
|
|
739
|
-
return ref.files;
|
|
740
|
-
}
|
|
741
|
-
if (isRadioInput(ref)) {
|
|
742
|
-
return getRadioValue(field._f.refs).value;
|
|
743
|
-
}
|
|
744
|
-
if (isMultipleSelect(ref)) {
|
|
745
|
-
return getMultipleSelectValue(ref.options);
|
|
746
|
-
}
|
|
747
|
-
if (isCheckBoxInput(ref)) {
|
|
748
|
-
return getCheckboxValue(field._f.refs).value;
|
|
749
|
-
}
|
|
750
|
-
return getFieldValueAs(isUndefined(ref.value) ? field._f.ref.value : ref.value, field._f);
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
var isErrorStateChanged = ({ errors, name, error, validFields, fieldsWithValidation, }) => {
|
|
755
|
-
const isValid = isUndefined(error);
|
|
756
|
-
const previousError = get(errors, name);
|
|
757
|
-
return ((isValid && !!previousError) ||
|
|
758
|
-
(!isValid && !deepEqual(previousError, error, true)) ||
|
|
759
|
-
(isValid && get(fieldsWithValidation, name) && !get(validFields, name)));
|
|
760
|
-
};
|
|
761
|
-
|
|
762
|
-
var skipValidation = ({ isOnBlur, isOnChange, isOnTouch, isTouched, isReValidateOnBlur, isReValidateOnChange, isBlurEvent, isSubmitted, isOnAll, }) => {
|
|
763
|
-
if (isOnAll) {
|
|
764
|
-
return false;
|
|
765
|
-
}
|
|
766
|
-
else if (!isSubmitted && isOnTouch) {
|
|
767
|
-
return !(isTouched || isBlurEvent);
|
|
768
|
-
}
|
|
769
|
-
else if (isSubmitted ? isReValidateOnBlur : isOnBlur) {
|
|
770
|
-
return !isBlurEvent;
|
|
771
|
-
}
|
|
772
|
-
else if (isSubmitted ? isReValidateOnChange : isOnChange) {
|
|
773
|
-
return isBlurEvent;
|
|
774
|
-
}
|
|
775
|
-
return true;
|
|
776
|
-
};
|
|
777
|
-
|
|
778
|
-
var isFunction = (value) => typeof value === 'function';
|
|
779
|
-
|
|
780
|
-
var isString = (value) => typeof value === 'string';
|
|
781
|
-
|
|
782
|
-
var isMessage = (value) => isString(value) || React__namespace.isValidElement(value);
|
|
783
|
-
|
|
784
|
-
var isRegex = (value) => value instanceof RegExp;
|
|
785
|
-
|
|
786
|
-
function getValidateError(result, ref, type = 'validate') {
|
|
787
|
-
if (isMessage(result) || (isBoolean(result) && !result)) {
|
|
788
|
-
return {
|
|
789
|
-
type,
|
|
790
|
-
message: isMessage(result) ? result : '',
|
|
791
|
-
ref,
|
|
792
|
-
};
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
|
797
|
-
? validationData
|
|
798
|
-
: {
|
|
799
|
-
value: validationData,
|
|
800
|
-
message: '',
|
|
801
|
-
};
|
|
802
|
-
|
|
803
|
-
var validateField = async ({ _f: { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, value: inputValue, valueAsNumber, }, }, validateAllFieldCriteria) => {
|
|
804
|
-
const error = {};
|
|
805
|
-
const isRadio = isRadioInput(ref);
|
|
806
|
-
const isCheckBox = isCheckBoxInput(ref);
|
|
807
|
-
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
808
|
-
const isEmpty = (valueAsNumber && ref.value === '') ||
|
|
809
|
-
inputValue === '' ||
|
|
810
|
-
(Array.isArray(inputValue) && !inputValue.length);
|
|
811
|
-
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
812
|
-
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
813
|
-
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
814
|
-
error[name] = Object.assign({ type: exceedMax ? maxType : minType, message,
|
|
815
|
-
ref }, appendErrorsCurry(exceedMax ? maxType : minType, message));
|
|
816
|
-
};
|
|
817
|
-
if (required &&
|
|
818
|
-
((!isRadio && !isCheckBox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
|
819
|
-
(isBoolean(inputValue) && !inputValue) ||
|
|
820
|
-
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
|
821
|
-
(isRadio && !getRadioValue(refs).isValid))) {
|
|
822
|
-
const { value, message } = isMessage(required)
|
|
823
|
-
? { value: !!required, message: required }
|
|
824
|
-
: getValueAndMessage(required);
|
|
825
|
-
if (value) {
|
|
826
|
-
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.required, message, ref: isRadioOrCheckbox ? (refs || [])[0] || {} : ref }, appendErrorsCurry(INPUT_VALIDATION_RULES.required, message));
|
|
827
|
-
if (!validateAllFieldCriteria) {
|
|
828
|
-
return error;
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
if ((!isNullOrUndefined(min) || !isNullOrUndefined(max)) &&
|
|
833
|
-
inputValue !== '') {
|
|
834
|
-
let exceedMax;
|
|
835
|
-
let exceedMin;
|
|
836
|
-
const maxOutput = getValueAndMessage(max);
|
|
837
|
-
const minOutput = getValueAndMessage(min);
|
|
838
|
-
if (!isNaN(inputValue)) {
|
|
839
|
-
const valueNumber = ref.valueAsNumber || parseFloat(inputValue);
|
|
840
|
-
if (!isNullOrUndefined(maxOutput.value)) {
|
|
841
|
-
exceedMax = valueNumber > maxOutput.value;
|
|
842
|
-
}
|
|
843
|
-
if (!isNullOrUndefined(minOutput.value)) {
|
|
844
|
-
exceedMin = valueNumber < minOutput.value;
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
else {
|
|
848
|
-
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
849
|
-
if (isString(maxOutput.value)) {
|
|
850
|
-
exceedMax = valueDate > new Date(maxOutput.value);
|
|
851
|
-
}
|
|
852
|
-
if (isString(minOutput.value)) {
|
|
853
|
-
exceedMin = valueDate < new Date(minOutput.value);
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
if (exceedMax || exceedMin) {
|
|
857
|
-
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
858
|
-
if (!validateAllFieldCriteria) {
|
|
859
|
-
return error;
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
if (isString(inputValue) && !isEmpty && (maxLength || minLength)) {
|
|
864
|
-
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
865
|
-
const minLengthOutput = getValueAndMessage(minLength);
|
|
866
|
-
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
|
867
|
-
inputValue.length > maxLengthOutput.value;
|
|
868
|
-
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
|
869
|
-
inputValue.length < minLengthOutput.value;
|
|
870
|
-
if (exceedMax || exceedMin) {
|
|
871
|
-
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
872
|
-
if (!validateAllFieldCriteria) {
|
|
873
|
-
return error;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
if (isString(inputValue) && pattern && !isEmpty) {
|
|
878
|
-
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
879
|
-
if (isRegex(patternValue) && !patternValue.test(inputValue)) {
|
|
880
|
-
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.pattern, message,
|
|
881
|
-
ref }, appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message));
|
|
882
|
-
if (!validateAllFieldCriteria) {
|
|
883
|
-
return error;
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
if (validate) {
|
|
888
|
-
const validateRef = isRadioOrCheckbox && refs ? refs[0] : ref;
|
|
889
|
-
if (isFunction(validate)) {
|
|
890
|
-
const result = await validate(inputValue);
|
|
891
|
-
const validateError = getValidateError(result, validateRef);
|
|
892
|
-
if (validateError) {
|
|
893
|
-
error[name] = Object.assign(Object.assign({}, validateError), appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message));
|
|
894
|
-
if (!validateAllFieldCriteria) {
|
|
895
|
-
return error;
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
else if (isObject(validate)) {
|
|
900
|
-
let validationResult = {};
|
|
901
|
-
for (const [key, validateFunction] of Object.entries(validate)) {
|
|
902
|
-
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
903
|
-
break;
|
|
904
|
-
}
|
|
905
|
-
const validateResult = await validateFunction(inputValue);
|
|
906
|
-
const validateError = getValidateError(validateResult, validateRef, key);
|
|
907
|
-
if (validateError) {
|
|
908
|
-
validationResult = Object.assign(Object.assign({}, validateError), appendErrorsCurry(key, validateError.message));
|
|
909
|
-
if (validateAllFieldCriteria) {
|
|
910
|
-
error[name] = validationResult;
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
if (!isEmptyObject(validationResult)) {
|
|
915
|
-
error[name] = Object.assign({ ref: validateRef }, validationResult);
|
|
916
|
-
if (!validateAllFieldCriteria) {
|
|
917
|
-
return error;
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
return error;
|
|
923
|
-
};
|
|
924
|
-
|
|
925
|
-
var getValidationModes = (mode) => ({
|
|
926
|
-
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
927
|
-
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
928
|
-
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
929
|
-
isOnAll: mode === VALIDATION_MODE.all,
|
|
930
|
-
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
|
931
|
-
});
|
|
932
|
-
|
|
933
|
-
var isHTMLElement = (value) => value instanceof HTMLElement;
|
|
934
|
-
|
|
935
|
-
var isRadioOrCheckboxFunction = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
936
|
-
|
|
937
|
-
class Subscription {
|
|
938
|
-
constructor() {
|
|
939
|
-
this.tearDowns = [];
|
|
940
|
-
}
|
|
941
|
-
add(tearDown) {
|
|
942
|
-
this.tearDowns.push(tearDown);
|
|
943
|
-
}
|
|
944
|
-
unsubscribe() {
|
|
945
|
-
for (const teardown of this.tearDowns) {
|
|
946
|
-
teardown();
|
|
947
|
-
}
|
|
948
|
-
this.tearDowns = [];
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
class Subscriber {
|
|
952
|
-
constructor(observer, subscription) {
|
|
953
|
-
this.observer = observer;
|
|
954
|
-
this.closed = false;
|
|
955
|
-
subscription.add(() => (this.closed = true));
|
|
956
|
-
}
|
|
957
|
-
next(value) {
|
|
958
|
-
if (!this.closed) {
|
|
959
|
-
this.observer.next(value);
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
class Subject {
|
|
964
|
-
constructor() {
|
|
965
|
-
this.observers = [];
|
|
966
|
-
}
|
|
967
|
-
next(value) {
|
|
968
|
-
for (const observer of this.observers) {
|
|
969
|
-
observer.next(value);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
subscribe(observer) {
|
|
973
|
-
const subscription = new Subscription();
|
|
974
|
-
const subscriber = new Subscriber(observer, subscription);
|
|
975
|
-
this.observers.push(subscriber);
|
|
976
|
-
return subscription;
|
|
977
|
-
}
|
|
978
|
-
unsubscribe() {
|
|
979
|
-
this.observers = [];
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
const isWindowUndefined = typeof window === UNDEFINED;
|
|
984
|
-
function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_MODE.onChange, resolver, context, defaultValues = {}, shouldFocusError = true, criteriaMode, } = {}) {
|
|
985
|
-
const fieldsRef = React__namespace.useRef({});
|
|
986
|
-
const fieldsNamesRef = React__namespace.useRef(new Set());
|
|
987
|
-
const formStateSubjectRef = React__namespace.useRef(new Subject());
|
|
988
|
-
const watchSubjectRef = React__namespace.useRef(new Subject());
|
|
989
|
-
const controllerSubjectRef = React__namespace.useRef(new Subject());
|
|
990
|
-
const fieldArraySubjectRef = React__namespace.useRef(new Subject());
|
|
991
|
-
const fieldArrayDefaultValuesRef = React__namespace.useRef({});
|
|
992
|
-
const watchFieldsRef = React__namespace.useRef(new Set());
|
|
993
|
-
const isMountedRef = React__namespace.useRef(false);
|
|
994
|
-
const fieldsWithValidationRef = React__namespace.useRef({});
|
|
995
|
-
const validFieldsRef = React__namespace.useRef({});
|
|
996
|
-
const defaultValuesRef = React__namespace.useRef(defaultValues);
|
|
997
|
-
const isWatchAllRef = React__namespace.useRef(false);
|
|
998
|
-
const contextRef = React__namespace.useRef(context);
|
|
999
|
-
const resolverRef = React__namespace.useRef(resolver);
|
|
1000
|
-
const fieldArrayNamesRef = React__namespace.useRef(new Set());
|
|
1001
|
-
const validationMode = getValidationModes(mode);
|
|
1002
|
-
const isValidateAllFieldCriteria = criteriaMode === VALIDATION_MODE.all;
|
|
1003
|
-
const [formState, setFormState] = React__namespace.useState({
|
|
1004
|
-
isDirty: false,
|
|
1005
|
-
isValidating: false,
|
|
1006
|
-
dirtyFields: {},
|
|
1007
|
-
isSubmitted: false,
|
|
1008
|
-
submitCount: 0,
|
|
1009
|
-
touchedFields: {},
|
|
1010
|
-
isSubmitting: false,
|
|
1011
|
-
isSubmitSuccessful: false,
|
|
1012
|
-
isValid: !validationMode.isOnSubmit,
|
|
1013
|
-
errors: {},
|
|
1014
|
-
});
|
|
1015
|
-
const readFormStateRef = React__namespace.useRef({
|
|
1016
|
-
isDirty: !isProxyEnabled,
|
|
1017
|
-
dirtyFields: !isProxyEnabled,
|
|
1018
|
-
touchedFields: !isProxyEnabled,
|
|
1019
|
-
isValidating: !isProxyEnabled,
|
|
1020
|
-
isValid: !isProxyEnabled,
|
|
1021
|
-
errors: !isProxyEnabled,
|
|
1022
|
-
});
|
|
1023
|
-
const formStateRef = React__namespace.useRef(formState);
|
|
1024
|
-
contextRef.current = context;
|
|
1025
|
-
resolverRef.current = resolver;
|
|
1026
|
-
const getIsValid = () => (formStateRef.current.isValid =
|
|
1027
|
-
deepEqual(validFieldsRef.current, fieldsWithValidationRef.current) &&
|
|
1028
|
-
isEmptyObject(formStateRef.current.errors));
|
|
1029
|
-
const shouldRenderBaseOnError = React__namespace.useCallback((name, error, shouldRender = false, state = {}, isValid, isWatched) => {
|
|
1030
|
-
let shouldReRender = shouldRender ||
|
|
1031
|
-
isErrorStateChanged({
|
|
1032
|
-
errors: formStateRef.current.errors,
|
|
1033
|
-
error,
|
|
1034
|
-
name,
|
|
1035
|
-
validFields: validFieldsRef.current,
|
|
1036
|
-
fieldsWithValidation: fieldsWithValidationRef.current,
|
|
1037
|
-
});
|
|
1038
|
-
const previousError = get(formStateRef.current.errors, name);
|
|
1039
|
-
if (error) {
|
|
1040
|
-
unset(validFieldsRef.current, name);
|
|
1041
|
-
shouldReRender =
|
|
1042
|
-
shouldReRender ||
|
|
1043
|
-
!previousError ||
|
|
1044
|
-
!deepEqual(previousError, error, true);
|
|
1045
|
-
set(formStateRef.current.errors, name, error);
|
|
1046
|
-
}
|
|
1047
|
-
else {
|
|
1048
|
-
if (get(fieldsWithValidationRef.current, name) || resolverRef.current) {
|
|
1049
|
-
set(validFieldsRef.current, name, true);
|
|
1050
|
-
shouldReRender = shouldReRender || previousError;
|
|
1051
|
-
}
|
|
1052
|
-
unset(formStateRef.current.errors, name);
|
|
1053
|
-
}
|
|
1054
|
-
if ((shouldReRender && !isNullOrUndefined(shouldRender)) ||
|
|
1055
|
-
!isEmptyObject(state) ||
|
|
1056
|
-
isWatched) {
|
|
1057
|
-
const updatedFormState = Object.assign(Object.assign({}, state), { isValid: resolverRef.current ? !!isValid : getIsValid(), errors: formStateRef.current.errors });
|
|
1058
|
-
formStateRef.current = Object.assign(Object.assign({}, formStateRef.current), updatedFormState);
|
|
1059
|
-
formStateSubjectRef.current.next(isWatched ? {} : updatedFormState);
|
|
1060
|
-
}
|
|
1061
|
-
formStateSubjectRef.current.next({
|
|
1062
|
-
isValidating: false,
|
|
1063
|
-
});
|
|
1064
|
-
}, []);
|
|
1065
|
-
const setFieldValue = React__namespace.useCallback((name, rawValue, options = {}, shouldRender, shouldRegister) => {
|
|
1066
|
-
shouldRegister && register(name);
|
|
1067
|
-
const _f = get(fieldsRef.current, name, {})._f;
|
|
1068
|
-
if (_f) {
|
|
1069
|
-
const value = isWeb && isHTMLElement(_f.ref) && isNullOrUndefined(rawValue)
|
|
1070
|
-
? ''
|
|
1071
|
-
: rawValue;
|
|
1072
|
-
_f.value = rawValue;
|
|
1073
|
-
if (isRadioInput(_f.ref)) {
|
|
1074
|
-
(_f.refs || []).forEach((radioRef) => (radioRef.checked = radioRef.value === value));
|
|
1075
|
-
}
|
|
1076
|
-
else if (isFileInput(_f.ref) && !isString(value)) {
|
|
1077
|
-
_f.ref.files = value;
|
|
1078
|
-
}
|
|
1079
|
-
else if (isMultipleSelect(_f.ref)) {
|
|
1080
|
-
[..._f.ref.options].forEach((selectRef) => (selectRef.selected = value.includes(selectRef.value)));
|
|
1081
|
-
}
|
|
1082
|
-
else if (isCheckBoxInput(_f.ref) && _f.refs) {
|
|
1083
|
-
_f.refs.length > 1
|
|
1084
|
-
? _f.refs.forEach((checkboxRef) => (checkboxRef.checked = Array.isArray(value)
|
|
1085
|
-
? !!value.find((data) => data === checkboxRef.value)
|
|
1086
|
-
: value === checkboxRef.value))
|
|
1087
|
-
: (_f.refs[0].checked = !!value);
|
|
1088
|
-
}
|
|
1089
|
-
else {
|
|
1090
|
-
_f.ref.value = value;
|
|
1091
|
-
}
|
|
1092
|
-
if (shouldRender) {
|
|
1093
|
-
const values = getFieldsValues(fieldsRef);
|
|
1094
|
-
set(values, name, rawValue);
|
|
1095
|
-
controllerSubjectRef.current.next({
|
|
1096
|
-
values: Object.assign(Object.assign({}, defaultValuesRef.current), values),
|
|
1097
|
-
name,
|
|
1098
|
-
});
|
|
1099
|
-
}
|
|
1100
|
-
options.shouldDirty && updateAndGetDirtyState(name, value);
|
|
1101
|
-
options.shouldValidate && trigger(name);
|
|
1102
|
-
}
|
|
1103
|
-
}, []);
|
|
1104
|
-
const getFormIsDirty = React__namespace.useCallback((name, data) => {
|
|
1105
|
-
if (readFormStateRef.current.isDirty) {
|
|
1106
|
-
const formValues = getFieldsValues(fieldsRef);
|
|
1107
|
-
name && data && set(formValues, name, data);
|
|
1108
|
-
return !deepEqual(formValues, defaultValuesRef.current);
|
|
1109
|
-
}
|
|
1110
|
-
return false;
|
|
1111
|
-
}, []);
|
|
1112
|
-
const updateAndGetDirtyState = React__namespace.useCallback((name, inputValue, shouldRender = true) => {
|
|
1113
|
-
if (readFormStateRef.current.isDirty ||
|
|
1114
|
-
readFormStateRef.current.dirtyFields) {
|
|
1115
|
-
const isFieldDirty = !deepEqual(get(defaultValuesRef.current, name), inputValue);
|
|
1116
|
-
const isDirtyFieldExist = get(formStateRef.current.dirtyFields, name);
|
|
1117
|
-
const previousIsDirty = formStateRef.current.isDirty;
|
|
1118
|
-
isFieldDirty
|
|
1119
|
-
? set(formStateRef.current.dirtyFields, name, true)
|
|
1120
|
-
: unset(formStateRef.current.dirtyFields, name);
|
|
1121
|
-
formStateRef.current.isDirty = getFormIsDirty();
|
|
1122
|
-
const state = {
|
|
1123
|
-
isDirty: formStateRef.current.isDirty,
|
|
1124
|
-
dirtyFields: formStateRef.current.dirtyFields,
|
|
1125
|
-
};
|
|
1126
|
-
const isChanged = (readFormStateRef.current.isDirty &&
|
|
1127
|
-
previousIsDirty !== state.isDirty) ||
|
|
1128
|
-
(readFormStateRef.current.dirtyFields &&
|
|
1129
|
-
isDirtyFieldExist !== get(formStateRef.current.dirtyFields, name));
|
|
1130
|
-
isChanged && shouldRender && formStateSubjectRef.current.next(state);
|
|
1131
|
-
return isChanged ? state : {};
|
|
1132
|
-
}
|
|
1133
|
-
return {};
|
|
1134
|
-
}, []);
|
|
1135
|
-
const executeValidation = React__namespace.useCallback(async (name, skipReRender) => {
|
|
1136
|
-
const error = (await validateField(get(fieldsRef.current, name), isValidateAllFieldCriteria))[name];
|
|
1137
|
-
shouldRenderBaseOnError(name, error, skipReRender);
|
|
1138
|
-
return isUndefined(error);
|
|
1139
|
-
}, [isValidateAllFieldCriteria]);
|
|
1140
|
-
const executeSchemaOrResolverValidation = React__namespace.useCallback(async (names, currentNames = []) => {
|
|
1141
|
-
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, defaultValuesRef), contextRef.current, {
|
|
1142
|
-
criteriaMode,
|
|
1143
|
-
names: currentNames,
|
|
1144
|
-
fields: getFields(fieldsNamesRef.current, fieldsRef.current),
|
|
1145
|
-
});
|
|
1146
|
-
for (const name of names) {
|
|
1147
|
-
const error = get(errors, name);
|
|
1148
|
-
error
|
|
1149
|
-
? set(formStateRef.current.errors, name, error)
|
|
1150
|
-
: unset(formStateRef.current.errors, name);
|
|
1151
|
-
}
|
|
1152
|
-
return errors;
|
|
1153
|
-
}, [criteriaMode]);
|
|
1154
|
-
const validateForm = async (fieldsRef) => {
|
|
1155
|
-
for (const name in fieldsRef) {
|
|
1156
|
-
const field = fieldsRef[name];
|
|
1157
|
-
if (field) {
|
|
1158
|
-
const _f = field._f;
|
|
1159
|
-
const current = omit(field, '_f');
|
|
1160
|
-
if (_f) {
|
|
1161
|
-
const fieldError = await validateField(field, isValidateAllFieldCriteria);
|
|
1162
|
-
if (fieldError[_f.name]) {
|
|
1163
|
-
set(formStateRef.current.errors, _f.name, fieldError[_f.name]);
|
|
1164
|
-
unset(validFieldsRef.current, _f.name);
|
|
1165
|
-
}
|
|
1166
|
-
else if (get(fieldsWithValidationRef.current, _f.name)) {
|
|
1167
|
-
set(validFieldsRef.current, _f.name, true);
|
|
1168
|
-
unset(formStateRef.current.errors, _f.name);
|
|
1169
|
-
}
|
|
1170
|
-
}
|
|
1171
|
-
current && (await validateForm(current));
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
};
|
|
1175
|
-
const trigger = React__namespace.useCallback(async (name) => {
|
|
1176
|
-
const fields = isUndefined(name)
|
|
1177
|
-
? Object.keys(fieldsRef.current)
|
|
1178
|
-
: Array.isArray(name)
|
|
1179
|
-
? name
|
|
1180
|
-
: [name];
|
|
1181
|
-
let isValid;
|
|
1182
|
-
formStateSubjectRef.current.next({
|
|
1183
|
-
isValidating: true,
|
|
1184
|
-
});
|
|
1185
|
-
if (resolverRef.current) {
|
|
1186
|
-
isValid = isEmptyObject(await executeSchemaOrResolverValidation(fields, isUndefined(name)
|
|
1187
|
-
? undefined
|
|
1188
|
-
: fields));
|
|
1189
|
-
}
|
|
1190
|
-
else {
|
|
1191
|
-
isUndefined(name)
|
|
1192
|
-
? await validateForm(fieldsRef.current)
|
|
1193
|
-
: await Promise.all(fields.map(async (data) => await executeValidation(data, null)));
|
|
1194
|
-
}
|
|
1195
|
-
formStateSubjectRef.current.next({
|
|
1196
|
-
errors: formStateRef.current.errors,
|
|
1197
|
-
isValidating: false,
|
|
1198
|
-
isValid: resolverRef.current ? isValid : getIsValid(),
|
|
1199
|
-
});
|
|
1200
|
-
}, [executeSchemaOrResolverValidation, executeValidation]);
|
|
1201
|
-
const setInternalValues = React__namespace.useCallback((name, value, options) => Object.entries(value).forEach(([inputKey, inputValue]) => {
|
|
1202
|
-
const fieldName = `${name}.${inputKey}`;
|
|
1203
|
-
const field = get(fieldsRef.current, fieldName);
|
|
1204
|
-
field && !field._f
|
|
1205
|
-
? setInternalValues(fieldName, inputValue, options)
|
|
1206
|
-
: setFieldValue(fieldName, inputValue, options, true, !field);
|
|
1207
|
-
}), [trigger]);
|
|
1208
|
-
const isFieldWatched = (name) => isWatchAllRef.current ||
|
|
1209
|
-
watchFieldsRef.current.has(name) ||
|
|
1210
|
-
watchFieldsRef.current.has((name.match(/\w+/) || [])[0]);
|
|
1211
|
-
const updateValueAndGetDefault = (name) => {
|
|
1212
|
-
let defaultValue;
|
|
1213
|
-
const field = get(fieldsRef.current, name);
|
|
1214
|
-
if (field &&
|
|
1215
|
-
(!isEmptyObject(defaultValuesRef.current) || !isUndefined(field._f.value))) {
|
|
1216
|
-
defaultValue = isUndefined(field._f.value)
|
|
1217
|
-
? get(defaultValuesRef.current, name)
|
|
1218
|
-
: field._f.value;
|
|
1219
|
-
if (!isUndefined(defaultValue)) {
|
|
1220
|
-
setFieldValue(name, defaultValue);
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
return defaultValue;
|
|
1224
|
-
};
|
|
1225
|
-
const setValue = (name, value, options = {}) => {
|
|
1226
|
-
isMountedRef.current = true;
|
|
1227
|
-
const field = get(fieldsRef.current, name);
|
|
1228
|
-
const isFieldArray = fieldArrayNamesRef.current.has(name);
|
|
1229
|
-
if (isFieldArray) {
|
|
1230
|
-
fieldArraySubjectRef.current.next({
|
|
1231
|
-
fields: value,
|
|
1232
|
-
name,
|
|
1233
|
-
isReset: true,
|
|
1234
|
-
});
|
|
1235
|
-
if ((readFormStateRef.current.isDirty ||
|
|
1236
|
-
readFormStateRef.current.dirtyFields) &&
|
|
1237
|
-
options.shouldDirty) {
|
|
1238
|
-
set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(value, get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
|
|
1239
|
-
formStateSubjectRef.current.next({
|
|
1240
|
-
dirtyFields: formStateRef.current.dirtyFields,
|
|
1241
|
-
isDirty: getFormIsDirty(name, value),
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
!value.length &&
|
|
1245
|
-
set(fieldsRef.current, name, []) &&
|
|
1246
|
-
set(fieldArrayDefaultValuesRef.current, name, []);
|
|
1247
|
-
}
|
|
1248
|
-
(field && !field._f) || isFieldArray
|
|
1249
|
-
? setInternalValues(name, value, isFieldArray ? {} : options)
|
|
1250
|
-
: setFieldValue(name, value, options, true, !field);
|
|
1251
|
-
isFieldWatched(name) && formStateSubjectRef.current.next({});
|
|
1252
|
-
watchSubjectRef.current.next({ name, value });
|
|
1253
|
-
};
|
|
1254
|
-
const handleChange = React__namespace.useCallback(async ({ type, target, target: { value, type: inputType } }) => {
|
|
1255
|
-
let name = target.name;
|
|
1256
|
-
let error;
|
|
1257
|
-
let isValid;
|
|
1258
|
-
const field = get(fieldsRef.current, name);
|
|
1259
|
-
if (field) {
|
|
1260
|
-
const inputValue = inputType ? getFieldValue(field) : value;
|
|
1261
|
-
const isBlurEvent = type === EVENTS.BLUR;
|
|
1262
|
-
const { isOnBlur: isReValidateOnBlur, isOnChange: isReValidateOnChange, } = getValidationModes(reValidateMode);
|
|
1263
|
-
const shouldSkipValidation = skipValidation(Object.assign({ isBlurEvent, isTouched: !!get(formStateRef.current.touchedFields, name), isSubmitted: formStateRef.current.isSubmitted, isReValidateOnBlur,
|
|
1264
|
-
isReValidateOnChange }, validationMode));
|
|
1265
|
-
const isWatched = !isBlurEvent && isFieldWatched(name);
|
|
1266
|
-
if (!isUndefined(inputValue)) {
|
|
1267
|
-
field._f.value = inputValue;
|
|
1268
|
-
}
|
|
1269
|
-
const state = updateAndGetDirtyState(name, field._f.value, false);
|
|
1270
|
-
if (isBlurEvent && !get(formStateRef.current.touchedFields, name)) {
|
|
1271
|
-
set(formStateRef.current.touchedFields, name, true);
|
|
1272
|
-
state.touchedFields = formStateRef.current.touchedFields;
|
|
1273
|
-
}
|
|
1274
|
-
let shouldRender = !isEmptyObject(state) || isWatched;
|
|
1275
|
-
if (shouldSkipValidation) {
|
|
1276
|
-
!isBlurEvent &&
|
|
1277
|
-
watchSubjectRef.current.next({
|
|
1278
|
-
name,
|
|
1279
|
-
type,
|
|
1280
|
-
value: inputValue,
|
|
1281
|
-
});
|
|
1282
|
-
return (shouldRender &&
|
|
1283
|
-
formStateSubjectRef.current.next(isWatched ? {} : state));
|
|
1284
|
-
}
|
|
1285
|
-
formStateSubjectRef.current.next({
|
|
1286
|
-
isValidating: true,
|
|
1287
|
-
});
|
|
1288
|
-
if (resolverRef.current) {
|
|
1289
|
-
const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, defaultValuesRef), contextRef.current, {
|
|
1290
|
-
criteriaMode,
|
|
1291
|
-
fields: getFields([name], fieldsRef.current),
|
|
1292
|
-
names: [name],
|
|
1293
|
-
});
|
|
1294
|
-
const previousFormIsValid = formStateRef.current.isValid;
|
|
1295
|
-
error = get(errors, name);
|
|
1296
|
-
if (isCheckBoxInput(target) && !error) {
|
|
1297
|
-
const parentNodeName = getNodeParentName(name);
|
|
1298
|
-
const currentError = get(errors, parentNodeName, {});
|
|
1299
|
-
currentError.type && currentError.message && (error = currentError);
|
|
1300
|
-
if (currentError ||
|
|
1301
|
-
get(formStateRef.current.errors, parentNodeName)) {
|
|
1302
|
-
name = parentNodeName;
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
isValid = isEmptyObject(errors);
|
|
1306
|
-
previousFormIsValid !== isValid && (shouldRender = true);
|
|
1307
|
-
}
|
|
1308
|
-
else {
|
|
1309
|
-
error = (await validateField(field, isValidateAllFieldCriteria))[name];
|
|
1310
|
-
}
|
|
1311
|
-
!isBlurEvent &&
|
|
1312
|
-
watchSubjectRef.current.next({
|
|
1313
|
-
name,
|
|
1314
|
-
type,
|
|
1315
|
-
value: inputValue,
|
|
1316
|
-
});
|
|
1317
|
-
shouldRenderBaseOnError(name, error, shouldRender, state, isValid, isWatched);
|
|
1318
|
-
}
|
|
1319
|
-
}, []);
|
|
1320
|
-
const getValues = (fieldNames) => {
|
|
1321
|
-
const values = isMountedRef.current
|
|
1322
|
-
? getFieldsValues(fieldsRef, defaultValuesRef)
|
|
1323
|
-
: defaultValuesRef.current;
|
|
1324
|
-
return isUndefined(fieldNames)
|
|
1325
|
-
? values
|
|
1326
|
-
: isString(fieldNames)
|
|
1327
|
-
? get(values, fieldNames)
|
|
1328
|
-
: fieldNames.map((name) => get(values, name));
|
|
1329
|
-
};
|
|
1330
|
-
const updateIsValid = React__namespace.useCallback(async (values = {}) => {
|
|
1331
|
-
const previousIsValid = formStateRef.current.isValid;
|
|
1332
|
-
if (resolver) {
|
|
1333
|
-
const { errors } = await resolverRef.current(Object.assign(Object.assign({}, getFieldsValues(fieldsRef, defaultValuesRef)), values), contextRef.current, {
|
|
1334
|
-
criteriaMode,
|
|
1335
|
-
fields: getFields(fieldsNamesRef.current, fieldsRef.current),
|
|
1336
|
-
});
|
|
1337
|
-
formStateRef.current.isValid = isEmptyObject(errors);
|
|
1338
|
-
}
|
|
1339
|
-
else {
|
|
1340
|
-
getIsValid();
|
|
1341
|
-
}
|
|
1342
|
-
previousIsValid !== formStateRef.current.isValid &&
|
|
1343
|
-
formStateSubjectRef.current.next({
|
|
1344
|
-
isValid: formStateRef.current.isValid,
|
|
1345
|
-
});
|
|
1346
|
-
}, [criteriaMode]);
|
|
1347
|
-
const clearErrors = (name) => {
|
|
1348
|
-
name &&
|
|
1349
|
-
(Array.isArray(name) ? name : [name]).forEach((inputName) => unset(formStateRef.current.errors, inputName));
|
|
1350
|
-
formStateSubjectRef.current.next({
|
|
1351
|
-
errors: name ? formStateRef.current.errors : {},
|
|
1352
|
-
});
|
|
1353
|
-
};
|
|
1354
|
-
const setError = (name, error, options) => {
|
|
1355
|
-
const ref = ((get(fieldsRef.current, name) || { _f: {} })._f || {}).ref;
|
|
1356
|
-
set(formStateRef.current.errors, name, Object.assign(Object.assign({}, error), { ref }));
|
|
1357
|
-
formStateSubjectRef.current.next({
|
|
1358
|
-
errors: formStateRef.current.errors,
|
|
1359
|
-
isValid: false,
|
|
1360
|
-
});
|
|
1361
|
-
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1362
|
-
};
|
|
1363
|
-
const watchInternal = React__namespace.useCallback((fieldNames, defaultValue, isGlobal) => {
|
|
1364
|
-
const isArrayNames = Array.isArray(fieldNames);
|
|
1365
|
-
const fieldValues = isMountedRef.current
|
|
1366
|
-
? getValues()
|
|
1367
|
-
: isUndefined(defaultValue)
|
|
1368
|
-
? defaultValuesRef.current
|
|
1369
|
-
: isArrayNames
|
|
1370
|
-
? defaultValue || {}
|
|
1371
|
-
: { [fieldNames]: defaultValue };
|
|
1372
|
-
if (isUndefined(fieldNames)) {
|
|
1373
|
-
isGlobal && (isWatchAllRef.current = true);
|
|
1374
|
-
return fieldValues;
|
|
1375
|
-
}
|
|
1376
|
-
const result = [];
|
|
1377
|
-
for (const fieldName of isArrayNames ? fieldNames : [fieldNames]) {
|
|
1378
|
-
isGlobal && watchFieldsRef.current.add(fieldName);
|
|
1379
|
-
result.push(get(fieldValues, fieldName));
|
|
1380
|
-
}
|
|
1381
|
-
return isArrayNames ? result : result[0];
|
|
1382
|
-
}, []);
|
|
1383
|
-
const watch = (fieldName, defaultValue) => isFunction(fieldName)
|
|
1384
|
-
? watchSubjectRef.current.subscribe({
|
|
1385
|
-
next: (info) => fieldName(watchInternal(undefined, defaultValue), info),
|
|
1386
|
-
})
|
|
1387
|
-
: watchInternal(fieldName, defaultValue, true);
|
|
1388
|
-
const unregister = (name, options = {}) => {
|
|
1389
|
-
for (const inputName of name
|
|
1390
|
-
? Array.isArray(name)
|
|
1391
|
-
? name
|
|
1392
|
-
: [name]
|
|
1393
|
-
: Object.keys(fieldsNamesRef.current)) {
|
|
1394
|
-
fieldsNamesRef.current.delete(inputName);
|
|
1395
|
-
fieldArrayNamesRef.current.delete(inputName);
|
|
1396
|
-
if (get(fieldsRef.current, inputName)) {
|
|
1397
|
-
if (!options.keepIsValid) {
|
|
1398
|
-
unset(fieldsWithValidationRef.current, inputName);
|
|
1399
|
-
unset(validFieldsRef.current, inputName);
|
|
1400
|
-
}
|
|
1401
|
-
!options.keepError && unset(formStateRef.current.errors, inputName);
|
|
1402
|
-
!options.keepValue && unset(fieldsRef.current, inputName);
|
|
1403
|
-
!options.keepDirty &&
|
|
1404
|
-
unset(formStateRef.current.dirtyFields, inputName);
|
|
1405
|
-
!options.keepTouched &&
|
|
1406
|
-
unset(formStateRef.current.touchedFields, inputName);
|
|
1407
|
-
!options.keepDefaultValue && unset(defaultValuesRef.current, inputName);
|
|
1408
|
-
watchSubjectRef.current.next({
|
|
1409
|
-
name: inputName,
|
|
1410
|
-
});
|
|
1411
|
-
}
|
|
1412
|
-
}
|
|
1413
|
-
formStateSubjectRef.current.next(Object.assign(Object.assign(Object.assign({}, formStateRef.current), (!options.keepDirty ? {} : { isDirty: getFormIsDirty() })), (resolverRef.current ? {} : { isValid: getIsValid() })));
|
|
1414
|
-
if (!options.keepIsValid) {
|
|
1415
|
-
updateIsValid();
|
|
1416
|
-
}
|
|
1417
|
-
};
|
|
1418
|
-
const registerFieldRef = (name, ref, options) => {
|
|
1419
|
-
let field = get(fieldsRef.current, name);
|
|
1420
|
-
if (field) {
|
|
1421
|
-
const isRadioOrCheckbox = isRadioOrCheckboxFunction(ref);
|
|
1422
|
-
if ((isRadioOrCheckbox
|
|
1423
|
-
? Array.isArray(field._f.refs) &&
|
|
1424
|
-
compact(field._f.refs).find((option) => ref.value === option.value && option === ref)
|
|
1425
|
-
: ref === field._f.ref) ||
|
|
1426
|
-
!field ||
|
|
1427
|
-
(isWeb && isHTMLElement(field._f.ref) && !isHTMLElement(ref))) {
|
|
1428
|
-
return;
|
|
1429
|
-
}
|
|
1430
|
-
field = {
|
|
1431
|
-
_f: isRadioOrCheckbox
|
|
1432
|
-
? Object.assign(Object.assign({}, field._f), { refs: [
|
|
1433
|
-
...compact(field._f.refs || []).filter((ref) => isHTMLElement(ref) && document.contains(ref)),
|
|
1434
|
-
ref,
|
|
1435
|
-
], ref: { type: ref.type, name } }) : Object.assign(Object.assign({}, field._f), { ref }),
|
|
1436
|
-
};
|
|
1437
|
-
set(fieldsRef.current, name, field);
|
|
1438
|
-
const defaultValue = updateValueAndGetDefault(name);
|
|
1439
|
-
if (isRadioOrCheckbox && Array.isArray(defaultValue)
|
|
1440
|
-
? !deepEqual(get(fieldsRef.current, name)._f.value, defaultValue)
|
|
1441
|
-
: isUndefined(get(fieldsRef.current, name)._f.value)) {
|
|
1442
|
-
get(fieldsRef.current, name)._f.value = getFieldValue(get(fieldsRef.current, name));
|
|
1443
|
-
}
|
|
1444
|
-
if (options) {
|
|
1445
|
-
if (!validationMode.isOnSubmit &&
|
|
1446
|
-
field &&
|
|
1447
|
-
readFormStateRef.current.isValid) {
|
|
1448
|
-
validateField(field, isValidateAllFieldCriteria).then((error) => {
|
|
1449
|
-
isEmptyObject(error)
|
|
1450
|
-
? set(validFieldsRef.current, name, true)
|
|
1451
|
-
: unset(validFieldsRef.current, name);
|
|
1452
|
-
formStateRef.current.isValid &&
|
|
1453
|
-
!isEmptyObject(error) &&
|
|
1454
|
-
setFormState(Object.assign(Object.assign({}, formStateRef.current), { isValid: getIsValid() }));
|
|
1455
|
-
});
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
};
|
|
1460
|
-
const register = React__namespace.useCallback((name, options) => {
|
|
1461
|
-
const isInitialRegister = !get(fieldsRef.current, name);
|
|
1462
|
-
set(fieldsRef.current, name, {
|
|
1463
|
-
_f: Object.assign(Object.assign(Object.assign({}, (isInitialRegister
|
|
1464
|
-
? { ref: { name } }
|
|
1465
|
-
: Object.assign({ ref: (get(fieldsRef.current, name)._f || {}).ref }, get(fieldsRef.current, name)._f))), { name }), options),
|
|
1466
|
-
});
|
|
1467
|
-
options && set(fieldsWithValidationRef.current, name, true);
|
|
1468
|
-
fieldsNamesRef.current.add(name);
|
|
1469
|
-
isInitialRegister && updateValueAndGetDefault(name);
|
|
1470
|
-
return isWindowUndefined
|
|
1471
|
-
? { name: name }
|
|
1472
|
-
: {
|
|
1473
|
-
name,
|
|
1474
|
-
onChange: handleChange,
|
|
1475
|
-
onBlur: handleChange,
|
|
1476
|
-
ref: (ref) => ref && registerFieldRef(name, ref, options),
|
|
1477
|
-
};
|
|
1478
|
-
}, [defaultValuesRef.current]);
|
|
1479
|
-
const handleSubmit = React__namespace.useCallback((onValid, onInvalid) => async (e) => {
|
|
1480
|
-
if (e && e.preventDefault) {
|
|
1481
|
-
e.preventDefault();
|
|
1482
|
-
e.persist();
|
|
1483
|
-
}
|
|
1484
|
-
let fieldValues = Object.assign(Object.assign({}, defaultValuesRef.current), getFieldsValues(fieldsRef, defaultValuesRef));
|
|
1485
|
-
formStateSubjectRef.current.next({
|
|
1486
|
-
isSubmitting: true,
|
|
1487
|
-
});
|
|
1488
|
-
try {
|
|
1489
|
-
if (resolverRef.current) {
|
|
1490
|
-
const { errors, values } = await resolverRef.current(fieldValues, contextRef.current, {
|
|
1491
|
-
criteriaMode,
|
|
1492
|
-
fields: getFields(fieldsNamesRef.current, fieldsRef.current),
|
|
1493
|
-
});
|
|
1494
|
-
formStateRef.current.errors = errors;
|
|
1495
|
-
fieldValues = values;
|
|
1496
|
-
}
|
|
1497
|
-
else {
|
|
1498
|
-
await validateForm(fieldsRef.current);
|
|
1499
|
-
}
|
|
1500
|
-
if (isEmptyObject(formStateRef.current.errors) &&
|
|
1501
|
-
Object.keys(formStateRef.current.errors).every((name) => get(fieldValues, name))) {
|
|
1502
|
-
formStateSubjectRef.current.next({
|
|
1503
|
-
errors: {},
|
|
1504
|
-
isSubmitting: true,
|
|
1505
|
-
});
|
|
1506
|
-
await onValid(fieldValues, e);
|
|
1507
|
-
}
|
|
1508
|
-
else {
|
|
1509
|
-
onInvalid && (await onInvalid(formStateRef.current.errors, e));
|
|
1510
|
-
shouldFocusError &&
|
|
1511
|
-
focusFieldBy(fieldsRef.current, (key) => get(formStateRef.current.errors, key), fieldsNamesRef.current);
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
finally {
|
|
1515
|
-
formStateRef.current.isSubmitted = true;
|
|
1516
|
-
formStateSubjectRef.current.next({
|
|
1517
|
-
isSubmitted: true,
|
|
1518
|
-
isSubmitting: false,
|
|
1519
|
-
isSubmitSuccessful: isEmptyObject(formStateRef.current.errors),
|
|
1520
|
-
submitCount: formStateRef.current.submitCount + 1,
|
|
1521
|
-
errors: formStateRef.current.errors,
|
|
1522
|
-
});
|
|
1523
|
-
}
|
|
1524
|
-
}, [shouldFocusError, isValidateAllFieldCriteria, criteriaMode]);
|
|
1525
|
-
const resetFromState = React__namespace.useCallback(({ keepErrors, keepDirty, keepIsSubmitted, keepTouched, keepIsValid, keepSubmitCount, }) => {
|
|
1526
|
-
if (!keepIsValid) {
|
|
1527
|
-
validFieldsRef.current = {};
|
|
1528
|
-
fieldsWithValidationRef.current = {};
|
|
1529
|
-
}
|
|
1530
|
-
watchFieldsRef.current = new Set();
|
|
1531
|
-
isWatchAllRef.current = false;
|
|
1532
|
-
formStateSubjectRef.current.next({
|
|
1533
|
-
submitCount: keepSubmitCount ? formStateRef.current.submitCount : 0,
|
|
1534
|
-
isDirty: keepDirty ? formStateRef.current.isDirty : false,
|
|
1535
|
-
isSubmitted: keepIsSubmitted ? formStateRef.current.isSubmitted : false,
|
|
1536
|
-
isValid: keepIsValid
|
|
1537
|
-
? formStateRef.current.isValid
|
|
1538
|
-
: !validationMode.isOnSubmit,
|
|
1539
|
-
dirtyFields: keepDirty ? formStateRef.current.dirtyFields : {},
|
|
1540
|
-
touchedFields: keepTouched ? formStateRef.current.touchedFields : {},
|
|
1541
|
-
errors: keepErrors ? formStateRef.current.errors : {},
|
|
1542
|
-
isSubmitting: false,
|
|
1543
|
-
isSubmitSuccessful: false,
|
|
1544
|
-
});
|
|
1545
|
-
}, []);
|
|
1546
|
-
const reset = (values, keepStateOptions = {}) => {
|
|
1547
|
-
const updatedValues = values || defaultValuesRef.current;
|
|
1548
|
-
if (isWeb && !keepStateOptions.keepValues) {
|
|
1549
|
-
for (const field of Object.values(fieldsRef.current)) {
|
|
1550
|
-
if (field && field._f) {
|
|
1551
|
-
const inputRef = Array.isArray(field._f.refs)
|
|
1552
|
-
? field._f.refs[0]
|
|
1553
|
-
: field._f.ref;
|
|
1554
|
-
if (isHTMLElement(inputRef)) {
|
|
1555
|
-
try {
|
|
1556
|
-
inputRef.closest('form').reset();
|
|
1557
|
-
break;
|
|
1558
|
-
}
|
|
1559
|
-
catch (_a) { }
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
!keepStateOptions.keepDefaultValues &&
|
|
1565
|
-
(defaultValuesRef.current = Object.assign({}, updatedValues));
|
|
1566
|
-
if (!keepStateOptions.keepValues) {
|
|
1567
|
-
fieldsRef.current = {};
|
|
1568
|
-
controllerSubjectRef.current.next({
|
|
1569
|
-
values: Object.assign({}, updatedValues),
|
|
1570
|
-
});
|
|
1571
|
-
watchSubjectRef.current.next({
|
|
1572
|
-
value: Object.assign({}, updatedValues),
|
|
1573
|
-
});
|
|
1574
|
-
fieldArraySubjectRef.current.next({
|
|
1575
|
-
fields: Object.assign({}, updatedValues),
|
|
1576
|
-
isReset: true,
|
|
1577
|
-
});
|
|
1578
|
-
}
|
|
1579
|
-
resetFromState(keepStateOptions);
|
|
1580
|
-
};
|
|
1581
|
-
React__namespace.useEffect(() => {
|
|
1582
|
-
isMountedRef.current = true;
|
|
1583
|
-
const formStateSubscription = formStateSubjectRef.current.subscribe({
|
|
1584
|
-
next(formState = {}) {
|
|
1585
|
-
if (shouldRenderFormState(formState, readFormStateRef.current, true)) {
|
|
1586
|
-
formStateRef.current = Object.assign(Object.assign({}, formStateRef.current), formState);
|
|
1587
|
-
setFormState(formStateRef.current);
|
|
1588
|
-
}
|
|
1589
|
-
},
|
|
1590
|
-
});
|
|
1591
|
-
const useFieldArraySubscription = fieldArraySubjectRef.current.subscribe({
|
|
1592
|
-
next(state) {
|
|
1593
|
-
if (state.fields && state.name && readFormStateRef.current.isValid) {
|
|
1594
|
-
const values = getFieldsValues(fieldsRef);
|
|
1595
|
-
set(values, state.name, state.fields);
|
|
1596
|
-
updateIsValid(values);
|
|
1597
|
-
}
|
|
1598
|
-
},
|
|
1599
|
-
});
|
|
1600
|
-
resolverRef.current && readFormStateRef.current.isValid && updateIsValid();
|
|
1601
|
-
return () => {
|
|
1602
|
-
watchSubjectRef.current.unsubscribe();
|
|
1603
|
-
formStateSubscription.unsubscribe();
|
|
1604
|
-
useFieldArraySubscription.unsubscribe();
|
|
1605
|
-
};
|
|
1606
|
-
}, []);
|
|
1607
|
-
return {
|
|
1608
|
-
control: React__namespace.useMemo(() => ({
|
|
1609
|
-
register,
|
|
1610
|
-
isWatchAllRef,
|
|
1611
|
-
watchFieldsRef,
|
|
1612
|
-
getFormIsDirty,
|
|
1613
|
-
formStateSubjectRef,
|
|
1614
|
-
fieldArraySubjectRef,
|
|
1615
|
-
controllerSubjectRef,
|
|
1616
|
-
watchSubjectRef,
|
|
1617
|
-
watchInternal,
|
|
1618
|
-
fieldsRef,
|
|
1619
|
-
validFieldsRef,
|
|
1620
|
-
fieldsWithValidationRef,
|
|
1621
|
-
fieldArrayNamesRef,
|
|
1622
|
-
readFormStateRef,
|
|
1623
|
-
formStateRef,
|
|
1624
|
-
defaultValuesRef,
|
|
1625
|
-
fieldArrayDefaultValuesRef,
|
|
1626
|
-
}), []),
|
|
1627
|
-
formState: getProxyFormState(isProxyEnabled, formState, readFormStateRef),
|
|
1628
|
-
trigger,
|
|
1629
|
-
register,
|
|
1630
|
-
handleSubmit,
|
|
1631
|
-
watch: React__namespace.useCallback(watch, []),
|
|
1632
|
-
setValue: React__namespace.useCallback(setValue, [setInternalValues]),
|
|
1633
|
-
getValues: React__namespace.useCallback(getValues, []),
|
|
1634
|
-
reset: React__namespace.useCallback(reset, []),
|
|
1635
|
-
clearErrors: React__namespace.useCallback(clearErrors, []),
|
|
1636
|
-
unregister: React__namespace.useCallback(unregister, []),
|
|
1637
|
-
setError: React__namespace.useCallback(setError, []),
|
|
1638
|
-
};
|
|
1639
|
-
}
|
|
1640
|
-
|
|
1641
|
-
function useWatch(props) {
|
|
1642
|
-
const { control, name, defaultValue } = props || {};
|
|
1643
|
-
const methods = useFormContext();
|
|
1644
|
-
const { watchInternal, watchSubjectRef } = control || methods.control;
|
|
1645
|
-
const [value, updateValue] = React__namespace.useState(isUndefined(defaultValue)
|
|
1646
|
-
? watchInternal(name)
|
|
1647
|
-
: defaultValue);
|
|
1648
|
-
React__namespace.useEffect(() => {
|
|
1649
|
-
watchInternal(name);
|
|
1650
|
-
const watchSubscription = watchSubjectRef.current.subscribe({
|
|
1651
|
-
next: ({ name: inputName, value }) => (!name ||
|
|
1652
|
-
!inputName ||
|
|
1653
|
-
(Array.isArray(name) ? name : [name]).some((fieldName) => inputName &&
|
|
1654
|
-
fieldName &&
|
|
1655
|
-
inputName.startsWith(fieldName))) &&
|
|
1656
|
-
updateValue(isString(inputName) && name === inputName && !isUndefined(value)
|
|
1657
|
-
? value
|
|
1658
|
-
: watchInternal(name, defaultValue)),
|
|
1659
|
-
});
|
|
1660
|
-
return () => watchSubscription.unsubscribe();
|
|
1661
|
-
}, []);
|
|
1662
|
-
return value;
|
|
1663
|
-
}
|
|
1664
|
-
|
|
1665
|
-
exports.Controller = Controller;
|
|
1666
|
-
exports.FormProvider = FormProvider;
|
|
1667
|
-
exports.appendErrors = appendErrors;
|
|
1668
|
-
exports.get = get;
|
|
1669
|
-
exports.set = set;
|
|
1670
|
-
exports.useController = useController;
|
|
1671
|
-
exports.useFieldArray = useFieldArray;
|
|
1672
|
-
exports.useForm = useForm;
|
|
1673
|
-
exports.useFormContext = useFormContext;
|
|
1674
|
-
exports.useFormState = useFormState;
|
|
1675
|
-
exports.useWatch = useWatch;
|
|
1676
|
-
//# sourceMappingURL=index.cjs.development.js.map
|