@formulier/react
Version:
Simple, performant form library for React
629 lines (618 loc) • 31.1 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// ../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/cjs/react-dom.production.js
var require_react_dom_production = __commonJS({
"../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/cjs/react-dom.production.js"(exports2) {
"use strict";
var React4 = require("react");
function formatProdErrorMessage(code) {
var url = "https://react.dev/errors/" + code;
if (1 < arguments.length) {
url += "?args[]=" + encodeURIComponent(arguments[1]);
for (var i = 2; i < arguments.length; i++)
url += "&args[]=" + encodeURIComponent(arguments[i]);
}
return "Minified React error #" + code + "; visit " + url + " for the full message or use the non-minified dev environment for full errors and additional helpful warnings.";
}
function noop() {
}
var Internals = {
d: {
f: noop,
r: function() {
throw Error(formatProdErrorMessage(522));
},
D: noop,
C: noop,
L: noop,
m: noop,
X: noop,
S: noop,
M: noop
},
p: 0,
findDOMNode: null
};
var REACT_PORTAL_TYPE = Symbol.for("react.portal");
function createPortal$1(children, containerInfo, implementation) {
var key = 3 < arguments.length && void 0 !== arguments[3] ? arguments[3] : null;
return {
$$typeof: REACT_PORTAL_TYPE,
key: null == key ? null : "" + key,
children,
containerInfo,
implementation
};
}
var ReactSharedInternals = React4.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
function getCrossOriginStringAs(as, input) {
if ("font" === as) return "";
if ("string" === typeof input)
return "use-credentials" === input ? input : "";
}
exports2.__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE = Internals;
exports2.createPortal = function(children, container) {
var key = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : null;
if (!container || 1 !== container.nodeType && 9 !== container.nodeType && 11 !== container.nodeType)
throw Error(formatProdErrorMessage(299));
return createPortal$1(children, container, null, key);
};
exports2.flushSync = function(fn) {
var previousTransition = ReactSharedInternals.T, previousUpdatePriority = Internals.p;
try {
if (ReactSharedInternals.T = null, Internals.p = 2, fn) return fn();
} finally {
ReactSharedInternals.T = previousTransition, Internals.p = previousUpdatePriority, Internals.d.f();
}
};
exports2.preconnect = function(href, options) {
"string" === typeof href && (options ? (options = options.crossOrigin, options = "string" === typeof options ? "use-credentials" === options ? options : "" : void 0) : options = null, Internals.d.C(href, options));
};
exports2.prefetchDNS = function(href) {
"string" === typeof href && Internals.d.D(href);
};
exports2.preinit = function(href, options) {
if ("string" === typeof href && options && "string" === typeof options.as) {
var as = options.as, crossOrigin = getCrossOriginStringAs(as, options.crossOrigin), integrity = "string" === typeof options.integrity ? options.integrity : void 0, fetchPriority = "string" === typeof options.fetchPriority ? options.fetchPriority : void 0;
"style" === as ? Internals.d.S(
href,
"string" === typeof options.precedence ? options.precedence : void 0,
{
crossOrigin,
integrity,
fetchPriority
}
) : "script" === as && Internals.d.X(href, {
crossOrigin,
integrity,
fetchPriority,
nonce: "string" === typeof options.nonce ? options.nonce : void 0
});
}
};
exports2.preinitModule = function(href, options) {
if ("string" === typeof href)
if ("object" === typeof options && null !== options) {
if (null == options.as || "script" === options.as) {
var crossOrigin = getCrossOriginStringAs(
options.as,
options.crossOrigin
);
Internals.d.M(href, {
crossOrigin,
integrity: "string" === typeof options.integrity ? options.integrity : void 0,
nonce: "string" === typeof options.nonce ? options.nonce : void 0
});
}
} else null == options && Internals.d.M(href);
};
exports2.preload = function(href, options) {
if ("string" === typeof href && "object" === typeof options && null !== options && "string" === typeof options.as) {
var as = options.as, crossOrigin = getCrossOriginStringAs(as, options.crossOrigin);
Internals.d.L(href, as, {
crossOrigin,
integrity: "string" === typeof options.integrity ? options.integrity : void 0,
nonce: "string" === typeof options.nonce ? options.nonce : void 0,
type: "string" === typeof options.type ? options.type : void 0,
fetchPriority: "string" === typeof options.fetchPriority ? options.fetchPriority : void 0,
referrerPolicy: "string" === typeof options.referrerPolicy ? options.referrerPolicy : void 0,
imageSrcSet: "string" === typeof options.imageSrcSet ? options.imageSrcSet : void 0,
imageSizes: "string" === typeof options.imageSizes ? options.imageSizes : void 0,
media: "string" === typeof options.media ? options.media : void 0
});
}
};
exports2.preloadModule = function(href, options) {
if ("string" === typeof href)
if (options) {
var crossOrigin = getCrossOriginStringAs(options.as, options.crossOrigin);
Internals.d.m(href, {
as: "string" === typeof options.as && "script" !== options.as ? options.as : void 0,
crossOrigin,
integrity: "string" === typeof options.integrity ? options.integrity : void 0
});
} else Internals.d.m(href);
};
exports2.requestFormReset = function(form) {
Internals.d.r(form);
};
exports2.unstable_batchedUpdates = function(fn, a) {
return fn(a);
};
exports2.useFormState = function(action, initialState, permalink) {
return ReactSharedInternals.H.useFormState(action, initialState, permalink);
};
exports2.useFormStatus = function() {
return ReactSharedInternals.H.useHostTransitionStatus();
};
exports2.version = "19.1.1";
}
});
// ../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/cjs/react-dom.development.js
var require_react_dom_development = __commonJS({
"../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/cjs/react-dom.development.js"(exports2) {
"use strict";
"production" !== process.env.NODE_ENV && (function() {
function noop() {
}
function testStringCoercion(value) {
return "" + value;
}
function createPortal$1(children, containerInfo, implementation) {
var key = 3 < arguments.length && void 0 !== arguments[3] ? arguments[3] : null;
try {
testStringCoercion(key);
var JSCompiler_inline_result = false;
} catch (e) {
JSCompiler_inline_result = true;
}
JSCompiler_inline_result && (console.error(
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
"function" === typeof Symbol && Symbol.toStringTag && key[Symbol.toStringTag] || key.constructor.name || "Object"
), testStringCoercion(key));
return {
$$typeof: REACT_PORTAL_TYPE,
key: null == key ? null : "" + key,
children,
containerInfo,
implementation
};
}
function getCrossOriginStringAs(as, input) {
if ("font" === as) return "";
if ("string" === typeof input)
return "use-credentials" === input ? input : "";
}
function getValueDescriptorExpectingObjectForWarning(thing) {
return null === thing ? "`null`" : void 0 === thing ? "`undefined`" : "" === thing ? "an empty string" : 'something with type "' + typeof thing + '"';
}
function getValueDescriptorExpectingEnumForWarning(thing) {
return null === thing ? "`null`" : void 0 === thing ? "`undefined`" : "" === thing ? "an empty string" : "string" === typeof thing ? JSON.stringify(thing) : "number" === typeof thing ? "`" + thing + "`" : 'something with type "' + typeof thing + '"';
}
function resolveDispatcher() {
var dispatcher = ReactSharedInternals.H;
null === dispatcher && console.error(
"Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:\n1. You might have mismatching versions of React and the renderer (such as React DOM)\n2. You might be breaking the Rules of Hooks\n3. You might have more than one copy of React in the same app\nSee https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem."
);
return dispatcher;
}
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
var React4 = require("react"), Internals = {
d: {
f: noop,
r: function() {
throw Error(
"Invalid form element. requestFormReset must be passed a form that was rendered by React."
);
},
D: noop,
C: noop,
L: noop,
m: noop,
X: noop,
S: noop,
M: noop
},
p: 0,
findDOMNode: null
}, REACT_PORTAL_TYPE = Symbol.for("react.portal"), ReactSharedInternals = React4.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
"function" === typeof Map && null != Map.prototype && "function" === typeof Map.prototype.forEach && "function" === typeof Set && null != Set.prototype && "function" === typeof Set.prototype.clear && "function" === typeof Set.prototype.forEach || console.error(
"React depends on Map and Set built-in types. Make sure that you load a polyfill in older browsers. https://reactjs.org/link/react-polyfills"
);
exports2.__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE = Internals;
exports2.createPortal = function(children, container) {
var key = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : null;
if (!container || 1 !== container.nodeType && 9 !== container.nodeType && 11 !== container.nodeType)
throw Error("Target container is not a DOM element.");
return createPortal$1(children, container, null, key);
};
exports2.flushSync = function(fn) {
var previousTransition = ReactSharedInternals.T, previousUpdatePriority = Internals.p;
try {
if (ReactSharedInternals.T = null, Internals.p = 2, fn)
return fn();
} finally {
ReactSharedInternals.T = previousTransition, Internals.p = previousUpdatePriority, Internals.d.f() && console.error(
"flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task."
);
}
};
exports2.preconnect = function(href, options) {
"string" === typeof href && href ? null != options && "object" !== typeof options ? console.error(
"ReactDOM.preconnect(): Expected the `options` argument (second) to be an object but encountered %s instead. The only supported option at this time is `crossOrigin` which accepts a string.",
getValueDescriptorExpectingEnumForWarning(options)
) : null != options && "string" !== typeof options.crossOrigin && console.error(
"ReactDOM.preconnect(): Expected the `crossOrigin` option (second argument) to be a string but encountered %s instead. Try removing this option or passing a string value instead.",
getValueDescriptorExpectingObjectForWarning(options.crossOrigin)
) : console.error(
"ReactDOM.preconnect(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.",
getValueDescriptorExpectingObjectForWarning(href)
);
"string" === typeof href && (options ? (options = options.crossOrigin, options = "string" === typeof options ? "use-credentials" === options ? options : "" : void 0) : options = null, Internals.d.C(href, options));
};
exports2.prefetchDNS = function(href) {
if ("string" !== typeof href || !href)
console.error(
"ReactDOM.prefetchDNS(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.",
getValueDescriptorExpectingObjectForWarning(href)
);
else if (1 < arguments.length) {
var options = arguments[1];
"object" === typeof options && options.hasOwnProperty("crossOrigin") ? console.error(
"ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. It looks like the you are attempting to set a crossOrigin property for this DNS lookup hint. Browsers do not perform DNS queries using CORS and setting this attribute on the resource hint has no effect. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.",
getValueDescriptorExpectingEnumForWarning(options)
) : console.error(
"ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.",
getValueDescriptorExpectingEnumForWarning(options)
);
}
"string" === typeof href && Internals.d.D(href);
};
exports2.preinit = function(href, options) {
"string" === typeof href && href ? null == options || "object" !== typeof options ? console.error(
"ReactDOM.preinit(): Expected the `options` argument (second) to be an object with an `as` property describing the type of resource to be preinitialized but encountered %s instead.",
getValueDescriptorExpectingEnumForWarning(options)
) : "style" !== options.as && "script" !== options.as && console.error(
'ReactDOM.preinit(): Expected the `as` property in the `options` argument (second) to contain a valid value describing the type of resource to be preinitialized but encountered %s instead. Valid values for `as` are "style" and "script".',
getValueDescriptorExpectingEnumForWarning(options.as)
) : console.error(
"ReactDOM.preinit(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.",
getValueDescriptorExpectingObjectForWarning(href)
);
if ("string" === typeof href && options && "string" === typeof options.as) {
var as = options.as, crossOrigin = getCrossOriginStringAs(as, options.crossOrigin), integrity = "string" === typeof options.integrity ? options.integrity : void 0, fetchPriority = "string" === typeof options.fetchPriority ? options.fetchPriority : void 0;
"style" === as ? Internals.d.S(
href,
"string" === typeof options.precedence ? options.precedence : void 0,
{
crossOrigin,
integrity,
fetchPriority
}
) : "script" === as && Internals.d.X(href, {
crossOrigin,
integrity,
fetchPriority,
nonce: "string" === typeof options.nonce ? options.nonce : void 0
});
}
};
exports2.preinitModule = function(href, options) {
var encountered = "";
"string" === typeof href && href || (encountered += " The `href` argument encountered was " + getValueDescriptorExpectingObjectForWarning(href) + ".");
void 0 !== options && "object" !== typeof options ? encountered += " The `options` argument encountered was " + getValueDescriptorExpectingObjectForWarning(options) + "." : options && "as" in options && "script" !== options.as && (encountered += " The `as` option encountered was " + getValueDescriptorExpectingEnumForWarning(options.as) + ".");
if (encountered)
console.error(
"ReactDOM.preinitModule(): Expected up to two arguments, a non-empty `href` string and, optionally, an `options` object with a valid `as` property.%s",
encountered
);
else
switch (encountered = options && "string" === typeof options.as ? options.as : "script", encountered) {
case "script":
break;
default:
encountered = getValueDescriptorExpectingEnumForWarning(encountered), console.error(
'ReactDOM.preinitModule(): Currently the only supported "as" type for this function is "script" but received "%s" instead. This warning was generated for `href` "%s". In the future other module types will be supported, aligning with the import-attributes proposal. Learn more here: (https://github.com/tc39/proposal-import-attributes)',
encountered,
href
);
}
if ("string" === typeof href)
if ("object" === typeof options && null !== options) {
if (null == options.as || "script" === options.as)
encountered = getCrossOriginStringAs(
options.as,
options.crossOrigin
), Internals.d.M(href, {
crossOrigin: encountered,
integrity: "string" === typeof options.integrity ? options.integrity : void 0,
nonce: "string" === typeof options.nonce ? options.nonce : void 0
});
} else null == options && Internals.d.M(href);
};
exports2.preload = function(href, options) {
var encountered = "";
"string" === typeof href && href || (encountered += " The `href` argument encountered was " + getValueDescriptorExpectingObjectForWarning(href) + ".");
null == options || "object" !== typeof options ? encountered += " The `options` argument encountered was " + getValueDescriptorExpectingObjectForWarning(options) + "." : "string" === typeof options.as && options.as || (encountered += " The `as` option encountered was " + getValueDescriptorExpectingObjectForWarning(options.as) + ".");
encountered && console.error(
'ReactDOM.preload(): Expected two arguments, a non-empty `href` string and an `options` object with an `as` property valid for a `<link rel="preload" as="..." />` tag.%s',
encountered
);
if ("string" === typeof href && "object" === typeof options && null !== options && "string" === typeof options.as) {
encountered = options.as;
var crossOrigin = getCrossOriginStringAs(
encountered,
options.crossOrigin
);
Internals.d.L(href, encountered, {
crossOrigin,
integrity: "string" === typeof options.integrity ? options.integrity : void 0,
nonce: "string" === typeof options.nonce ? options.nonce : void 0,
type: "string" === typeof options.type ? options.type : void 0,
fetchPriority: "string" === typeof options.fetchPriority ? options.fetchPriority : void 0,
referrerPolicy: "string" === typeof options.referrerPolicy ? options.referrerPolicy : void 0,
imageSrcSet: "string" === typeof options.imageSrcSet ? options.imageSrcSet : void 0,
imageSizes: "string" === typeof options.imageSizes ? options.imageSizes : void 0,
media: "string" === typeof options.media ? options.media : void 0
});
}
};
exports2.preloadModule = function(href, options) {
var encountered = "";
"string" === typeof href && href || (encountered += " The `href` argument encountered was " + getValueDescriptorExpectingObjectForWarning(href) + ".");
void 0 !== options && "object" !== typeof options ? encountered += " The `options` argument encountered was " + getValueDescriptorExpectingObjectForWarning(options) + "." : options && "as" in options && "string" !== typeof options.as && (encountered += " The `as` option encountered was " + getValueDescriptorExpectingObjectForWarning(options.as) + ".");
encountered && console.error(
'ReactDOM.preloadModule(): Expected two arguments, a non-empty `href` string and, optionally, an `options` object with an `as` property valid for a `<link rel="modulepreload" as="..." />` tag.%s',
encountered
);
"string" === typeof href && (options ? (encountered = getCrossOriginStringAs(
options.as,
options.crossOrigin
), Internals.d.m(href, {
as: "string" === typeof options.as && "script" !== options.as ? options.as : void 0,
crossOrigin: encountered,
integrity: "string" === typeof options.integrity ? options.integrity : void 0
})) : Internals.d.m(href));
};
exports2.requestFormReset = function(form) {
Internals.d.r(form);
};
exports2.unstable_batchedUpdates = function(fn, a) {
return fn(a);
};
exports2.useFormState = function(action, initialState, permalink) {
return resolveDispatcher().useFormState(action, initialState, permalink);
};
exports2.useFormStatus = function() {
return resolveDispatcher().useHostTransitionStatus();
};
exports2.version = "19.1.1";
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
})();
}
});
// ../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/index.js
var require_react_dom = __commonJS({
"../../node_modules/.pnpm/react-dom@19.1.1_react@19.1.1/node_modules/react-dom/index.js"(exports2, module2) {
"use strict";
function checkDCE() {
if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === "undefined" || typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE !== "function") {
return;
}
if (process.env.NODE_ENV !== "production") {
throw new Error("^_^");
}
try {
__REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE(checkDCE);
} catch (err) {
console.error(err);
}
}
if (process.env.NODE_ENV === "production") {
checkDCE();
module2.exports = require_react_dom_production();
} else {
module2.exports = require_react_dom_development();
}
}
});
// src/index.ts
var index_exports = {};
__export(index_exports, {
FormProvider: () => FormProvider,
useCreateForm: () => useCreateForm,
useFormField: () => useFormField,
useFormFieldArray: () => useFormFieldArray,
useFormFieldValue: () => useFormFieldValue,
useFormInstance: () => useFormInstance,
useFormSelector: () => useFormSelector,
useOptionalFormInstance: () => useOptionalFormInstance,
useSubmitHandler: () => useSubmitHandler
});
module.exports = __toCommonJS(index_exports);
__reExport(index_exports, require("@formulier/core"), module.exports);
// src/form.ts
var React2 = __toESM(require("react"), 1);
var import_with_selector = require("use-sync-external-store/with-selector");
var import_core = require("@formulier/core");
// src/utils.ts
var React = __toESM(require("react"), 1);
function useEvent(handler) {
const handlerRef = React.useRef(handler);
handlerRef.current = handler;
return React.useCallback((...args) => {
const fn = handlerRef.current;
return fn(...args);
}, []);
}
function invariant(condition, message) {
if (!condition) throw new Error(`@formulier/react ${message}`);
}
// src/form.ts
var formContext = React2.createContext(void 0);
function FormProvider({ form, children }) {
const value = React2.useMemo(() => ({ form }), [form]);
return React2.createElement(formContext.Provider, { value }, children);
}
function useFormInstance() {
const value = React2.useContext(formContext);
invariant(value, `useFormInstance must be used inside of FormProvider`);
return value.form;
}
function useOptionalFormInstance() {
const value = React2.useContext(formContext);
return value?.form || null;
}
function useCreateForm(options) {
const [form] = React2.useState(() => new import_core.Formulier(options));
if (!form.hasMounted) form.setValues(options.initialValues);
return form;
}
function useFormSelector(form, selector, equalityFn) {
return (0, import_with_selector.useSyncExternalStoreWithSelector)(
form.store.subscribe,
form.store.getState,
form.store.getState,
selector,
equalityFn
);
}
function useSubmitHandler(form, onSubmit) {
return useEvent((event) => {
event.preventDefault?.();
const valid = form.validateFields();
if (valid) {
onSubmit(form.store.getState().values);
}
form.incrementSubmitCount();
});
}
// src/field.ts
var React3 = __toESM(require("react"), 1);
var ReactDOM = __toESM(require_react_dom(), 1);
var import_core2 = require("@formulier/core");
var callCallback = (cb) => void cb();
function useFormField(form, options) {
const id = React3.useId();
const rerender = React3.useReducer((x) => x + 1, 0)[1];
const { name, validate, valueOptions, flushSyncOnChange = false, flushSyncOnBlur = false } = options;
const value = useFormFieldValue(form, name, valueOptions);
const error = useFormSelector(form, (state) => state.errors[name]) || null;
const touched = useFormSelector(form, (state) => state.touched[name]) ?? false;
const hasSubmitted = useFormSelector(form, (state) => state.submitCount > 0);
React3.useEffect(() => {
const unregisterField = form.registerField(name, validate);
const removeInstance = form.addInstance(name, id);
return () => {
removeInstance();
queueMicrotask(() => {
ReactDOM.flushSync(rerender);
if (!form.hasInstance(name)) unregisterField();
});
};
}, [form, name, id, validate, rerender]);
const onChange = useEvent((value2) => {
const maybeFlushSync = flushSyncOnChange ? ReactDOM.flushSync : callCallback;
maybeFlushSync(() => {
form.setFieldValue(name, value2);
if (touched || hasSubmitted) form.validateField(name);
});
});
const onBlur = useEvent(() => {
const maybeFlushSync = flushSyncOnBlur ? ReactDOM.flushSync : callCallback;
maybeFlushSync(() => {
form.validateField(name);
form.touchField(name);
});
});
const field = { id, name, value, onChange, onBlur };
const meta = { error, touched };
return [field, meta];
}
function useFormFieldArray(form, name, options) {
const { valueOptions } = options || {};
const items = useFormFieldValue(form, name, valueOptions);
invariant(Array.isArray(items), `Field "${name}" is not an array.`);
const update = (value) => void form.setFieldValue(name, value);
const arrayMethods = {
push: useEvent((item) => update(import_core2.arrayUtils.push(items, item))),
insert: useEvent((item, index) => update(import_core2.arrayUtils.insert(items, item, index))),
remove: useEvent((index) => update(import_core2.arrayUtils.remove(items, index))),
move: useEvent((fromIndex, toIndex) => update(import_core2.arrayUtils.move(items, fromIndex, toIndex))),
swap: useEvent((fromIndex, toIndex) => update(import_core2.arrayUtils.swap(items, fromIndex, toIndex)))
};
return [items, arrayMethods];
}
function useFormFieldValue(form, name, options) {
const { fallback, equalityFn = import_core2.stateUtils.isEqual } = options || {};
return useFormSelector(
form,
(state) => import_core2.stateUtils.getPath(state.values, name, fallback),
equalityFn
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FormProvider,
useCreateForm,
useFormField,
useFormFieldArray,
useFormFieldValue,
useFormInstance,
useFormSelector,
useOptionalFormInstance,
useSubmitHandler,
...require("@formulier/core")
});
/*! Bundled license information:
react-dom/cjs/react-dom.production.js:
(**
* @license React
* react-dom.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
react-dom/cjs/react-dom.development.js:
(**
* @license React
* react-dom.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
*/