import React from 'react';
var isCheckBoxInput = (element) => element.type === 'checkbox';
var isDateObject = (value) => value instanceof Date;
var isNullOrUndefined = (value) => value == null;
const isObjectType = (value) => typeof value === 'object';
var isObject = (value) => !isNullOrUndefined(value) &&
!Array.isArray(value) &&
isObjectType(value) &&
!isDateObject(value);
var getEventValue = (event) => isObject(event) && event.target
? isCheckBoxInput(event.target)
? event.target.checked
: event.target.value
: event;
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
var isUndefined = (val) => val === undefined;
var get = (obj, path, defaultValue) => {
if (!path || !isObject(obj)) {
return defaultValue;
}
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
return isUndefined(result) || result === obj
? isUndefined(obj[path])
? defaultValue
: obj[path]
: result;
};
const EVENTS = {
BLUR: 'blur',
FOCUS_OUT: 'focusout',
CHANGE: 'change',
};
const VALIDATION_MODE = {
onBlur: 'onBlur',
onChange: 'onChange',
onSubmit: 'onSubmit',
onTouched: 'onTouched',
all: 'all',
};
const INPUT_VALIDATION_RULES = {
max: 'max',
min: 'min',
maxLength: 'maxLength',
minLength: 'minLength',
pattern: 'pattern',
required: 'required',
validate: 'validate',
};
const HookFormContext = React.createContext(null);
/**
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
*
* @remarks
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
*
* @returns return all useForm methods
*
* @example
* ```tsx
* function App() {
* const methods = useForm();
* const onSubmit = data => console.log(data);
*
* return (
*
*
*
* );
* }
*
* function NestedInput() {
* const { register } = useFormContext(); // retrieve all hook methods
* return ;
* }
* ```
*/
const useFormContext = () => React.useContext(HookFormContext);
/**
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
*
* @remarks
* [API](https://react-hook-form.com/api/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
*
* @param props - all useFrom methods
*
* @example
* ```tsx
* function App() {
* const methods = useForm();
* const onSubmit = data => console.log(data);
*
* return (
*
*
*
* );
* }
*
* function NestedInput() {
* const { register } = useFormContext(); // retrieve all hook methods
* return ;
* }
* ```
*/
const FormProvider = (props) => {
const { children, ...data } = props;
return (React.createElement(HookFormContext.Provider, { value: data }, children));
};
var getProxyFormState = (formState, _proxyFormState, localProxyFormState, isRoot = true) => {
const result = {};
for (const key in formState) {
Object.defineProperty(result, key, {
get: () => {
const _key = key;
if (_proxyFormState[_key] !== VALIDATION_MODE.all) {
_proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
}
localProxyFormState && (localProxyFormState[_key] = true);
return formState[_key];
},
});
}
return result;
};
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
const { name, ...formState } = formStateData;
return (isEmptyObject(formState) ||
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
Object.keys(formState).find((key) => _proxyFormState[key] ===
(!isRoot || VALIDATION_MODE.all)));
};
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
? name === signalName
: !name ||
!signalName ||
name === signalName ||
convertToArrayPayload(name).some((currentName) => currentName &&
(currentName.startsWith(signalName) ||
signalName.startsWith(currentName)));
function useSubscribe(props) {
const _props = React.useRef(props);
_props.current = props;
React.useEffect(() => {
const tearDown = (subscription) => {
if (subscription) {
subscription.unsubscribe();
}
};
const subscription = !props.disabled &&
_props.current.subject.subscribe({
next: _props.current.callback,
});
return () => tearDown(subscription);
}, [props.disabled]);
}
/**
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
*
* @remarks
* [API](https://react-hook-form.com/api/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
*
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
*
* @example
* ```tsx
* function App() {
* const { register, handleSubmit, control } = useForm({
* defaultValues: {
* firstName: "firstName"
* }});
* const { dirtyFields } = useFormState({
* control
* });
* const onSubmit = (data) => console.log(data);
*
* return (
*
* );
* }
* ```
*/
function useFormState(props) {
const methods = useFormContext();
const { control = methods.control, disabled, name, exact } = props || {};
const [formState, updateFormState] = React.useState(control._formState);
const _localProxyFormState = React.useRef({
isDirty: false,
dirtyFields: false,
touchedFields: false,
isValidating: false,
isValid: false,
errors: false,
});
const _name = React.useRef(name);
const _mounted = React.useRef(true);
_name.current = name;
const callback = React.useCallback((value) => _mounted.current &&
shouldSubscribeByName(_name.current, value.name, exact) &&
shouldRenderFormState(value, _localProxyFormState.current) &&
updateFormState({
...control._formState,
...value,
}), [control, exact]);
useSubscribe({
disabled,
callback,
subject: control._subjects.state,
});
React.useEffect(() => {
_mounted.current = true;
return () => {
_mounted.current = false;
};
}, []);
return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
}
var isString = (value) => typeof value === 'string';
var generateWatchOutput = (names, _names, formValues, isGlobal) => {
const isArray = Array.isArray(names);
if (isString(names)) {
isGlobal && _names.watch.add(names);
return get(formValues, names);
}
if (isArray) {
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
get(formValues, fieldName)));
}
isGlobal && (_names.watchAll = true);
return formValues;
};
var isFunction = (value) => typeof value === 'function';
var objectHasFunction = (data) => {
for (const key in data) {
if (isFunction(data[key])) {
return true;
}
}
return false;
};
/**
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
*
* @remarks
*
* [API](https://react-hook-form.com/api/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
*
* @example
* ```tsx
* const { watch } = useForm();
* const values = useWatch({
* name: "fieldName"
* control,
* })
* ```
*/
function useWatch(props) {
const methods = useFormContext();
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
const _name = React.useRef(name);
_name.current = name;
const callback = React.useCallback((formState) => {
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
updateValue(isUndefined(_name.current) ||
(isObject(fieldValues) && !objectHasFunction(fieldValues))
? { ...fieldValues }
: Array.isArray(fieldValues)
? [...fieldValues]
: isUndefined(fieldValues)
? defaultValue
: fieldValues);
}
}, [control, exact, defaultValue]);
useSubscribe({
disabled,
subject: control._subjects.watch,
callback,
});
const [value, updateValue] = React.useState(isUndefined(defaultValue)
? control._getWatch(name)
: defaultValue);
React.useEffect(() => {
control._removeUnmounted();
});
return value;
}
/**
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
*
* @remarks
* [API](https://react-hook-form.com/api/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
*
* @param props - the path name to the form field value, and validation rules.
*
* @returns field properties, field and form state. {@link UseControllerReturn}
*
* @example
* ```tsx
* function Input(props) {
* const { field, fieldState, formState } = useController(props);
* return (
*
*
*
{fieldState.isTouched && "Touched"}
*
{formState.isSubmitted ? "submitted" : ""}
*
* );
* }
* ```
*/
function useController(props) {
const methods = useFormContext();
const { name, control = methods.control, shouldUnregister } = props;
const isArrayField = isNameInFieldArray(control._names.array, name);
const value = useWatch({
control,
name,
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
exact: true,
});
const formState = useFormState({
control,
name,
});
const _registerProps = React.useRef(control.register(name, {
...props.rules,
value,
}));
React.useEffect(() => {
const updateMounted = (name, value) => {
const field = get(control._fields, name);
if (field) {
field._f.mount = value;
}
};
updateMounted(name, true);
return () => {
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
(isArrayField
? _shouldUnregisterField && !control._stateFlags.action
: _shouldUnregisterField)
? control.unregister(name)
: updateMounted(name, false);
};
}, [name, control, isArrayField, shouldUnregister]);
return {
field: {
name,
value,
onChange: React.useCallback((event) => {
_registerProps.current.onChange({
target: {
value: getEventValue(event),
name: name,
},
type: EVENTS.CHANGE,
});
}, [name]),
onBlur: React.useCallback(() => {
_registerProps.current.onBlur({
target: {
value: get(control._formValues, name),
name: name,
},
type: EVENTS.BLUR,
});
}, [name, control]),
ref: React.useCallback((elm) => {
const field = get(control._fields, name);
if (elm && field && elm.focus) {
field._f.ref = {
focus: () => elm.focus(),
select: () => elm.select(),
setCustomValidity: (message) => elm.setCustomValidity(message),
reportValidity: () => elm.reportValidity(),
};
}
}, [name, control._fields]),
},
formState,
fieldState: Object.defineProperties({}, {
invalid: {
get: () => !!get(formState.errors, name),
},
isDirty: {
get: () => !!get(formState.dirtyFields, name),
},
isTouched: {
get: () => !!get(formState.touchedFields, name),
},
error: {
get: () => get(formState.errors, name),
},
}),
};
}
/**
* Component based on `useController` hook to work with controlled component.
*
* @remarks
* [API](https://react-hook-form.com/api/usecontroller/controller) • [Demo](https://codesandbox.io/s/react-hook-form-v6-controller-ts-jwyzw) • [Video](https://www.youtube.com/watch?v=N2UNk_UCVyA)
*
* @param props - the path name to the form field value, and validation rules.
*
* @returns provide field handler functions, field and form state.
*
* @example
* ```tsx
* function App() {
* const { control } = useForm({
* defaultValues: {
* test: ""
* }
* });
*
* return (
*
* );
* }
* ```
*/
const Controller = (props) => props.render(useController(props));
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
? {
...errors[name],
types: {
...(errors[name] && errors[name].types ? errors[name].types : {}),
[type]: message || true,
},
}
: {};
var isKey = (value) => /^\w*$/.test(value);
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
function set(object, path, value) {
let index = -1;
const tempPath = isKey(path) ? [path] : stringToPath(path);
const length = tempPath.length;
const lastIndex = length - 1;
while (++index < length) {
const key = tempPath[index];
let newValue = value;
if (index !== lastIndex) {
const objValue = object[key];
newValue =
isObject(objValue) || Array.isArray(objValue)
? objValue
: !isNaN(+tempPath[index + 1])
? []
: {};
}
object[key] = newValue;
object = object[key];
}
return object;
}
const focusFieldBy = (fields, callback, fieldsNames) => {
for (const key of fieldsNames || Object.keys(fields)) {
const field = get(fields, key);
if (field) {
const { _f, ...currentField } = field;
if (_f && callback(_f.name)) {
if (_f.ref.focus && isUndefined(_f.ref.focus())) {
break;
}
else if (_f.refs) {
_f.refs[0].focus();
break;
}
}
else if (isObject(currentField)) {
focusFieldBy(currentField, callback);
}
}
}
};
var generateId = () => {
const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16 + d) % 16 | 0;
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
};
var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined(options.shouldFocus)
? options.focusName ||
`${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.`
: '';
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
(_names.watchAll ||
_names.watch.has(name) ||
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
/^\.\w+/.test(name.slice(watchName.length))));
function append(data, value) {
return [...data, ...convertToArrayPayload(value)];
}
var isWeb = typeof window !== 'undefined' &&
typeof window.HTMLElement !== 'undefined' &&
typeof document !== 'undefined';
function cloneObject(data) {
let copy;
const isArray = Array.isArray(data);
if (data instanceof Date) {
copy = new Date(data);
}
else if (data instanceof Set) {
copy = new Set(data);
}
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
(isArray || isObject(data))) {
copy = isArray ? [] : {};
for (const key in data) {
if (isFunction(data[key])) {
copy = data;
break;
}
copy[key] = cloneObject(data[key]);
}
}
else {
return data;
}
return copy;
}
var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => undefined) : undefined;
function insert(data, index, value) {
return [
...data.slice(0, index),
...convertToArrayPayload(value),
...data.slice(index),
];
}
var moveArrayAt = (data, from, to) => {
if (!Array.isArray(data)) {
return [];
}
if (isUndefined(data[to])) {
data[to] = undefined;
}
data.splice(to, 0, data.splice(from, 1)[0]);
return data;
};
function prepend(data, value) {
return [...convertToArrayPayload(value), ...convertToArrayPayload(data)];
}
function removeAtIndexes(data, indexes) {
let i = 0;
const temp = [...data];
for (const index of indexes) {
temp.splice(index - i, 1);
i++;
}
return compact(temp).length ? temp : [];
}
var removeArrayAt = (data, index) => isUndefined(index)
? []
: removeAtIndexes(data, convertToArrayPayload(index).sort((a, b) => a - b));
var swapArrayAt = (data, indexA, indexB) => {
data[indexA] = [data[indexB], (data[indexB] = data[indexA])][0];
};
function baseGet(object, updatePath) {
const length = updatePath.slice(0, -1).length;
let index = 0;
while (index < length) {
object = isUndefined(object) ? index++ : object[updatePath[index++]];
}
return object;
}
function unset(object, path) {
const updatePath = isKey(path) ? [path] : stringToPath(path);
const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
const key = updatePath[updatePath.length - 1];
let previousObjRef;
if (childObject) {
delete childObject[key];
}
for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
let index = -1;
let objectRef;
const currentPaths = updatePath.slice(0, -(k + 1));
const currentPathsLength = currentPaths.length - 1;
if (k > 0) {
previousObjRef = object;
}
while (++index < currentPaths.length) {
const item = currentPaths[index];
objectRef = objectRef ? objectRef[item] : object[item];
if (currentPathsLength === index &&
((isObject(objectRef) && isEmptyObject(objectRef)) ||
(Array.isArray(objectRef) &&
!objectRef.filter((data) => !isUndefined(data)).length))) {
previousObjRef ? delete previousObjRef[item] : delete object[item];
}
previousObjRef = objectRef;
}
}
return object;
}
var updateAt = (fieldValues, index, value) => {
fieldValues[index] = value;
return fieldValues;
};
/**
* A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc.
*
* @remarks
* [API](https://react-hook-form.com/api/usefieldarray) • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn)
*
* @param props - useFieldArray props
*
* @returns methods - functions to manipulate with the Field Arrays (dynamic inputs) {@link UseFieldArrayReturn}
*
* @example
* ```tsx
* function App() {
* const { register, control, handleSubmit, reset, trigger, setError } = useForm({
* defaultValues: {
* test: []
* }
* });
* const { fields, append } = useFieldArray({
* control,
* name: "test"
* });
*
* return (
*
* );
* }
* ```
*/
function useFieldArray(props) {
const methods = useFormContext();
const { control = methods.control, name, keyName = 'id', shouldUnregister, } = props;
const [fields, setFields] = React.useState(control._getFieldArray(name));
const ids = React.useRef(control._getFieldArray(name).map(generateId));
const _fieldIds = React.useRef(fields);
const _name = React.useRef(name);
const _actioned = React.useRef(false);
_name.current = name;
_fieldIds.current = fields;
control._names.array.add(name);
const callback = React.useCallback(({ values, name: fieldArrayName, }) => {
if (fieldArrayName === _name.current || !fieldArrayName) {
const fieldValues = get(values, _name.current, []);
setFields(fieldValues);
ids.current = fieldValues.map(generateId);
}
}, []);
useSubscribe({
callback,
subject: control._subjects.array,
});
const updateValues = React.useCallback((updatedFieldArrayValues) => {
_actioned.current = true;
control._updateFieldArray(name, updatedFieldArrayValues);
}, [control, name]);
const append$1 = (value, options) => {
const appendValue = convertToArrayPayload(cloneObject(value));
const updatedFieldArrayValues = append(control._getFieldArray(name), appendValue);
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
ids.current = append(ids.current, appendValue.map(generateId));
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, append, {
argA: fillEmptyArray(value),
});
};
const prepend$1 = (value, options) => {
const prependValue = convertToArrayPayload(cloneObject(value));
const updatedFieldArrayValues = prepend(control._getFieldArray(name), prependValue);
control._names.focus = getFocusFieldName(name, 0, options);
ids.current = prepend(ids.current, prependValue.map(generateId));
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, prepend, {
argA: fillEmptyArray(value),
});
};
const remove = (index) => {
const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
ids.current = removeArrayAt(ids.current, index);
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
argA: index,
});
};
const insert$1 = (index, value, options) => {
const insertValue = convertToArrayPayload(cloneObject(value));
const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
control._names.focus = getFocusFieldName(name, index, options);
ids.current = insert(ids.current, index, insertValue.map(generateId));
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, insert, {
argA: index,
argB: fillEmptyArray(value),
});
};
const swap = (indexA, indexB) => {
const updatedFieldArrayValues = control._getFieldArray(name);
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
swapArrayAt(ids.current, indexA, indexB);
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
argA: indexA,
argB: indexB,
}, false);
};
const move = (from, to) => {
const updatedFieldArrayValues = control._getFieldArray(name);
moveArrayAt(updatedFieldArrayValues, from, to);
moveArrayAt(ids.current, from, to);
updateValues(updatedFieldArrayValues);
setFields(updatedFieldArrayValues);
control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
argA: from,
argB: to,
}, false);
};
const update = (index, value) => {
const updateValue = cloneObject(value);
const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
ids.current = [...updatedFieldArrayValues].map((item, i) => !item || i === index ? generateId() : ids.current[i]);
updateValues(updatedFieldArrayValues);
setFields([...updatedFieldArrayValues]);
control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
argA: index,
argB: updateValue,
}, true, false);
};
const replace = (value) => {
const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
ids.current = updatedFieldArrayValues.map(generateId);
updateValues([...updatedFieldArrayValues]);
setFields([...updatedFieldArrayValues]);
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
};
React.useEffect(() => {
control._stateFlags.action = false;
isWatched(name, control._names) && control._subjects.state.next({});
if (_actioned.current) {
control._executeSchema([name]).then((result) => {
const error = get(result.errors, name);
const existingError = get(control._formState.errors, name);
if (existingError ? !error && existingError.type : error && error.type) {
error
? set(control._formState.errors, name, error)
: unset(control._formState.errors, name);
control._subjects.state.next({
errors: control._formState.errors,
});
}
});
}
control._subjects.watch.next({
name,
values: control._formValues,
});
control._names.focus &&
focusFieldBy(control._fields, (key) => key.startsWith(control._names.focus));
control._names.focus = '';
control._proxyFormState.isValid && control._updateValid();
}, [fields, name, control]);
React.useEffect(() => {
!get(control._formValues, name) && control._updateFieldArray(name);
return () => {
(control._options.shouldUnregister || shouldUnregister) &&
control.unregister(name);
};
}, [name, control, keyName, shouldUnregister]);
return {
swap: React.useCallback(swap, [updateValues, name, control]),
move: React.useCallback(move, [updateValues, name, control]),
prepend: React.useCallback(prepend$1, [updateValues, name, control]),
append: React.useCallback(append$1, [updateValues, name, control]),
remove: React.useCallback(remove, [updateValues, name, control]),
insert: React.useCallback(insert$1, [updateValues, name, control]),
update: React.useCallback(update, [updateValues, name, control]),
replace: React.useCallback(replace, [updateValues, name, control]),
fields: React.useMemo(() => fields.map((field, index) => ({
...field,
[keyName]: ids.current[index] || generateId(),
})), [fields, keyName]),
};
}
function createSubject() {
let _observers = [];
const next = (value) => {
for (const observer of _observers) {
observer.next(value);
}
};
const subscribe = (observer) => {
_observers.push(observer);
return {
unsubscribe: () => {
_observers = _observers.filter((o) => o !== observer);
},
};
};
const unsubscribe = () => {
_observers = [];
};
return {
get observers() {
return _observers;
},
next,
subscribe,
unsubscribe,
};
}
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
function deepEqual(object1, object2) {
if (isPrimitive(object1) || isPrimitive(object2)) {
return object1 === object2;
}
if (isDateObject(object1) && isDateObject(object2)) {
return object1.getTime() === object2.getTime();
}
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1 = object1[key];
if (!keys2.includes(key)) {
return false;
}
if (key !== 'ref') {
const val2 = object2[key];
if ((isDateObject(val1) && isDateObject(val2)) ||
(isObject(val1) && isObject(val2)) ||
(Array.isArray(val1) && Array.isArray(val2))
? !deepEqual(val1, val2)
: val1 !== val2) {
return false;
}
}
}
return true;
}
var getValidationModes = (mode) => ({
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
isOnBlur: mode === VALIDATION_MODE.onBlur,
isOnChange: mode === VALIDATION_MODE.onChange,
isOnAll: mode === VALIDATION_MODE.all,
isOnTouch: mode === VALIDATION_MODE.onTouched,
});
var isBoolean = (value) => typeof value === 'boolean';
var isFileInput = (element) => element.type === 'file';
var isHTMLElement = (value) => {
const owner = value ? value.ownerDocument : 0;
const ElementClass = owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement;
return value instanceof ElementClass;
};
var isMultipleSelect = (element) => element.type === `select-multiple`;
var isRadioInput = (element) => element.type === 'radio';
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
function markFieldsDirty(data, fields = {}) {
const isParentNodeArray = Array.isArray(data);
if (isObject(data) || isParentNodeArray) {
for (const key in data) {
if (Array.isArray(data[key]) ||
(isObject(data[key]) && !objectHasFunction(data[key]))) {
fields[key] = Array.isArray(data[key]) ? [] : {};
markFieldsDirty(data[key], fields[key]);
}
else if (!isNullOrUndefined(data[key])) {
fields[key] = true;
}
}
}
return fields;
}
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
const isParentNodeArray = Array.isArray(data);
if (isObject(data) || isParentNodeArray) {
for (const key in data) {
if (Array.isArray(data[key]) ||
(isObject(data[key]) && !objectHasFunction(data[key]))) {
if (isUndefined(formValues) ||
isPrimitive(dirtyFieldsFromValues[key])) {
dirtyFieldsFromValues[key] = Array.isArray(data[key])
? markFieldsDirty(data[key], [])
: { ...markFieldsDirty(data[key]) };
}
else {
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
}
}
else {
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
}
}
}
return dirtyFieldsFromValues;
}
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
const defaultResult = {
value: false,
isValid: false,
};
const validResult = { value: true, isValid: true };
var getCheckboxValue = (options) => {
if (Array.isArray(options)) {
if (options.length > 1) {
const values = options
.filter((option) => option && option.checked && !option.disabled)
.map((option) => option.value);
return { value: values, isValid: !!values.length };
}
return options[0].checked && !options[0].disabled
? // @ts-expect-error expected to work in the browser
options[0].attributes && !isUndefined(options[0].attributes.value)
? isUndefined(options[0].value) || options[0].value === ''
? validResult
: { value: options[0].value, isValid: true }
: validResult
: defaultResult;
}
return defaultResult;
};
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
? value
: valueAsNumber
? value === '' || isNullOrUndefined(value)
? NaN
: +value
: valueAsDate && isString(value)
? new Date(value)
: setValueAs
? setValueAs(value)
: value;
const defaultReturn = {
isValid: false,
value: null,
};
var getRadioValue = (options) => Array.isArray(options)
? options.reduce((previous, option) => option && option.checked && !option.disabled
? {
isValid: true,
value: option.value,
}
: previous, defaultReturn)
: defaultReturn;
function getFieldValue(_f) {
const ref = _f.ref;
if (_f.refs ? _f.refs.every((ref) => ref.disabled) : ref.disabled) {
return;
}
if (isFileInput(ref)) {
return ref.files;
}
if (isRadioInput(ref)) {
return getRadioValue(_f.refs).value;
}
if (isMultipleSelect(ref)) {
return [...ref.selectedOptions].map(({ value }) => value);
}
if (isCheckBoxInput(ref)) {
return getCheckboxValue(_f.refs).value;
}
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
}
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
const fields = {};
for (const name of fieldsNames) {
const field = get(_fields, name);
field && set(fields, name, field._f);
}
return {
criteriaMode,
names: [...fieldsNames],
fields,
shouldUseNativeValidation,
};
};
var isRegex = (value) => value instanceof RegExp;
var getRuleValue = (rule) => isUndefined(rule)
? undefined
: isRegex(rule)
? rule.source
: isObject(rule)
? isRegex(rule.value)
? rule.value.source
: rule.value
: rule;
var hasValidation = (options) => options.mount &&
(options.required ||
options.min ||
options.max ||
options.maxLength ||
options.minLength ||
options.pattern ||
options.validate);
function schemaErrorLookup(errors, _fields, name) {
const error = get(errors, name);
if (error || isKey(name)) {
return {
error,
name,
};
}
const names = name.split('.');
while (names.length) {
const fieldName = names.join('.');
const field = get(_fields, fieldName);
const foundError = get(errors, fieldName);
if (field && !Array.isArray(field) && name !== fieldName) {
return { name };
}
if (foundError && foundError.type) {
return {
name: fieldName,
error: foundError,
};
}
names.pop();
}
return {
name,
};
}
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
if (mode.isOnAll) {
return false;
}
else if (!isSubmitted && mode.isOnTouch) {
return !(isTouched || isBlurEvent);
}
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
return !isBlurEvent;
}
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
return isBlurEvent;
}
return true;
};
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
var isMessage = (value) => isString(value) || React.isValidElement(value);
function getValidateError(result, ref, type = 'validate') {
if (isMessage(result) ||
(Array.isArray(result) && result.every(isMessage)) ||
(isBoolean(result) && !result)) {
return {
type,
message: isMessage(result) ? result : '',
ref,
};
}
}
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
? validationData
: {
value: validationData,
message: '',
};
var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUseNativeValidation) => {
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
if (!mount || disabled) {
return {};
}
const inputRef = refs ? refs[0] : ref;
const setCustomValidity = (message) => {
if (shouldUseNativeValidation && inputRef.reportValidity) {
inputRef.setCustomValidity(isBoolean(message) ? '' : message || ' ');
inputRef.reportValidity();
}
};
const error = {};
const isRadio = isRadioInput(ref);
const isCheckBox = isCheckBoxInput(ref);
const isRadioOrCheckbox = isRadio || isCheckBox;
const isEmpty = ((valueAsNumber || isFileInput(ref)) && !ref.value) ||
inputValue === '' ||
(Array.isArray(inputValue) && !inputValue.length);
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
const message = exceedMax ? maxLengthMessage : minLengthMessage;
error[name] = {
type: exceedMax ? maxType : minType,
message,
ref,
...appendErrorsCurry(exceedMax ? maxType : minType, message),
};
};
if (required &&
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
(isBoolean(inputValue) && !inputValue) ||
(isCheckBox && !getCheckboxValue(refs).isValid) ||
(isRadio && !getRadioValue(refs).isValid))) {
const { value, message } = isMessage(required)
? { value: !!required, message: required }
: getValueAndMessage(required);
if (value) {
error[name] = {
type: INPUT_VALIDATION_RULES.required,
message,
ref: inputRef,
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
};
if (!validateAllFieldCriteria) {
setCustomValidity(message);
return error;
}
}
}
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
let exceedMax;
let exceedMin;
const maxOutput = getValueAndMessage(max);
const minOutput = getValueAndMessage(min);
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
const valueNumber = ref.valueAsNumber || +inputValue;
if (!isNullOrUndefined(maxOutput.value)) {
exceedMax = valueNumber > maxOutput.value;
}
if (!isNullOrUndefined(minOutput.value)) {
exceedMin = valueNumber < minOutput.value;
}
}
else {
const valueDate = ref.valueAsDate || new Date(inputValue);
if (isString(maxOutput.value)) {
exceedMax = valueDate > new Date(maxOutput.value);
}
if (isString(minOutput.value)) {
exceedMin = valueDate < new Date(minOutput.value);
}
}
if (exceedMax || exceedMin) {
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
if (!validateAllFieldCriteria) {
setCustomValidity(error[name].message);
return error;
}
}
}
if ((maxLength || minLength) && !isEmpty && isString(inputValue)) {
const maxLengthOutput = getValueAndMessage(maxLength);
const minLengthOutput = getValueAndMessage(minLength);
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
inputValue.length > maxLengthOutput.value;
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
inputValue.length < minLengthOutput.value;
if (exceedMax || exceedMin) {
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
if (!validateAllFieldCriteria) {
setCustomValidity(error[name].message);
return error;
}
}
}
if (pattern && !isEmpty && isString(inputValue)) {
const { value: patternValue, message } = getValueAndMessage(pattern);
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
error[name] = {
type: INPUT_VALIDATION_RULES.pattern,
message,
ref,
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
};
if (!validateAllFieldCriteria) {
setCustomValidity(message);
return error;
}
}
}
if (validate) {
if (isFunction(validate)) {
const result = await validate(inputValue);
const validateError = getValidateError(result, inputRef);
if (validateError) {
error[name] = {
...validateError,
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
};
if (!validateAllFieldCriteria) {
setCustomValidity(validateError.message);
return error;
}
}
}
else if (isObject(validate)) {
let validationResult = {};
for (const key in validate) {
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
break;
}
const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
if (validateError) {
validationResult = {
...validateError,
...appendErrorsCurry(key, validateError.message),
};
setCustomValidity(validateError.message);
if (validateAllFieldCriteria) {
error[name] = validationResult;
}
}
}
if (!isEmptyObject(validationResult)) {
error[name] = {
ref: inputRef,
...validationResult,
};
if (!validateAllFieldCriteria) {
return error;
}
}
}
}
setCustomValidity(true);
return error;
};
const defaultOptions = {
mode: VALIDATION_MODE.onSubmit,
reValidateMode: VALIDATION_MODE.onChange,
shouldFocusError: true,
};
function createFormControl(props = {}) {
let _options = {
...defaultOptions,
...props,
};
let _formState = {
isDirty: false,
isValidating: false,
dirtyFields: {},
isSubmitted: false,
submitCount: 0,
touchedFields: {},
isSubmitting: false,
isSubmitSuccessful: false,
isValid: false,
errors: {},
};
let _fields = {};
let _defaultValues = cloneObject(_options.defaultValues) || {};
let _formValues = _options.shouldUnregister
? {}
: cloneObject(_defaultValues);
let _stateFlags = {
action: false,
mount: false,
watch: false,
};
let _names = {
mount: new Set(),
unMount: new Set(),
array: new Set(),
watch: new Set(),
};
let delayErrorCallback;
let timer = 0;
let validateFields = {};
const _proxyFormState = {
isDirty: false,
dirtyFields: false,
touchedFields: false,
isValidating: false,
isValid: false,
errors: false,
};
const _subjects = {
watch: createSubject(),
array: createSubject(),
state: createSubject(),
};
const validationModeBeforeSubmit = getValidationModes(_options.mode);
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
const debounce = (callback) => (wait) => {
clearTimeout(timer);
timer = window.setTimeout(callback, wait);
};
const _updateValid = async (shouldSkipRender) => {
let isValid = false;
if (_proxyFormState.isValid) {
isValid = _options.resolver
? isEmptyObject((await _executeSchema()).errors)
: await executeBuildInValidation(_fields, true);
if (!shouldSkipRender && isValid !== _formState.isValid) {
_formState.isValid = isValid;
_subjects.state.next({
isValid,
});
}
}
return isValid;
};
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
if (args && method) {
_stateFlags.action = true;
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
const fieldValues = method(get(_fields, name), args.argA, args.argB);
shouldSetValues && set(_fields, name, fieldValues);
}
if (_proxyFormState.errors &&
shouldUpdateFieldsAndState &&
Array.isArray(get(_formState.errors, name))) {
const errors = method(get(_formState.errors, name), args.argA, args.argB);
shouldSetValues && set(_formState.errors, name, errors);
unsetEmptyArray(_formState.errors, name);
}
if (_proxyFormState.touchedFields &&
shouldUpdateFieldsAndState &&
Array.isArray(get(_formState.touchedFields, name))) {
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
}
if (_proxyFormState.dirtyFields) {
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
}
_subjects.state.next({
isDirty: _getDirty(name, values),
dirtyFields: _formState.dirtyFields,
errors: _formState.errors,
isValid: _formState.isValid,
});
}
else {
set(_formValues, name, values);
}
};
const updateErrors = (name, error) => {
set(_formState.errors, name, error);
_subjects.state.next({
errors: _formState.errors,
});
};
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
const field = get(_fields, name);
if (field) {
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
isUndefined(defaultValue) ||
(ref && ref.defaultChecked) ||
shouldSkipSetValueAs
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
: setFieldValue(name, defaultValue);
_stateFlags.mount && _updateValid();
}
};
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
let isFieldDirty = false;
const output = {
name,
};
const isPreviousFieldTouched = get(_formState.touchedFields, name);
if (_proxyFormState.isDirty) {
const isPreviousFormDirty = _formState.isDirty;
_formState.isDirty = output.isDirty = _getDirty();
isFieldDirty = isPreviousFormDirty !== output.isDirty;
}
if (_proxyFormState.dirtyFields && (!isBlurEvent || shouldDirty)) {
const isPreviousFieldDirty = get(_formState.dirtyFields, name);
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
isCurrentFieldPristine
? unset(_formState.dirtyFields, name)
: set(_formState.dirtyFields, name, true);
output.dirtyFields = _formState.dirtyFields;
isFieldDirty =
isFieldDirty ||
isPreviousFieldDirty !== get(_formState.dirtyFields, name);
}
if (isBlurEvent && !isPreviousFieldTouched) {
set(_formState.touchedFields, name, isBlurEvent);
output.touchedFields = _formState.touchedFields;
isFieldDirty =
isFieldDirty ||
(_proxyFormState.touchedFields &&
isPreviousFieldTouched !== isBlurEvent);
}
isFieldDirty && shouldRender && _subjects.state.next(output);
return isFieldDirty ? output : {};
};
const shouldRenderByError = async (name, isValid, error, fieldState) => {
const previousFieldError = get(_formState.errors, name);
const shouldUpdateValid = _proxyFormState.isValid && _formState.isValid !== isValid;
if (props.delayError && error) {
delayErrorCallback = debounce(() => updateErrors(name, error));
delayErrorCallback(props.delayError);
}
else {
clearTimeout(timer);
delayErrorCallback = null;
error
? set(_formState.errors, name, error)
: unset(_formState.errors, name);
}
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
!isEmptyObject(fieldState) ||
shouldUpdateValid) {
const updatedFormState = {
...fieldState,
...(shouldUpdateValid ? { isValid } : {}),
errors: _formState.errors,
name,
};
_formState = {
..._formState,
...updatedFormState,
};
_subjects.state.next(updatedFormState);
}
validateFields[name]--;
if (_proxyFormState.isValidating &&
!Object.values(validateFields).some((v) => v)) {
_subjects.state.next({
isValidating: false,
});
validateFields = {};
}
};
const _executeSchema = async (name) => _options.resolver
? await _options.resolver({ ..._formValues }, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation))
: {};
const executeSchemaAndUpdateState = async (names) => {
const { errors } = await _executeSchema();
if (names) {
for (const name of names) {
const error = get(errors, name);
error
? set(_formState.errors, name, error)
: unset(_formState.errors, name);
}
}
else {
_formState.errors = errors;
}
return errors;
};
const executeBuildInValidation = async (fields, shouldOnlyCheckValid, context = {
valid: true,
}) => {
for (const name in fields) {
const field = fields[name];
if (field) {
const { _f: fieldReference, ...fieldValue } = field;
if (fieldReference) {
const fieldError = await validateField(field, get(_formValues, fieldReference.name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation);
if (fieldError[fieldReference.name]) {
context.valid = false;
if (shouldOnlyCheckValid) {
break;
}
}
if (!shouldOnlyCheckValid) {
fieldError[fieldReference.name]
? set(_formState.errors, fieldReference.name, fieldError[fieldReference.name])
: unset(_formState.errors, fieldReference.name);
}
}
fieldValue &&
(await executeBuildInValidation(fieldValue, shouldOnlyCheckValid, context));
}
}
return context.valid;
};
const _removeUnmounted = () => {
for (const name of _names.unMount) {
const field = get(_fields, name);
field &&
(field._f.refs
? field._f.refs.every((ref) => !live(ref))
: !live(field._f.ref)) &&
unregister(name);
}
_names.unMount = new Set();
};
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
!deepEqual(getValues(), _defaultValues));
const _getWatch = (names, defaultValue, isGlobal) => {
const fieldValues = {
...(_stateFlags.mount
? _formValues
: isUndefined(defaultValue)
? _defaultValues
: isString(names)
? { [names]: defaultValue }
: defaultValue),
};
return generateWatchOutput(names, _names, fieldValues, isGlobal);
};
const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
const setFieldValue = (name, value, options = {}) => {
const field = get(_fields, name);
let fieldValue = value;
if (field) {
const fieldReference = field._f;
if (fieldReference) {
!fieldReference.disabled &&
set(_formValues, name, getFieldValueAs(value, fieldReference));
fieldValue =
isWeb && isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
? ''
: value;
if (isMultipleSelect(fieldReference.ref)) {
[...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
}
else if (fieldReference.refs) {
if (isCheckBoxInput(fieldReference.ref)) {
fieldReference.refs.length > 1
? fieldReference.refs.forEach((checkboxRef) => !checkboxRef.disabled &&
(checkboxRef.checked = Array.isArray(fieldValue)
? !!fieldValue.find((data) => data === checkboxRef.value)
: fieldValue === checkboxRef.value))
: fieldReference.refs[0] &&
(fieldReference.refs[0].checked = !!fieldValue);
}
else {
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
}
}
else if (isFileInput(fieldReference.ref)) {
fieldReference.ref.value = '';
}
else {
fieldReference.ref.value = fieldValue;
if (!fieldReference.ref.type) {
_subjects.watch.next({
name,
});
}
}
}
}
(options.shouldDirty || options.shouldTouch) &&
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
options.shouldValidate && trigger(name);
};
const setValues = (name, value, options) => {
for (const fieldKey in value) {
const fieldValue = value[fieldKey];
const fieldName = `${name}.${fieldKey}`;
const field = get(_fields, fieldName);
(_names.array.has(name) ||
!isPrimitive(fieldValue) ||
(field && !field._f)) &&
!isDateObject(fieldValue)
? setValues(fieldName, fieldValue, options)
: setFieldValue(fieldName, fieldValue, options);
}
};
const setValue = (name, value, options = {}) => {
const field = get(_fields, name);
const isFieldArray = _names.array.has(name);
const cloneValue = cloneObject(value);
set(_formValues, name, cloneValue);
if (isFieldArray) {
_subjects.array.next({
name,
values: _formValues,
});
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
options.shouldDirty) {
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
_subjects.state.next({
name,
dirtyFields: _formState.dirtyFields,
isDirty: _getDirty(name, cloneValue),
});
}
}
else {
field && !field._f && !isNullOrUndefined(cloneValue)
? setValues(name, cloneValue, options)
: setFieldValue(name, cloneValue, options);
}
isWatched(name, _names) && _subjects.state.next({});
_subjects.watch.next({
name,
});
};
const onChange = async (event) => {
const target = event.target;
let name = target.name;
const field = get(_fields, name);
if (field) {
let error;
let isValid;
const fieldValue = target.type
? getFieldValue(field._f)
: getEventValue(event);
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
const shouldSkipValidation = (!hasValidation(field._f) &&
!_options.resolver &&
!get(_formState.errors, name) &&
!field._f.deps) ||
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
const watched = isWatched(name, _names, isBlurEvent);
set(_formValues, name, fieldValue);
if (isBlurEvent) {
field._f.onBlur && field._f.onBlur(event);
delayErrorCallback && delayErrorCallback(0);
}
else if (field._f.onChange) {
field._f.onChange(event);
}
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
const shouldRender = !isEmptyObject(fieldState) || watched;
!isBlurEvent &&
_subjects.watch.next({
name,
type: event.type,
});
if (shouldSkipValidation) {
return (shouldRender &&
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
}
!isBlurEvent && watched && _subjects.state.next({});
validateFields[name] = validateFields[name] ? +1 : 1;
_subjects.state.next({
isValidating: true,
});
if (_options.resolver) {
const { errors } = await _executeSchema([name]);
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
error = errorLookupResult.error;
name = errorLookupResult.name;
isValid = isEmptyObject(errors);
}
else {
error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
isValid = await _updateValid(true);
}
field._f.deps &&
trigger(field._f.deps);
shouldRenderByError(name, isValid, error, fieldState);
}
};
const trigger = async (name, options = {}) => {
let isValid;
let validationResult;
const fieldNames = convertToArrayPayload(name);
_subjects.state.next({
isValidating: true,
});
if (_options.resolver) {
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
isValid = isEmptyObject(errors);
validationResult = name
? !fieldNames.some((name) => get(errors, name))
: isValid;
}
else if (name) {
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
const field = get(_fields, fieldName);
return await executeBuildInValidation(field && field._f ? { [fieldName]: field } : field);
}))).every(Boolean);
!(!validationResult && !_formState.isValid) && _updateValid();
}
else {
validationResult = isValid = await executeBuildInValidation(_fields);
}
_subjects.state.next({
...(!isString(name) ||
(_proxyFormState.isValid && isValid !== _formState.isValid)
? {}
: { name }),
...(_options.resolver ? { isValid } : {}),
errors: _formState.errors,
isValidating: false,
});
options.shouldFocus &&
!validationResult &&
focusFieldBy(_fields, (key) => get(_formState.errors, key), name ? fieldNames : _names.mount);
return validationResult;
};
const getValues = (fieldNames) => {
const values = {
..._defaultValues,
...(_stateFlags.mount ? _formValues : {}),
};
return isUndefined(fieldNames)
? values
: isString(fieldNames)
? get(values, fieldNames)
: fieldNames.map((name) => get(values, name));
};
const getFieldState = (name, formState) => ({
invalid: !!get((formState || _formState).errors, name),
isDirty: !!get((formState || _formState).dirtyFields, name),
isTouched: !!get((formState || _formState).touchedFields, name),
error: get((formState || _formState).errors, name),
});
const clearErrors = (name) => {
name
? convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName))
: (_formState.errors = {});
_subjects.state.next({
errors: _formState.errors,
});
};
const setError = (name, error, options) => {
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
set(_formState.errors, name, {
...error,
ref,
});
_subjects.state.next({
name,
errors: _formState.errors,
isValid: false,
});
options && options.shouldFocus && ref && ref.focus && ref.focus();
};
const watch = (name, defaultValue) => isFunction(name)
? _subjects.watch.subscribe({
next: (info) => name(_getWatch(undefined, defaultValue), info),
})
: _getWatch(name, defaultValue, true);
const unregister = (name, options = {}) => {
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
_names.mount.delete(fieldName);
_names.array.delete(fieldName);
if (get(_fields, fieldName)) {
if (!options.keepValue) {
unset(_fields, fieldName);
unset(_formValues, fieldName);
}
!options.keepError && unset(_formState.errors, fieldName);
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
!options.keepTouched && unset(_formState.touchedFields, fieldName);
!_options.shouldUnregister &&
!options.keepDefaultValue &&
unset(_defaultValues, fieldName);
}
}
_subjects.watch.next({});
_subjects.state.next({
..._formState,
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
});
!options.keepIsValid && _updateValid();
};
const register = (name, options = {}) => {
let field = get(_fields, name);
const disabledIsDefined = isBoolean(options.disabled);
set(_fields, name, {
_f: {
...(field && field._f ? field._f : { ref: { name } }),
name,
mount: true,
...options,
},
});
_names.mount.add(name);
field
? disabledIsDefined &&
set(_formValues, name, options.disabled
? undefined
: get(_formValues, name, getFieldValue(field._f)))
: updateValidAndValue(name, true, options.value);
return {
...(disabledIsDefined ? { disabled: options.disabled } : {}),
...(_options.shouldUseNativeValidation
? {
required: !!options.required,
min: getRuleValue(options.min),
max: getRuleValue(options.max),
minLength: getRuleValue(options.minLength),
maxLength: getRuleValue(options.maxLength),
pattern: getRuleValue(options.pattern),
}
: {}),
name,
onChange,
onBlur: onChange,
ref: (ref) => {
if (ref) {
register(name, options);
field = get(_fields, name);
const fieldRef = isUndefined(ref.value)
? ref.querySelectorAll
? ref.querySelectorAll('input,select,textarea')[0] || ref
: ref
: ref;
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
const refs = field._f.refs || [];
if (radioOrCheckbox
? refs.find((option) => option === fieldRef)
: fieldRef === field._f.ref) {
return;
}
set(_fields, name, {
_f: {
...field._f,
...(radioOrCheckbox
? {
refs: [
...refs.filter(live),
fieldRef,
...(!!Array.isArray(get(_defaultValues, name))
? [{}]
: []),
],
ref: { type: fieldRef.type, name },
}
: { ref: fieldRef }),
},
});
updateValidAndValue(name, false, undefined, fieldRef);
}
else {
field = get(_fields, name, {});
if (field._f) {
field._f.mount = false;
}
(_options.shouldUnregister || options.shouldUnregister) &&
!(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
_names.unMount.add(name);
}
},
};
};
const handleSubmit = (onValid, onInvalid) => async (e) => {
if (e) {
e.preventDefault && e.preventDefault();
e.persist && e.persist();
}
let hasNoPromiseError = true;
let fieldValues = cloneObject(_formValues);
_subjects.state.next({
isSubmitting: true,
});
try {
if (_options.resolver) {
const { errors, values } = await _executeSchema();
_formState.errors = errors;
fieldValues = values;
}
else {
await executeBuildInValidation(_fields);
}
if (isEmptyObject(_formState.errors)) {
_subjects.state.next({
errors: {},
isSubmitting: true,
});
await onValid(fieldValues, e);
}
else {
if (onInvalid) {
await onInvalid({ ..._formState.errors }, e);
}
_options.shouldFocusError &&
focusFieldBy(_fields, (key) => get(_formState.errors, key), _names.mount);
}
}
catch (err) {
hasNoPromiseError = false;
throw err;
}
finally {
_formState.isSubmitted = true;
_subjects.state.next({
isSubmitted: true,
isSubmitting: false,
isSubmitSuccessful: isEmptyObject(_formState.errors) && hasNoPromiseError,
submitCount: _formState.submitCount + 1,
errors: _formState.errors,
});
}
};
const resetField = (name, options = {}) => {
if (get(_fields, name)) {
if (isUndefined(options.defaultValue)) {
setValue(name, get(_defaultValues, name));
}
else {
setValue(name, options.defaultValue);
set(_defaultValues, name, options.defaultValue);
}
if (!options.keepTouched) {
unset(_formState.touchedFields, name);
}
if (!options.keepDirty) {
unset(_formState.dirtyFields, name);
_formState.isDirty = options.defaultValue
? _getDirty(name, get(_defaultValues, name))
: _getDirty();
}
if (!options.keepError) {
unset(_formState.errors, name);
_proxyFormState.isValid && _updateValid();
}
_subjects.state.next({ ..._formState });
}
};
const reset = (formValues, keepStateOptions = {}) => {
const updatedValues = formValues || _defaultValues;
const cloneUpdatedValues = cloneObject(updatedValues);
const values = formValues && !isEmptyObject(formValues)
? cloneUpdatedValues
: _defaultValues;
if (!keepStateOptions.keepDefaultValues) {
_defaultValues = updatedValues;
}
if (!keepStateOptions.keepValues) {
if (keepStateOptions.keepDirtyValues) {
for (const fieldName of _names.mount) {
get(_formState.dirtyFields, fieldName)
? set(values, fieldName, get(_formValues, fieldName))
: setValue(fieldName, get(values, fieldName));
}
}
else {
if (isWeb && isUndefined(formValues)) {
for (const name of _names.mount) {
const field = get(_fields, name);
if (field && field._f) {
const fieldReference = Array.isArray(field._f.refs)
? field._f.refs[0]
: field._f.ref;
try {
isHTMLElement(fieldReference) &&
fieldReference.closest('form').reset();
break;
}
catch (_a) { }
}
}
}
_fields = {};
}
_formValues = props.shouldUnregister
? keepStateOptions.keepDefaultValues
? cloneObject(_defaultValues)
: {}
: cloneUpdatedValues;
_subjects.array.next({
values,
});
_subjects.watch.next({
values,
});
}
_names = {
mount: new Set(),
unMount: new Set(),
array: new Set(),
watch: new Set(),
watchAll: false,
focus: '',
};
_stateFlags.mount =
!_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
_stateFlags.watch = !!props.shouldUnregister;
_subjects.state.next({
submitCount: keepStateOptions.keepSubmitCount
? _formState.submitCount
: 0,
isDirty: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
? _formState.isDirty
: !!(keepStateOptions.keepDefaultValues &&
!deepEqual(formValues, _defaultValues)),
isSubmitted: keepStateOptions.keepIsSubmitted
? _formState.isSubmitted
: false,
dirtyFields: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
? _formState.dirtyFields
: keepStateOptions.keepDefaultValues && formValues
? getDirtyFields(_defaultValues, formValues)
: {},
touchedFields: keepStateOptions.keepTouched
? _formState.touchedFields
: {},
errors: keepStateOptions.keepErrors
? _formState.errors
: {},
isSubmitting: false,
isSubmitSuccessful: false,
});
};
const setFocus = (name, options = {}) => {
const field = get(_fields, name)._f;
const fieldRef = field.refs ? field.refs[0] : field.ref;
fieldRef.focus();
options.shouldSelect && fieldRef.select();
};
return {
control: {
register,
unregister,
getFieldState,
_executeSchema,
_getWatch,
_getDirty,
_updateValid,
_removeUnmounted,
_updateFieldArray,
_getFieldArray,
_subjects,
_proxyFormState,
get _fields() {
return _fields;
},
get _formValues() {
return _formValues;
},
get _stateFlags() {
return _stateFlags;
},
set _stateFlags(value) {
_stateFlags = value;
},
get _defaultValues() {
return _defaultValues;
},
get _names() {
return _names;
},
set _names(value) {
_names = value;
},
get _formState() {
return _formState;
},
set _formState(value) {
_formState = value;
},
get _options() {
return _options;
},
set _options(value) {
_options = {
..._options,
...value,
};
},
},
trigger,
register,
handleSubmit,
watch,
setValue,
getValues,
reset,
resetField,
clearErrors,
unregister,
setError,
setFocus,
getFieldState,
};
}
/**
* Custom hook to mange the entire form.
*
* @remarks
* [API](https://react-hook-form.com/api/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
*
* @param props - form configuration and validation parameters.
*
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
*
* @example
* ```tsx
* function App() {
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
* const onSubmit = data => console.log(data);
*
* console.log(watch("example"));
*
* return (
*
* );
* }
* ```
*/
function useForm(props = {}) {
const _formControl = React.useRef();
const [formState, updateFormState] = React.useState({
isDirty: false,
isValidating: false,
dirtyFields: {},
isSubmitted: false,
submitCount: 0,
touchedFields: {},
isSubmitting: false,
isSubmitSuccessful: false,
isValid: false,
errors: {},
});
if (_formControl.current) {
_formControl.current.control._options = props;
}
else {
_formControl.current = {
...createFormControl(props),
formState,
};
}
const control = _formControl.current.control;
const callback = React.useCallback((value) => {
if (shouldRenderFormState(value, control._proxyFormState, true)) {
control._formState = {
...control._formState,
...value,
};
updateFormState({ ...control._formState });
}
}, [control]);
useSubscribe({
subject: control._subjects.state,
callback,
});
React.useEffect(() => {
if (!control._stateFlags.mount) {
control._proxyFormState.isValid && control._updateValid();
control._stateFlags.mount = true;
}
if (control._stateFlags.watch) {
control._stateFlags.watch = false;
control._subjects.state.next({});
}
control._removeUnmounted();
});
_formControl.current.formState = getProxyFormState(formState, control._proxyFormState);
return _formControl.current;
}
export { Controller, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
//# sourceMappingURL=index.esm.mjs.map