1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560 |
- 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 isPlainObject = (tempObject) => {
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
- return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
- };
- 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 ? [] : {};
- if (!isArray && !isPlainObject(data)) {
- copy = data;
- }
- else {
- for (const key in data) {
- if (data.hasOwnProperty(key)) {
- copy[key] = cloneObject(data[key]);
- }
- }
- }
- }
- else {
- return data;
- }
- return copy;
- }
- var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
- var isUndefined = (val) => val === undefined;
- var get = (object, path, defaultValue) => {
- if (!path || !isObject(object)) {
- return defaultValue;
- }
- const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
- return isUndefined(result) || result === object
- ? isUndefined(object[path])
- ? defaultValue
- : object[path]
- : result;
- };
- var isBoolean = (value) => typeof value === 'boolean';
- 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/docs/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 (
- * <FormProvider {...methods} >
- * <form onSubmit={methods.handleSubmit(onSubmit)}>
- * <NestedInput />
- * <input type="submit" />
- * </form>
- * </FormProvider>
- * );
- * }
- *
- * function NestedInput() {
- * const { register } = useFormContext(); // retrieve all hook methods
- * return <input {...register("test")} />;
- * }
- * ```
- */
- 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/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
- *
- * @param props - all useForm methods
- *
- * @example
- * ```tsx
- * function App() {
- * const methods = useForm();
- * const onSubmit = data => console.log(data);
- *
- * return (
- * <FormProvider {...methods} >
- * <form onSubmit={methods.handleSubmit(onSubmit)}>
- * <NestedInput />
- * <input type="submit" />
- * </form>
- * </FormProvider>
- * );
- * }
- *
- * function NestedInput() {
- * const { register } = useFormContext(); // retrieve all hook methods
- * return <input {...register("test")} />;
- * }
- * ```
- */
- const FormProvider = (props) => {
- const { children, ...data } = props;
- return (React.createElement(HookFormContext.Provider, { value: data }, children));
- };
- var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
- const result = {
- defaultValues: control._defaultValues,
- };
- for (const key in formState) {
- Object.defineProperty(result, key, {
- get: () => {
- const _key = key;
- if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
- control._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, updateFormState, isRoot) => {
- updateFormState(formStateData);
- 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) => !name ||
- !signalName ||
- name === signalName ||
- convertToArrayPayload(name).some((currentName) => currentName &&
- (exact
- ? currentName === signalName
- : currentName.startsWith(signalName) ||
- signalName.startsWith(currentName)));
- function useSubscribe(props) {
- const _props = React.useRef(props);
- _props.current = props;
- React.useEffect(() => {
- const subscription = !props.disabled &&
- _props.current.subject &&
- _props.current.subject.subscribe({
- next: _props.current.next,
- });
- return () => {
- subscription && subscription.unsubscribe();
- };
- }, [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/docs/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 (
- * <form onSubmit={handleSubmit(onSubmit)}>
- * <input {...register("firstName")} placeholder="First Name" />
- * {dirtyFields.firstName && <p>Field is dirty.</p>}
- * <input type="submit" />
- * </form>
- * );
- * }
- * ```
- */
- function useFormState(props) {
- const methods = useFormContext();
- const { control = methods.control, disabled, name, exact } = props || {};
- const [formState, updateFormState] = React.useState(control._formState);
- const _mounted = React.useRef(true);
- const _localProxyFormState = React.useRef({
- isDirty: false,
- isLoading: false,
- dirtyFields: false,
- touchedFields: false,
- isValidating: false,
- isValid: false,
- errors: false,
- });
- const _name = React.useRef(name);
- _name.current = name;
- useSubscribe({
- disabled,
- next: (value) => _mounted.current &&
- shouldSubscribeByName(_name.current, value.name, exact) &&
- shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
- updateFormState({
- ...control._formState,
- ...value,
- }),
- subject: control._subjects.state,
- });
- React.useEffect(() => {
- _mounted.current = true;
- _localProxyFormState.current.isValid && control._updateValid(true);
- return () => {
- _mounted.current = false;
- };
- }, [control]);
- return getProxyFormState(formState, control, _localProxyFormState.current, false);
- }
- var isString = (value) => typeof value === 'string';
- var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
- if (isString(names)) {
- isGlobal && _names.watch.add(names);
- return get(formValues, names, defaultValue);
- }
- if (Array.isArray(names)) {
- return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
- }
- isGlobal && (_names.watchAll = true);
- return formValues;
- };
- /**
- * Custom hook to subscribe to field change and isolate re-rendering at the component level.
- *
- * @remarks
- *
- * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
- *
- * @example
- * ```tsx
- * const { control } = 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;
- useSubscribe({
- disabled,
- subject: control._subjects.values,
- next: (formState) => {
- if (shouldSubscribeByName(_name.current, formState.name, exact)) {
- updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
- }
- },
- });
- const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
- React.useEffect(() => control._removeUnmounted());
- return value;
- }
- var isKey = (value) => /^\w*$/.test(value);
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
- var 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;
- };
- /**
- * 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/docs/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 (
- * <div>
- * <input {...field} placeholder={props.name} />
- * <p>{fieldState.isTouched && "Touched"}</p>
- * <p>{formState.isSubmitted ? "submitted" : ""}</p>
- * </div>
- * );
- * }
- * ```
- */
- function useController(props) {
- const methods = useFormContext();
- const { name, disabled, 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,
- ...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
- }));
- React.useEffect(() => {
- const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
- const updateMounted = (name, value) => {
- const field = get(control._fields, name);
- if (field) {
- field._f.mount = value;
- }
- };
- updateMounted(name, true);
- if (_shouldUnregisterField) {
- const value = cloneObject(get(control._options.defaultValues, name));
- set(control._defaultValues, name, value);
- if (isUndefined(get(control._formValues, name))) {
- set(control._formValues, name, value);
- }
- }
- return () => {
- (isArrayField
- ? _shouldUnregisterField && !control._state.action
- : _shouldUnregisterField)
- ? control.unregister(name)
- : updateMounted(name, false);
- };
- }, [name, control, isArrayField, shouldUnregister]);
- React.useEffect(() => {
- if (get(control._fields, name)) {
- control._updateDisabledField({
- disabled,
- fields: control._fields,
- name,
- value: get(control._fields, name)._f.value,
- });
- }
- }, [disabled, name, control]);
- return {
- field: {
- name,
- value,
- ...(isBoolean(disabled) || formState.disabled
- ? { disabled: formState.disabled || disabled }
- : {}),
- 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: (elm) => {
- const field = get(control._fields, name);
- if (field && elm) {
- field._f.ref = {
- focus: () => elm.focus(),
- select: () => elm.select(),
- setCustomValidity: (message) => elm.setCustomValidity(message),
- reportValidity: () => elm.reportValidity(),
- };
- }
- },
- },
- formState,
- fieldState: Object.defineProperties({}, {
- invalid: {
- enumerable: true,
- get: () => !!get(formState.errors, name),
- },
- isDirty: {
- enumerable: true,
- get: () => !!get(formState.dirtyFields, name),
- },
- isTouched: {
- enumerable: true,
- get: () => !!get(formState.touchedFields, name),
- },
- error: {
- enumerable: true,
- get: () => get(formState.errors, name),
- },
- }),
- };
- }
- /**
- * Component based on `useController` hook to work with controlled component.
- *
- * @remarks
- * [API](https://react-hook-form.com/docs/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<FormValues>({
- * defaultValues: {
- * test: ""
- * }
- * });
- *
- * return (
- * <form>
- * <Controller
- * control={control}
- * name="test"
- * render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
- * <>
- * <input
- * onChange={onChange} // send value to hook form
- * onBlur={onBlur} // notify when input is touched
- * value={value} // return updated value
- * ref={ref} // set ref for focus management
- * />
- * <p>{formState.isSubmitted ? "submitted" : ""}</p>
- * <p>{fieldState.isTouched ? "touched" : ""}</p>
- * </>
- * )}
- * />
- * </form>
- * );
- * }
- * ```
- */
- const Controller = (props) => props.render(useController(props));
- const POST_REQUEST = 'post';
- /**
- * Form component to manage submission.
- *
- * @param props - to setup submission detail. {@link FormProps}
- *
- * @returns form component or headless render prop.
- *
- * @example
- * ```tsx
- * function App() {
- * const { control, formState: { errors } } = useForm();
- *
- * return (
- * <Form action="/api" control={control}>
- * <input {...register("name")} />
- * <p>{errors?.root?.server && 'Server error'}</p>
- * <button>Submit</button>
- * </Form>
- * );
- * }
- * ```
- */
- function Form(props) {
- const methods = useFormContext();
- const [mounted, setMounted] = React.useState(false);
- const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, ...rest } = props;
- const submit = async (event) => {
- let hasError = false;
- let type = '';
- await control.handleSubmit(async (data) => {
- const formData = new FormData();
- let formDataJson = '';
- try {
- formDataJson = JSON.stringify(data);
- }
- catch (_a) { }
- for (const name of control._names.mount) {
- formData.append(name, get(data, name));
- }
- if (onSubmit) {
- await onSubmit({
- data,
- event,
- method,
- formData,
- formDataJson,
- });
- }
- if (action) {
- try {
- const shouldStringifySubmissionData = [
- headers && headers['Content-Type'],
- encType,
- ].some((value) => value && value.includes('json'));
- const response = await fetch(action, {
- method,
- headers: {
- ...headers,
- ...(encType ? { 'Content-Type': encType } : {}),
- },
- body: shouldStringifySubmissionData ? formDataJson : formData,
- });
- if (response &&
- (validateStatus
- ? !validateStatus(response.status)
- : response.status < 200 || response.status >= 300)) {
- hasError = true;
- onError && onError({ response });
- type = String(response.status);
- }
- else {
- onSuccess && onSuccess({ response });
- }
- }
- catch (error) {
- hasError = true;
- onError && onError({ error });
- }
- }
- })(event);
- if (hasError && props.control) {
- props.control._subjects.state.next({
- isSubmitSuccessful: false,
- });
- props.control.setError('root.server', {
- type,
- });
- }
- };
- React.useEffect(() => {
- setMounted(true);
- }, []);
- return render ? (React.createElement(React.Fragment, null, render({
- submit,
- }))) : (React.createElement("form", { noValidate: mounted, action: action, method: method, encType: encType, onSubmit: submit, ...rest }, children));
- }
- var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
- ? {
- ...errors[name],
- types: {
- ...(errors[name] && errors[name].types ? errors[name].types : {}),
- [type]: message || true,
- },
- }
- : {};
- 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 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 isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
- (_names.watchAll ||
- _names.watch.has(name) ||
- [..._names.watch].some((watchName) => name.startsWith(watchName) &&
- /^\.\w+/.test(name.slice(watchName.length))));
- const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
- for (const key of fieldsNames || Object.keys(fields)) {
- const field = get(fields, key);
- if (field) {
- const { _f, ...currentField } = field;
- if (_f) {
- if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
- break;
- }
- else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
- break;
- }
- else {
- iterateFieldsByAction(currentField, action);
- }
- }
- else if (isObject(currentField)) {
- iterateFieldsByAction(currentField, action);
- }
- }
- }
- };
- var updateFieldArrayRootError = (errors, error, name) => {
- const fieldArrayErrors = compact(get(errors, name));
- set(fieldArrayErrors, 'root', error[name]);
- set(errors, name, fieldArrayErrors);
- return errors;
- };
- var isFileInput = (element) => element.type === 'file';
- var isFunction = (value) => typeof value === 'function';
- var isHTMLElement = (value) => {
- if (!isWeb) {
- return false;
- }
- const owner = value ? value.ownerDocument : 0;
- return (value instanceof
- (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
- };
- var isMessage = (value) => isString(value);
- var isRadioInput = (element) => element.type === 'radio';
- var isRegex = (value) => value instanceof RegExp;
- 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;
- };
- 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 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, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
- const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
- const inputValue = get(formValues, name);
- 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)) &&
- isUndefined(ref.value) &&
- isUndefined(inputValue)) ||
- (isHTMLElement(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 (isFieldArray
- ? !Array.isArray(inputValue) || !inputValue.length
- : 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 ? +inputValue : 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);
- const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
- const isTime = ref.type == 'time';
- const isWeek = ref.type == 'week';
- if (isString(maxOutput.value) && inputValue) {
- exceedMax = isTime
- ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
- : isWeek
- ? inputValue > maxOutput.value
- : valueDate > new Date(maxOutput.value);
- }
- if (isString(minOutput.value) && inputValue) {
- exceedMin = isTime
- ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
- : isWeek
- ? inputValue < minOutput.value
- : 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) || (isFieldArray && Array.isArray(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, formValues);
- 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, formValues), 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;
- };
- var appendAt = (data, value) => [
- ...data,
- ...convertToArrayPayload(value),
- ];
- 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;
- };
- var prependAt = (data, value) => [
- ...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]];
- };
- 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 isEmptyArray(obj) {
- for (const key in obj) {
- if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
- return false;
- }
- }
- return true;
- }
- function unset(object, path) {
- const paths = Array.isArray(path)
- ? path
- : isKey(path)
- ? [path]
- : stringToPath(path);
- const childObject = paths.length === 1 ? object : baseGet(object, paths);
- const index = paths.length - 1;
- const key = paths[index];
- if (childObject) {
- delete childObject[key];
- }
- if (index !== 0 &&
- ((isObject(childObject) && isEmptyObject(childObject)) ||
- (Array.isArray(childObject) && isEmptyArray(childObject)))) {
- unset(object, paths.slice(0, -1));
- }
- 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. • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn) • [Video](https://youtu.be/4MrbfGSFY2A)
- *
- * @remarks
- * [API](https://react-hook-form.com/docs/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 (
- * <form onSubmit={handleSubmit(data => console.log(data))}>
- * {fields.map((item, index) => (
- * <input key={item.id} {...register(`test.${index}.firstName`)} />
- * ))}
- * <button type="button" onClick={() => append({ firstName: "bill" })}>
- * append
- * </button>
- * <input type="submit" />
- * </form>
- * );
- * }
- * ```
- */
- 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);
- props.rules &&
- control.register(name, props.rules);
- useSubscribe({
- next: ({ values, name: fieldArrayName, }) => {
- if (fieldArrayName === _name.current || !fieldArrayName) {
- const fieldValues = get(values, _name.current);
- if (Array.isArray(fieldValues)) {
- setFields(fieldValues);
- ids.current = fieldValues.map(generateId);
- }
- }
- },
- subject: control._subjects.array,
- });
- const updateValues = React.useCallback((updatedFieldArrayValues) => {
- _actioned.current = true;
- control._updateFieldArray(name, updatedFieldArrayValues);
- }, [control, name]);
- const append = (value, options) => {
- const appendValue = convertToArrayPayload(cloneObject(value));
- const updatedFieldArrayValues = appendAt(control._getFieldArray(name), appendValue);
- control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
- ids.current = appendAt(ids.current, appendValue.map(generateId));
- updateValues(updatedFieldArrayValues);
- setFields(updatedFieldArrayValues);
- control._updateFieldArray(name, updatedFieldArrayValues, appendAt, {
- argA: fillEmptyArray(value),
- });
- };
- const prepend = (value, options) => {
- const prependValue = convertToArrayPayload(cloneObject(value));
- const updatedFieldArrayValues = prependAt(control._getFieldArray(name), prependValue);
- control._names.focus = getFocusFieldName(name, 0, options);
- ids.current = prependAt(ids.current, prependValue.map(generateId));
- updateValues(updatedFieldArrayValues);
- setFields(updatedFieldArrayValues);
- control._updateFieldArray(name, updatedFieldArrayValues, prependAt, {
- 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._state.action = false;
- isWatched(name, control._names) &&
- control._subjects.state.next({
- ...control._formState,
- });
- if (_actioned.current &&
- (!getValidationModes(control._options.mode).isOnSubmit ||
- control._formState.isSubmitted)) {
- if (control._options.resolver) {
- control._executeSchema([name]).then((result) => {
- const error = get(result.errors, name);
- const existingError = get(control._formState.errors, name);
- if (existingError
- ? (!error && existingError.type) ||
- (error &&
- (existingError.type !== error.type ||
- existingError.message !== error.message))
- : error && error.type) {
- error
- ? set(control._formState.errors, name, error)
- : unset(control._formState.errors, name);
- control._subjects.state.next({
- errors: control._formState.errors,
- });
- }
- });
- }
- else {
- const field = get(control._fields, name);
- if (field &&
- field._f &&
- !(getValidationModes(control._options.reValidateMode).isOnSubmit &&
- getValidationModes(control._options.mode).isOnSubmit)) {
- validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
- control._subjects.state.next({
- errors: updateFieldArrayRootError(control._formState.errors, error, name),
- }));
- }
- }
- }
- control._subjects.values.next({
- name,
- values: { ...control._formValues },
- });
- control._names.focus &&
- iterateFieldsByAction(control._fields, (ref, key) => {
- if (control._names.focus &&
- key.startsWith(control._names.focus) &&
- ref.focus) {
- ref.focus();
- return 1;
- }
- return;
- });
- control._names.focus = '';
- control._updateValid();
- _actioned.current = false;
- }, [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, [updateValues, name, control]),
- append: React.useCallback(append, [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]),
- };
- }
- var createSubject = () => {
- let _observers = [];
- const next = (value) => {
- for (const observer of _observers) {
- observer.next && 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 isMultipleSelect = (element) => element.type === `select-multiple`;
- var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
- var live = (ref) => isHTMLElement(ref) && ref.isConnected;
- var objectHasFunction = (data) => {
- for (const key in data) {
- if (isFunction(data[key])) {
- return true;
- }
- }
- return false;
- };
- 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));
- var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
- ? value
- : valueAsNumber
- ? value === ''
- ? NaN
- : value
- ? +value
- : value
- : valueAsDate && isString(value)
- ? new Date(value)
- : setValueAs
- ? setValueAs(value)
- : value;
- 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 getRuleValue = (rule) => isUndefined(rule)
- ? rule
- : 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);
- const defaultOptions = {
- mode: VALIDATION_MODE.onSubmit,
- reValidateMode: VALIDATION_MODE.onChange,
- shouldFocusError: true,
- };
- function createFormControl(props = {}, flushRootRender) {
- let _options = {
- ...defaultOptions,
- ...props,
- };
- let _formState = {
- submitCount: 0,
- isDirty: false,
- isLoading: isFunction(_options.defaultValues),
- isValidating: false,
- isSubmitted: false,
- isSubmitting: false,
- isSubmitSuccessful: false,
- isValid: false,
- touchedFields: {},
- dirtyFields: {},
- errors: _options.errors || {},
- disabled: _options.disabled || false,
- };
- let _fields = {};
- let _defaultValues = isObject(_options.values) || isObject(_options.defaultValues)
- ? cloneObject(_options.values || _options.defaultValues) || {}
- : {};
- let _formValues = _options.shouldUnregister
- ? {}
- : cloneObject(_defaultValues);
- let _state = {
- 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;
- const _proxyFormState = {
- isDirty: false,
- dirtyFields: false,
- touchedFields: false,
- isValidating: false,
- isValid: false,
- errors: false,
- };
- const _subjects = {
- values: 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 = setTimeout(callback, wait);
- };
- const _updateValid = async (shouldUpdateValid) => {
- if (_proxyFormState.isValid || shouldUpdateValid) {
- const isValid = _options.resolver
- ? isEmptyObject((await _executeSchema()).errors)
- : await executeBuiltInValidation(_fields, true);
- if (isValid !== _formState.isValid) {
- _subjects.state.next({
- isValid,
- });
- }
- }
- };
- const _updateIsValidating = (value) => _proxyFormState.isValidating &&
- _subjects.state.next({
- isValidating: value,
- });
- const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
- if (args && method) {
- _state.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 (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({
- name,
- 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 _setErrors = (errors) => {
- _formState.errors = errors;
- _subjects.state.next({
- errors: _formState.errors,
- isValid: false,
- });
- };
- 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);
- _state.mount && _updateValid();
- }
- };
- const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
- let shouldUpdateField = false;
- let isPreviousDirty = false;
- const output = {
- name,
- };
- const disabledField = !!(get(_fields, name) && get(_fields, name)._f.disabled);
- if (!isBlurEvent || shouldDirty) {
- if (_proxyFormState.isDirty) {
- isPreviousDirty = _formState.isDirty;
- _formState.isDirty = output.isDirty = _getDirty();
- shouldUpdateField = isPreviousDirty !== output.isDirty;
- }
- const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
- isPreviousDirty = !!(!disabledField && get(_formState.dirtyFields, name));
- isCurrentFieldPristine || disabledField
- ? unset(_formState.dirtyFields, name)
- : set(_formState.dirtyFields, name, true);
- output.dirtyFields = _formState.dirtyFields;
- shouldUpdateField =
- shouldUpdateField ||
- (_proxyFormState.dirtyFields &&
- isPreviousDirty !== !isCurrentFieldPristine);
- }
- if (isBlurEvent) {
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
- if (!isPreviousFieldTouched) {
- set(_formState.touchedFields, name, isBlurEvent);
- output.touchedFields = _formState.touchedFields;
- shouldUpdateField =
- shouldUpdateField ||
- (_proxyFormState.touchedFields &&
- isPreviousFieldTouched !== isBlurEvent);
- }
- }
- shouldUpdateField && shouldRender && _subjects.state.next(output);
- return shouldUpdateField ? output : {};
- };
- const shouldRenderByError = (name, isValid, error, fieldState) => {
- const previousFieldError = get(_formState.errors, name);
- const shouldUpdateValid = _proxyFormState.isValid &&
- isBoolean(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 && isBoolean(isValid) ? { isValid } : {}),
- errors: _formState.errors,
- name,
- };
- _formState = {
- ..._formState,
- ...updatedFormState,
- };
- _subjects.state.next(updatedFormState);
- }
- _updateIsValidating(false);
- };
- const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
- const executeSchemaAndUpdateState = async (names) => {
- const { errors } = await _executeSchema(names);
- 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 executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
- valid: true,
- }) => {
- for (const name in fields) {
- const field = fields[name];
- if (field) {
- const { _f, ...fieldValue } = field;
- if (_f) {
- const isFieldArrayRoot = _names.array.has(_f.name);
- const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
- if (fieldError[_f.name]) {
- context.valid = false;
- if (shouldOnlyCheckValid) {
- break;
- }
- }
- !shouldOnlyCheckValid &&
- (get(fieldError, _f.name)
- ? isFieldArrayRoot
- ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
- : set(_formState.errors, _f.name, fieldError[_f.name])
- : unset(_formState.errors, _f.name));
- }
- fieldValue &&
- (await executeBuiltInValidation(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) => generateWatchOutput(names, _names, {
- ...(_state.mount
- ? _formValues
- : isUndefined(defaultValue)
- ? _defaultValues
- : isString(names)
- ? { [names]: defaultValue }
- : defaultValue),
- }, isGlobal, defaultValue);
- const _getFieldArray = (name) => compact(get(_state.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 =
- isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
- ? ''
- : value;
- if (isMultipleSelect(fieldReference.ref)) {
- [...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
- }
- else if (fieldReference.refs) {
- if (isCheckBoxInput(fieldReference.ref)) {
- fieldReference.refs.length > 1
- ? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !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.values.next({
- name,
- values: { ..._formValues },
- });
- }
- }
- }
- }
- (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) {
- _subjects.state.next({
- name,
- dirtyFields: getDirtyFields(_defaultValues, _formValues),
- isDirty: _getDirty(name, cloneValue),
- });
- }
- }
- else {
- field && !field._f && !isNullOrUndefined(cloneValue)
- ? setValues(name, cloneValue, options)
- : setFieldValue(name, cloneValue, options);
- }
- isWatched(name, _names) && _subjects.state.next({ ..._formState });
- _subjects.values.next({
- name,
- values: { ..._formValues },
- });
- !_state.mount && flushRootRender();
- };
- const onChange = async (event) => {
- const target = event.target;
- let name = target.name;
- let isFieldValueUpdated = true;
- const field = get(_fields, name);
- const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
- const _updateIsFieldValueUpdated = (fieldValue) => {
- isFieldValueUpdated =
- Number.isNaN(fieldValue) ||
- fieldValue === get(_formValues, name, fieldValue);
- };
- if (field) {
- let error;
- let isValid;
- const fieldValue = getCurrentFieldValue();
- 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.values.next({
- name,
- type: event.type,
- values: { ..._formValues },
- });
- if (shouldSkipValidation) {
- _proxyFormState.isValid && _updateValid();
- return (shouldRender &&
- _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
- }
- !isBlurEvent && watched && _subjects.state.next({ ..._formState });
- _updateIsValidating(true);
- if (_options.resolver) {
- const { errors } = await _executeSchema([name]);
- _updateIsFieldValueUpdated(fieldValue);
- if (isFieldValueUpdated) {
- 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, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
- _updateIsFieldValueUpdated(fieldValue);
- if (isFieldValueUpdated) {
- if (error) {
- isValid = false;
- }
- else if (_proxyFormState.isValid) {
- isValid = await executeBuiltInValidation(_fields, true);
- }
- }
- }
- if (isFieldValueUpdated) {
- field._f.deps &&
- trigger(field._f.deps);
- shouldRenderByError(name, isValid, error, fieldState);
- }
- }
- };
- const _focusInput = (ref, key) => {
- if (get(_formState.errors, key) && ref.focus) {
- ref.focus();
- return 1;
- }
- return;
- };
- const trigger = async (name, options = {}) => {
- let isValid;
- let validationResult;
- const fieldNames = convertToArrayPayload(name);
- _updateIsValidating(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 executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
- }))).every(Boolean);
- !(!validationResult && !_formState.isValid) && _updateValid();
- }
- else {
- validationResult = isValid = await executeBuiltInValidation(_fields);
- }
- _subjects.state.next({
- ...(!isString(name) ||
- (_proxyFormState.isValid && isValid !== _formState.isValid)
- ? {}
- : { name }),
- ...(_options.resolver || !name ? { isValid } : {}),
- errors: _formState.errors,
- isValidating: false,
- });
- options.shouldFocus &&
- !validationResult &&
- iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
- return validationResult;
- };
- const getValues = (fieldNames) => {
- const values = {
- ..._defaultValues,
- ...(_state.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));
- _subjects.state.next({
- errors: name ? _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.values.subscribe({
- next: (payload) => name(_getWatch(undefined, defaultValue), payload),
- })
- : _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 (!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.values.next({
- values: { ..._formValues },
- });
- _subjects.state.next({
- ..._formState,
- ...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
- });
- !options.keepIsValid && _updateValid();
- };
- const _updateDisabledField = ({ disabled, name, field, fields, value, }) => {
- if (isBoolean(disabled)) {
- const inputValue = disabled
- ? undefined
- : isUndefined(value)
- ? getFieldValue(field ? field._f : get(fields, name)._f)
- : value;
- set(_formValues, name, inputValue);
- updateTouchAndDirty(name, inputValue, false, false, true);
- }
- };
- const register = (name, options = {}) => {
- let field = get(_fields, name);
- const disabledIsDefined = isBoolean(options.disabled);
- set(_fields, name, {
- ...(field || {}),
- _f: {
- ...(field && field._f ? field._f : { ref: { name } }),
- name,
- mount: true,
- ...options,
- },
- });
- _names.mount.add(name);
- if (field) {
- _updateDisabledField({
- field,
- disabled: options.disabled,
- name,
- value: options.value,
- });
- }
- else {
- updateValidAndValue(name, true, options.value);
- }
- return {
- ...(disabledIsDefined ? { disabled: options.disabled } : {}),
- ...(_options.progressive
- ? {
- 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) && _state.action) &&
- _names.unMount.add(name);
- }
- },
- };
- };
- const _focusError = () => _options.shouldFocusError &&
- iterateFieldsByAction(_fields, _focusInput, _names.mount);
- const _disableForm = (disabled) => {
- if (isBoolean(disabled)) {
- _subjects.state.next({ disabled });
- iterateFieldsByAction(_fields, (ref, name) => {
- let requiredDisabledState = disabled;
- const currentField = get(_fields, name);
- if (currentField && isBoolean(currentField._f.disabled)) {
- requiredDisabledState || (requiredDisabledState = currentField._f.disabled);
- }
- ref.disabled = requiredDisabledState;
- }, 0, false);
- }
- };
- const handleSubmit = (onValid, onInvalid) => async (e) => {
- let onValidError = undefined;
- if (e) {
- e.preventDefault && e.preventDefault();
- e.persist && e.persist();
- }
- let fieldValues = cloneObject(_formValues);
- _subjects.state.next({
- isSubmitting: true,
- });
- if (_options.resolver) {
- const { errors, values } = await _executeSchema();
- _formState.errors = errors;
- fieldValues = values;
- }
- else {
- await executeBuiltInValidation(_fields);
- }
- unset(_formState.errors, 'root');
- if (isEmptyObject(_formState.errors)) {
- _subjects.state.next({
- errors: {},
- });
- try {
- await onValid(fieldValues, e);
- }
- catch (error) {
- onValidError = error;
- }
- }
- else {
- if (onInvalid) {
- await onInvalid({ ..._formState.errors }, e);
- }
- _focusError();
- setTimeout(_focusError);
- }
- _subjects.state.next({
- isSubmitted: true,
- isSubmitting: false,
- isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
- submitCount: _formState.submitCount + 1,
- errors: _formState.errors,
- });
- if (onValidError) {
- throw onValidError;
- }
- };
- const resetField = (name, options = {}) => {
- if (get(_fields, name)) {
- if (isUndefined(options.defaultValue)) {
- setValue(name, cloneObject(get(_defaultValues, name)));
- }
- else {
- setValue(name, options.defaultValue);
- set(_defaultValues, name, cloneObject(options.defaultValue));
- }
- if (!options.keepTouched) {
- unset(_formState.touchedFields, name);
- }
- if (!options.keepDirty) {
- unset(_formState.dirtyFields, name);
- _formState.isDirty = options.defaultValue
- ? _getDirty(name, cloneObject(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 ? cloneObject(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;
- if (isHTMLElement(fieldReference)) {
- const form = fieldReference.closest('form');
- if (form) {
- form.reset();
- break;
- }
- }
- }
- }
- }
- _fields = {};
- }
- _formValues = props.shouldUnregister
- ? keepStateOptions.keepDefaultValues
- ? cloneObject(_defaultValues)
- : {}
- : cloneObject(values);
- _subjects.array.next({
- values: { ...values },
- });
- _subjects.values.next({
- values: { ...values },
- });
- }
- _names = {
- mount: new Set(),
- unMount: new Set(),
- array: new Set(),
- watch: new Set(),
- watchAll: false,
- focus: '',
- };
- !_state.mount && flushRootRender();
- _state.mount =
- !_proxyFormState.isValid ||
- !!keepStateOptions.keepIsValid ||
- !!keepStateOptions.keepDirtyValues;
- _state.watch = !!props.shouldUnregister;
- _subjects.state.next({
- submitCount: keepStateOptions.keepSubmitCount
- ? _formState.submitCount
- : 0,
- isDirty: keepStateOptions.keepDirty
- ? _formState.isDirty
- : !!(keepStateOptions.keepDefaultValues &&
- !deepEqual(formValues, _defaultValues)),
- isSubmitted: keepStateOptions.keepIsSubmitted
- ? _formState.isSubmitted
- : false,
- dirtyFields: keepStateOptions.keepDirtyValues
- ? keepStateOptions.keepDefaultValues && _formValues
- ? getDirtyFields(_defaultValues, _formValues)
- : _formState.dirtyFields
- : keepStateOptions.keepDefaultValues && formValues
- ? getDirtyFields(_defaultValues, formValues)
- : {},
- touchedFields: keepStateOptions.keepTouched
- ? _formState.touchedFields
- : {},
- errors: keepStateOptions.keepErrors ? _formState.errors : {},
- isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful
- ? _formState.isSubmitSuccessful
- : false,
- isSubmitting: false,
- });
- };
- const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
- ? formValues(_formValues)
- : formValues, keepStateOptions);
- const setFocus = (name, options = {}) => {
- const field = get(_fields, name);
- const fieldReference = field && field._f;
- if (fieldReference) {
- const fieldRef = fieldReference.refs
- ? fieldReference.refs[0]
- : fieldReference.ref;
- if (fieldRef.focus) {
- fieldRef.focus();
- options.shouldSelect && fieldRef.select();
- }
- }
- };
- const _updateFormState = (updatedFormState) => {
- _formState = {
- ..._formState,
- ...updatedFormState,
- };
- };
- const _resetDefaultValues = () => isFunction(_options.defaultValues) &&
- _options.defaultValues().then((values) => {
- reset(values, _options.resetOptions);
- _subjects.state.next({
- isLoading: false,
- });
- });
- return {
- control: {
- register,
- unregister,
- getFieldState,
- handleSubmit,
- setError,
- _executeSchema,
- _getWatch,
- _getDirty,
- _updateValid,
- _removeUnmounted,
- _updateFieldArray,
- _updateDisabledField,
- _getFieldArray,
- _reset,
- _resetDefaultValues,
- _updateFormState,
- _disableForm,
- _subjects,
- _proxyFormState,
- _setErrors,
- get _fields() {
- return _fields;
- },
- get _formValues() {
- return _formValues;
- },
- get _state() {
- return _state;
- },
- set _state(value) {
- _state = 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 manage the entire form.
- *
- * @remarks
- * [API](https://react-hook-form.com/docs/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 (
- * <form onSubmit={handleSubmit(onSubmit)}>
- * <input defaultValue="test" {...register("example")} />
- * <input {...register("exampleRequired", { required: true })} />
- * {errors.exampleRequired && <span>This field is required</span>}
- * <button>Submit</button>
- * </form>
- * );
- * }
- * ```
- */
- function useForm(props = {}) {
- const _formControl = React.useRef();
- const _values = React.useRef();
- const [formState, updateFormState] = React.useState({
- isDirty: false,
- isValidating: false,
- isLoading: isFunction(props.defaultValues),
- isSubmitted: false,
- isSubmitting: false,
- isSubmitSuccessful: false,
- isValid: false,
- submitCount: 0,
- dirtyFields: {},
- touchedFields: {},
- errors: props.errors || {},
- disabled: props.disabled || false,
- defaultValues: isFunction(props.defaultValues)
- ? undefined
- : props.defaultValues,
- });
- if (!_formControl.current) {
- _formControl.current = {
- ...createFormControl(props, () => updateFormState((formState) => ({ ...formState }))),
- formState,
- };
- }
- const control = _formControl.current.control;
- control._options = props;
- useSubscribe({
- subject: control._subjects.state,
- next: (value) => {
- if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
- updateFormState({ ...control._formState });
- }
- },
- });
- React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
- React.useEffect(() => {
- if (control._proxyFormState.isDirty) {
- const isDirty = control._getDirty();
- if (isDirty !== formState.isDirty) {
- control._subjects.state.next({
- isDirty,
- });
- }
- }
- }, [control, formState.isDirty]);
- React.useEffect(() => {
- if (props.values && !deepEqual(props.values, _values.current)) {
- control._reset(props.values, control._options.resetOptions);
- _values.current = props.values;
- updateFormState((state) => ({ ...state }));
- }
- else {
- control._resetDefaultValues();
- }
- }, [props.values, control]);
- React.useEffect(() => {
- if (props.errors) {
- control._setErrors(props.errors);
- }
- }, [props.errors, control]);
- React.useEffect(() => {
- if (!control._state.mount) {
- control._updateValid();
- control._state.mount = true;
- }
- if (control._state.watch) {
- control._state.watch = false;
- control._subjects.state.next({ ...control._formState });
- }
- control._removeUnmounted();
- });
- React.useEffect(() => {
- props.shouldUnregister &&
- control._subjects.values.next({
- values: control._getWatch(),
- });
- }, [props.shouldUnregister, control]);
- _formControl.current.formState = getProxyFormState(formState, control);
- return _formControl.current;
- }
- export { Controller, Form, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
- //# sourceMappingURL=index.esm.mjs.map
|