react-swift-form
Version:
React library fro form validation
1,854 lines (1,850 loc) • 49.4 kB
JavaScript
import yr, { createContext as Kr, useContext as de, useRef as U, useState as Ae, useCallback as k, useEffect as Ee, useMemo as xe } from "react";
var Je = { exports: {} }, Ve = {};
/**
* @license React
* react-jsx-runtime.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var lr;
function Jr() {
if (lr)
return Ve;
lr = 1;
var r = yr, t = Symbol.for("react.element"), o = Symbol.for("react.fragment"), a = Object.prototype.hasOwnProperty, n = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, i = { key: !0, ref: !0, __self: !0, __source: !0 };
function l(u, f, h) {
var m, E = {}, C = null, p = null;
h !== void 0 && (C = "" + h), f.key !== void 0 && (C = "" + f.key), f.ref !== void 0 && (p = f.ref);
for (m in f)
a.call(f, m) && !i.hasOwnProperty(m) && (E[m] = f[m]);
if (u && u.defaultProps)
for (m in f = u.defaultProps, f)
E[m] === void 0 && (E[m] = f[m]);
return { $$typeof: t, type: u, key: C, ref: p, props: E, _owner: n.current };
}
return Ve.Fragment = o, Ve.jsx = l, Ve.jsxs = l, Ve;
}
var Pe = {};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var cr;
function zr() {
return cr || (cr = 1, process.env.NODE_ENV !== "production" && function() {
var r = yr, t = Symbol.for("react.element"), o = Symbol.for("react.portal"), a = Symbol.for("react.fragment"), n = Symbol.for("react.strict_mode"), i = Symbol.for("react.profiler"), l = Symbol.for("react.provider"), u = Symbol.for("react.context"), f = Symbol.for("react.forward_ref"), h = Symbol.for("react.suspense"), m = Symbol.for("react.suspense_list"), E = Symbol.for("react.memo"), C = Symbol.for("react.lazy"), p = Symbol.for("react.offscreen"), x = Symbol.iterator, D = "@@iterator";
function M(e) {
if (e === null || typeof e != "object")
return null;
var s = x && e[x] || e[D];
return typeof s == "function" ? s : null;
}
var y = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
function R(e) {
{
for (var s = arguments.length, c = new Array(s > 1 ? s - 1 : 0), g = 1; g < s; g++)
c[g - 1] = arguments[g];
$("error", e, c);
}
}
function $(e, s, c) {
{
var g = y.ReactDebugCurrentFrame, w = g.getStackAddendum();
w !== "" && (s += "%s", c = c.concat([w]));
var T = c.map(function(_) {
return String(_);
});
T.unshift("Warning: " + s), Function.prototype.apply.call(console[e], console, T);
}
}
var re = !1, L = !1, X = !1, te = !1, V = !1, F;
F = Symbol.for("react.module.reference");
function j(e) {
return !!(typeof e == "string" || typeof e == "function" || e === a || e === i || V || e === n || e === h || e === m || te || e === p || re || L || X || typeof e == "object" && e !== null && (e.$$typeof === C || e.$$typeof === E || e.$$typeof === l || e.$$typeof === u || e.$$typeof === f || // This needs to include all possible module reference object
// types supported by any Flight configuration anywhere since
// we don't know which Flight build this will end up being used
// with.
e.$$typeof === F || e.getModuleId !== void 0));
}
function A(e, s, c) {
var g = e.displayName;
if (g)
return g;
var w = s.displayName || s.name || "";
return w !== "" ? c + "(" + w + ")" : c;
}
function q(e) {
return e.displayName || "Context";
}
function P(e) {
if (e == null)
return null;
if (typeof e.tag == "number" && R("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function")
return e.displayName || e.name || null;
if (typeof e == "string")
return e;
switch (e) {
case a:
return "Fragment";
case o:
return "Portal";
case i:
return "Profiler";
case n:
return "StrictMode";
case h:
return "Suspense";
case m:
return "SuspenseList";
}
if (typeof e == "object")
switch (e.$$typeof) {
case u:
var s = e;
return q(s) + ".Consumer";
case l:
var c = e;
return q(c._context) + ".Provider";
case f:
return A(e, e.render, "ForwardRef");
case E:
var g = e.displayName || null;
return g !== null ? g : P(e.type) || "Memo";
case C: {
var w = e, T = w._payload, _ = w._init;
try {
return P(_(T));
} catch {
return null;
}
}
}
return null;
}
var K = Object.assign, le = 0, Z, ye, je, me, J, Re, H;
function ne() {
}
ne.__reactDisabledLog = !0;
function ke() {
{
if (le === 0) {
Z = console.log, ye = console.info, je = console.warn, me = console.error, J = console.group, Re = console.groupCollapsed, H = console.groupEnd;
var e = {
configurable: !0,
enumerable: !0,
value: ne,
writable: !0
};
Object.defineProperties(console, {
info: e,
log: e,
warn: e,
error: e,
group: e,
groupCollapsed: e,
groupEnd: e
});
}
le++;
}
}
function De() {
{
if (le--, le === 0) {
var e = {
configurable: !0,
enumerable: !0,
writable: !0
};
Object.defineProperties(console, {
log: K({}, e, {
value: Z
}),
info: K({}, e, {
value: ye
}),
warn: K({}, e, {
value: je
}),
error: K({}, e, {
value: me
}),
group: K({}, e, {
value: J
}),
groupCollapsed: K({}, e, {
value: Re
}),
groupEnd: K({}, e, {
value: H
})
});
}
le < 0 && R("disabledDepth fell below zero. This is a bug in React. Please file an issue.");
}
}
var ce = y.ReactCurrentDispatcher, oe;
function ae(e, s, c) {
{
if (oe === void 0)
try {
throw Error();
} catch (w) {
var g = w.stack.trim().match(/\n( *(at )?)/);
oe = g && g[1] || "";
}
return `
` + oe + e;
}
}
var ve = !1, se;
{
var Me = typeof WeakMap == "function" ? WeakMap : Map;
se = new Me();
}
function Ce(e, s) {
if (!e || ve)
return "";
{
var c = se.get(e);
if (c !== void 0)
return c;
}
var g;
ve = !0;
var w = Error.prepareStackTrace;
Error.prepareStackTrace = void 0;
var T;
T = ce.current, ce.current = null, ke();
try {
if (s) {
var _ = function() {
throw Error();
};
if (Object.defineProperty(_.prototype, "props", {
set: function() {
throw Error();
}
}), typeof Reflect == "object" && Reflect.construct) {
try {
Reflect.construct(_, []);
} catch (ie) {
g = ie;
}
Reflect.construct(e, [], _);
} else {
try {
_.call();
} catch (ie) {
g = ie;
}
e.call(_.prototype);
}
} else {
try {
throw Error();
} catch (ie) {
g = ie;
}
e();
}
} catch (ie) {
if (ie && g && typeof ie.stack == "string") {
for (var S = ie.stack.split(`
`), B = g.stack.split(`
`), N = S.length - 1, I = B.length - 1; N >= 1 && I >= 0 && S[N] !== B[I]; )
I--;
for (; N >= 1 && I >= 0; N--, I--)
if (S[N] !== B[I]) {
if (N !== 1 || I !== 1)
do
if (N--, I--, I < 0 || S[N] !== B[I]) {
var z = `
` + S[N].replace(" at new ", " at ");
return e.displayName && z.includes("<anonymous>") && (z = z.replace("<anonymous>", e.displayName)), typeof e == "function" && se.set(e, z), z;
}
while (N >= 1 && I >= 0);
break;
}
}
} finally {
ve = !1, ce.current = T, De(), Error.prepareStackTrace = w;
}
var _e = e ? e.displayName || e.name : "", ur = _e ? ae(_e) : "";
return typeof e == "function" && se.set(e, ur), ur;
}
function Ie(e, s, c) {
return Ce(e, !1);
}
function Se(e) {
var s = e.prototype;
return !!(s && s.isReactComponent);
}
function ge(e, s, c) {
if (e == null)
return "";
if (typeof e == "function")
return Ce(e, Se(e));
if (typeof e == "string")
return ae(e);
switch (e) {
case h:
return ae("Suspense");
case m:
return ae("SuspenseList");
}
if (typeof e == "object")
switch (e.$$typeof) {
case f:
return Ie(e.render);
case E:
return ge(e.type, s, c);
case C: {
var g = e, w = g._payload, T = g._init;
try {
return ge(T(w), s, c);
} catch {
}
}
}
return "";
}
var be = Object.prototype.hasOwnProperty, Te = {}, Fe = y.ReactDebugCurrentFrame;
function pe(e) {
if (e) {
var s = e._owner, c = ge(e.type, e._source, s ? s.type : null);
Fe.setExtraStackFrame(c);
} else
Fe.setExtraStackFrame(null);
}
function d(e, s, c, g, w) {
{
var T = Function.call.bind(be);
for (var _ in e)
if (T(e, _)) {
var S = void 0;
try {
if (typeof e[_] != "function") {
var B = Error((g || "React class") + ": " + c + " type `" + _ + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[_] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");
throw B.name = "Invariant Violation", B;
}
S = e[_](s, _, g, c, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED");
} catch (N) {
S = N;
}
S && !(S instanceof Error) && (pe(w), R("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", g || "React class", c, _, typeof S), pe(null)), S instanceof Error && !(S.message in Te) && (Te[S.message] = !0, pe(w), R("Failed %s type: %s", c, S.message), pe(null));
}
}
}
var b = Array.isArray;
function v(e) {
return b(e);
}
function O(e) {
{
var s = typeof Symbol == "function" && Symbol.toStringTag, c = s && e[Symbol.toStringTag] || e.constructor.name || "Object";
return c;
}
}
function W(e) {
try {
return Y(e), !1;
} catch {
return !0;
}
}
function Y(e) {
return "" + e;
}
function he(e) {
if (W(e))
return R("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", O(e)), Y(e);
}
var Q = y.ReactCurrentOwner, Pr = {
key: !0,
ref: !0,
__self: !0,
__source: !0
}, er, rr, Ye;
Ye = {};
function xr(e) {
if (be.call(e, "ref")) {
var s = Object.getOwnPropertyDescriptor(e, "ref").get;
if (s && s.isReactWarning)
return !1;
}
return e.ref !== void 0;
}
function Ar(e) {
if (be.call(e, "key")) {
var s = Object.getOwnPropertyDescriptor(e, "key").get;
if (s && s.isReactWarning)
return !1;
}
return e.key !== void 0;
}
function Nr(e, s) {
if (typeof e.ref == "string" && Q.current && s && Q.current.stateNode !== s) {
var c = P(Q.current.type);
Ye[c] || (R('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref', P(Q.current.type), e.ref), Ye[c] = !0);
}
}
function kr(e, s) {
{
var c = function() {
er || (er = !0, R("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", s));
};
c.isReactWarning = !0, Object.defineProperty(e, "key", {
get: c,
configurable: !0
});
}
}
function Dr(e, s) {
{
var c = function() {
rr || (rr = !0, R("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", s));
};
c.isReactWarning = !0, Object.defineProperty(e, "ref", {
get: c,
configurable: !0
});
}
}
var Mr = function(e, s, c, g, w, T, _) {
var S = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: t,
// Built-in properties that belong on the element
type: e,
key: s,
ref: c,
props: _,
// Record the component responsible for creating this element.
_owner: T
};
return S._store = {}, Object.defineProperty(S._store, "validated", {
configurable: !1,
enumerable: !1,
writable: !0,
value: !1
}), Object.defineProperty(S, "_self", {
configurable: !1,
enumerable: !1,
writable: !1,
value: g
}), Object.defineProperty(S, "_source", {
configurable: !1,
enumerable: !1,
writable: !1,
value: w
}), Object.freeze && (Object.freeze(S.props), Object.freeze(S)), S;
};
function Ir(e, s, c, g, w) {
{
var T, _ = {}, S = null, B = null;
c !== void 0 && (he(c), S = "" + c), Ar(s) && (he(s.key), S = "" + s.key), xr(s) && (B = s.ref, Nr(s, w));
for (T in s)
be.call(s, T) && !Pr.hasOwnProperty(T) && (_[T] = s[T]);
if (e && e.defaultProps) {
var N = e.defaultProps;
for (T in N)
_[T] === void 0 && (_[T] = N[T]);
}
if (S || B) {
var I = typeof e == "function" ? e.displayName || e.name || "Unknown" : e;
S && kr(_, I), B && Dr(_, I);
}
return Mr(e, S, B, w, g, Q.current, _);
}
}
var Be = y.ReactCurrentOwner, tr = y.ReactDebugCurrentFrame;
function Oe(e) {
if (e) {
var s = e._owner, c = ge(e.type, e._source, s ? s.type : null);
tr.setExtraStackFrame(c);
} else
tr.setExtraStackFrame(null);
}
var Ue;
Ue = !1;
function He(e) {
return typeof e == "object" && e !== null && e.$$typeof === t;
}
function nr() {
{
if (Be.current) {
var e = P(Be.current.type);
if (e)
return `
Check the render method of \`` + e + "`.";
}
return "";
}
}
function Lr(e) {
{
if (e !== void 0) {
var s = e.fileName.replace(/^.*[\\\/]/, ""), c = e.lineNumber;
return `
Check your code at ` + s + ":" + c + ".";
}
return "";
}
}
var or = {};
function $r(e) {
{
var s = nr();
if (!s) {
var c = typeof e == "string" ? e : e.displayName || e.name;
c && (s = `
Check the top-level render call using <` + c + ">.");
}
return s;
}
}
function ar(e, s) {
{
if (!e._store || e._store.validated || e.key != null)
return;
e._store.validated = !0;
var c = $r(s);
if (or[c])
return;
or[c] = !0;
var g = "";
e && e._owner && e._owner !== Be.current && (g = " It was passed a child from " + P(e._owner.type) + "."), Oe(e), R('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', c, g), Oe(null);
}
}
function sr(e, s) {
{
if (typeof e != "object")
return;
if (v(e))
for (var c = 0; c < e.length; c++) {
var g = e[c];
He(g) && ar(g, s);
}
else if (He(e))
e._store && (e._store.validated = !0);
else if (e) {
var w = M(e);
if (typeof w == "function" && w !== e.entries)
for (var T = w.call(e), _; !(_ = T.next()).done; )
He(_.value) && ar(_.value, s);
}
}
}
function Wr(e) {
{
var s = e.type;
if (s == null || typeof s == "string")
return;
var c;
if (typeof s == "function")
c = s.propTypes;
else if (typeof s == "object" && (s.$$typeof === f || // Note: Memo only checks outer props here.
// Inner props are checked in the reconciler.
s.$$typeof === E))
c = s.propTypes;
else
return;
if (c) {
var g = P(s);
d(c, e.props, "prop", g, e);
} else if (s.PropTypes !== void 0 && !Ue) {
Ue = !0;
var w = P(s);
R("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", w || "Unknown");
}
typeof s.getDefaultProps == "function" && !s.getDefaultProps.isReactClassApproved && R("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.");
}
}
function Yr(e) {
{
for (var s = Object.keys(e.props), c = 0; c < s.length; c++) {
var g = s[c];
if (g !== "children" && g !== "key") {
Oe(e), R("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", g), Oe(null);
break;
}
}
e.ref !== null && (Oe(e), R("Invalid attribute `ref` supplied to `React.Fragment`."), Oe(null));
}
}
function ir(e, s, c, g, w, T) {
{
var _ = j(e);
if (!_) {
var S = "";
(e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (S += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");
var B = Lr(w);
B ? S += B : S += nr();
var N;
e === null ? N = "null" : v(e) ? N = "array" : e !== void 0 && e.$$typeof === t ? (N = "<" + (P(e.type) || "Unknown") + " />", S = " Did you accidentally export a JSX literal instead of a component?") : N = typeof e, R("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", N, S);
}
var I = Ir(e, s, c, w, T);
if (I == null)
return I;
if (_) {
var z = s.children;
if (z !== void 0)
if (g)
if (v(z)) {
for (var _e = 0; _e < z.length; _e++)
sr(z[_e], e);
Object.freeze && Object.freeze(z);
} else
R("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");
else
sr(z, e);
}
return e === a ? Yr(I) : Wr(I), I;
}
}
function Br(e, s, c) {
return ir(e, s, c, !0);
}
function Ur(e, s, c) {
return ir(e, s, c, !1);
}
var Hr = Ur, qr = Br;
Pe.Fragment = a, Pe.jsx = Hr, Pe.jsxs = qr;
}()), Pe;
}
process.env.NODE_ENV === "production" ? Je.exports = Jr() : Je.exports = zr();
var we = Je.exports;
const ee = {
all: {},
global: {},
manual: {},
native: {},
validator: {}
}, ze = {
changedFields: /* @__PURE__ */ new Set(),
isSubmitting: !1,
isValid: !0,
isValidating: !1,
submitCount: 0,
touchedFields: /* @__PURE__ */ new Set()
}, Ze = Symbol.for("rsf-default"), ue = Kr({
errors: ee,
form: { current: null },
mode: "submit",
onChange: () => () => null,
onError: () => () => null,
onReset: () => () => null,
onSubmit: () => () => null,
register() {
},
reset: () => null,
revalidateMode: "submit",
states: {
...ze,
changedFields: [],
dirtyFields: [],
isChanged: !1,
isDirty: !1,
isPristine: !0,
isReady: !1,
isSubmitted: !1,
isTouched: !1,
isValid: !0,
touchedFields: []
},
subscribe() {
return () => {
};
},
unregister() {
},
useNativeValidation: !0,
validate() {
return Promise.resolve([!0, ee]);
},
watch: () => () => null
});
function Ge(r, t) {
return r.filter((o) => t.includes(o));
}
function Rr(r) {
return r instanceof Object && "id" in r;
}
function fr(r) {
return r instanceof Object && !Rr(r) && "error" in r;
}
function Gr(r, t) {
const o = t.split("."), { length: a } = o;
let n = 0, i = r;
for (; typeof i == "object" && i != null && n < a; )
i = i[o[n++]];
return n && n === a ? i : void 0;
}
function $e(r, t, o = !1) {
return !Array.from(new Set(Object.keys(r).concat(Object.keys(t)))).some((n) => typeof r[n] != typeof t[n] ? !0 : !o || r[n] === null || typeof r[n] != "object" ? r[n] !== t[n] : !$e(
r[n],
t[n],
o
));
}
function fe(r, t) {
return Object.fromEntries(Object.entries(r).filter(t));
}
function qe(r) {
return typeof r == "function";
}
function Xr(r) {
return !!(r && "names" in r && r.names instanceof Array && "validator" in r && typeof r.validator == "function");
}
function Qe(r) {
return "setErrors" in r;
}
function Zr(r) {
if (!r)
return null;
for (const t in r)
if (t !== "customError" && t !== "valid" && r[t])
return t;
return null;
}
function Xe(r) {
const { form: t, names: o, transformers: a = {}, values: n = {} } = r, i = new FormData(t), l = new Map(
Ne(t).map((h) => (h = G(h), [h.name, h]))
);
return Array.from(
new Set(Object.keys(n).concat(Array.from(i.keys())))
).filter((h) => !o || o.includes(h)).reduce((h, m) => {
if (n[m] !== void 0)
h[m] = n[m];
else {
const E = l.get(m);
h[m] = E ? Fr(E) : i.get(m);
}
return a[m] && (h[m] = a[m](h[m])), h;
}, {});
}
function Qr(r, t = {}) {
const o = Object.fromEntries(
r.reduce((a, n) => {
if (n.messages)
for (const i of n.names)
a.push([i, n.messages]);
return a;
}, [])
);
return o[Ze] = t, o;
}
function Ke(r, t) {
return t ? fe(r, ([o]) => t.includes(o)) : r;
}
function We(r) {
const t = Object.entries(r.native).find(([, o]) => o);
if (t)
r.main = {
error: t[1],
global: !1,
id: t[0],
names: [t[0]]
};
else {
const o = Object.entries(r.manual).find(([, a]) => a);
if (o)
r.main = {
error: o[1] ?? "",
global: !1,
id: o[0],
names: [o[0]]
};
else {
const a = Object.entries(r.validator).find(
([, { error: n }]) => n
);
if (a)
r.main = {
error: a[1].error,
global: a[1].global,
id: a[0],
names: a[1].names
};
else {
const n = Object.entries(r.global).find(
([, { error: i }]) => i
);
n && (r.main = {
error: n[1].error,
global: n[1].global,
id: n[0],
names: n[1].names
});
}
}
}
return r.main;
}
function et(r, t) {
const o = /* @__PURE__ */ new Set();
for (const a of r) {
const { id: n, names: i } = a;
(!t || Ge(t, i).length > 0) && o.add(n);
}
return [...o];
}
function Sr(r, t, o = {}) {
const a = Object.values(t).reduce(
(n, { error: i, names: l }) => {
for (const u of l)
n[u] || (n[u] = i);
return n;
},
{}
);
for (const [n, i] of Object.entries(o))
(i || !a[n]) && (a[n] = i ?? "");
for (const [n, i] of Object.entries(r))
(i || !a[n]) && (a[n] = i);
return a;
}
function Or(r) {
const {
global: t = !0,
ids: o,
manualErrors: a = {},
names: n,
nativeErrors: i,
validatorErrors: l
} = r, u = Ke(i, n), f = Ke(l, o), h = Ke(a, n), m = {
all: Sr(u, f, h),
global: t ? fe(f, ([, { global: E }]) => E) : {},
manual: h,
native: u,
validator: f
};
return We(m), m;
}
function dr(r, t) {
const { global: o, validator: a } = t, n = {
...r.native,
...t.native
}, i = {
...r.manual,
...t.manual
}, u = {
all: Sr(n, a, i),
global: o,
manual: i,
native: n,
validator: a
};
return We(u), u;
}
function mr(r) {
return !!r.main;
}
function _r(r, t = {}, o = {}) {
return r ? t[r] ? t[r] : o[r] ?? r : "";
}
function rt(r) {
const { defaultMessages: t = {}, input: o, messages: a = {} } = r, n = G(o);
n.setCustomValidity("");
const { validity: i } = n, l = Zr(i);
if (l) {
const u = a[l] ?? t[l];
return u && n.setCustomValidity(u), u ?? n.validationMessage;
}
return "";
}
function tt(r) {
const { fieldMessages: t, form: o } = r;
return Ne(o).reduce((n, i) => {
const l = G(i).name ?? "";
return n[l] = rt({
defaultMessages: t[Ze],
input: i,
messages: t[l]
}), n;
}, {});
}
function nt(r) {
const { errors: t = {}, fieldMessages: o = {}, form: a } = r, n = Object.fromEntries(
Object.entries(t).map(([i, l]) => [
i,
_r(
l,
o[i] ?? o[Ze]
)
])
);
for (const [i, l] of Object.entries(n)) {
const u = G(a.elements[i]);
u && l && !u.validationMessage && u.setCustomValidity(l);
}
return n;
}
function ot(r) {
const { form: t, transformers: o = {}, validators: a = [], values: n = {} } = r, i = a.map((l) => {
const { names: u, validator: f } = l;
return f ? f(Xe({ form: t, names: u, transformers: o, values: n }), u) : "";
});
return Promise.all(Object.values(i));
}
function at(r) {
const { defaultMessages: t = {}, form: o, validatorResults: a, validators: n } = r, i = {};
return a.forEach((l, u) => {
const { id: f, messages: h, names: m } = n[u];
if (l) {
const E = _r(l, h, t);
for (const C of m) {
const p = G(o.elements[C]);
p && E && !p.validationMessage && p.setCustomValidity(E);
}
i[f] = {
error: E,
global: !Qe(n[u]),
names: m
};
}
}), i;
}
function vr(r, t) {
if (t) {
const o = r.find(
(a) => t.names.includes(G(a).name)
);
if (o)
return G(o).focus(), !0;
}
return !1;
}
function st(r) {
const {
display: t,
errors: o,
filterLocalErrors: a,
focusOnError: n,
form: i,
localFields: l = {},
names: u,
revalidate: f,
setErrors: h,
useNativeValidation: m,
validators: E
} = r;
if (!t && !f)
return;
const { all: C, global: p, manual: x, native: D, validator: M } = o, y = Ne(i);
let R = !1;
function $() {
return R;
}
function re(V) {
R = V;
}
if (m) {
if (!u)
t && i.reportValidity();
else if (o.main && (t || f)) {
const V = o.main.names[0];
G(i.elements[V]).reportValidity();
}
return;
}
const L = /* @__PURE__ */ new Set();
for (const V of Object.keys(l))
L.add(V);
const X = Array.from(L), te = /* @__PURE__ */ new Map();
for (const V of E)
if (!(u && Ge(u, V.names).length === 0))
if (Qe(V)) {
const {
id: F,
names: j,
setErrors: A,
validator: q
} = V;
if (!q)
te.set(
A,
Or({
global: !1,
ids: [F],
manualErrors: x,
names: j,
nativeErrors: D,
validatorErrors: M
})
);
else if (M[F]) {
const P = te.get(A);
P && (P.validator = { ...P == null ? void 0 : P.validator, [F]: M[F] });
}
} else {
const { id: F, names: j } = V;
for (const A of j) {
const q = l[A];
if (q) {
const P = te.get(q);
P && M[F] && (P.validator = { ...P == null ? void 0 : P.validator, [F]: M[F] });
}
}
}
for (const [V, F] of te)
We(F), V((j) => {
if (t || f && mr(j)) {
n && !$() && re(vr(y, F.main));
const A = dr(j, F);
return A.main ? $e(A, j, !0) ? j : A : ee;
}
return j;
});
h((V) => {
if (t || f && mr(V)) {
let F = o;
a && (F = {
all: fe(C, ([A]) => !X.includes(A)),
global: p,
manual: fe(x, ([A]) => !X.includes(A)),
native: fe(D, ([A]) => !X.includes(A)),
validator: fe(
M,
([, { names: A }]) => Ge(X, A).length !== A.length
)
}, We(F)), n && !$() && re(vr(y, F.main));
const j = dr(V, F);
return j.main ? $e(j, V, !0) ? V : j : ee;
}
return V;
});
}
async function it(r) {
const {
display: t,
errors: o = {},
filterLocalErrors: a,
focusOnError: n,
form: i,
localFields: l,
messages: u,
names: f,
revalidate: h,
setErrors: m,
transformers: E = {},
useNativeValidation: C,
validators: p,
values: x = {}
} = r, D = Qr(p, u), M = await ot({
form: i,
transformers: E,
validators: p,
values: x
}), y = tt({ fieldMessages: D, form: i }), R = nt({
errors: o,
fieldMessages: D,
form: i
}), $ = at({
defaultMessages: u,
form: i,
validatorResults: M,
validators: p
}), re = et(p, f), L = Or({
ids: re,
manualErrors: R,
names: f,
nativeErrors: y,
validatorErrors: $
});
return st({
display: t,
errors: L,
filterLocalErrors: a,
focusOnError: n,
form: i,
localFields: l,
names: f,
revalidate: h,
setErrors: m,
useNativeValidation: C,
validators: p
}), L;
}
function wr(r) {
return r instanceof HTMLInputElement || r instanceof HTMLSelectElement || r instanceof HTMLTextAreaElement || r instanceof RadioNodeList;
}
function G(r) {
return r instanceof RadioNodeList ? r[0] : r;
}
const ut = ["submit", "reset"];
function Ne(r) {
return [...r.elements].filter(
(t) => wr(t) && t.name && !ut.includes(t.type)
);
}
function lt(r) {
const t = {};
for (const o of r.values()) {
const { setErrors: a } = o;
for (const n of o.names)
t[n] = a;
}
return t;
}
function gr(r, t, o) {
const a = [];
for (const n of r.values()) {
const {
defaultValues: i,
onBlurOptOut: l,
onChangeOptOut: u,
setErrors: f,
transformers: h,
validators: m,
...E
} = n;
if (a.push({ ...E, setErrors: f }), qe(m))
a.push({
...E,
setErrors: f,
validator: m
});
else if (Xr(m)) {
const { names: C, validator: p } = m;
a.push({
...E,
names: C,
setErrors: f,
validator: p
});
} else if (typeof m == "object")
for (const [C, p] of Object.entries(m))
if (qe(p))
a.push({
...E,
id: C,
names: [C],
setErrors: f,
validator: p
});
else {
const { names: x, validator: D } = p;
a.push({
...E,
id: C,
names: x,
setErrors: f,
validator: D
});
}
}
if (t)
for (const [n, i] of Object.entries(t))
if (qe(i))
a.push({
id: n,
messages: o,
names: [n],
validator: i
});
else {
const { names: l, validator: u } = i;
a.push({
id: n,
messages: o,
names: l,
validator: u
});
}
return a;
}
function jr(r) {
return typeof r == "object" && r !== null && "target" in r;
}
function Cr(r) {
return "type" in r && r.type === "checkbox";
}
function br(r) {
return r.type === "file";
}
function ct(r) {
return r.tagName === "SELECT";
}
const ft = ["INPUT", "SELECT", "TEXTAREA"];
function Tr(r) {
return !!("tagName" in r && ft.includes(r.tagName));
}
function pr(r) {
return jr(r) && r.target && Tr(r.target) ? r.target.name : null;
}
function Fr(r) {
if ("multiple" in r && r.multiple) {
if (r.type === "email")
return String(r.value).split(",").map((t) => t.trim());
if (br(r) && r.files)
return [...r.files];
if (ct(r))
return [...r.options].filter((t) => t.selected).map((t) => t.value);
} else if (br(r) && r.files)
return r.files[0];
return r.value;
}
function dt(r, t) {
let o = r;
return jr(r) && r.target && (Cr(r.target) ? o = r.target.checked : Tr(r.target) && (o = Fr(r.target))), t ? t(o) : o;
}
function mt(r, t, o) {
return !t || o === "all" || o === t || o instanceof Array && o.includes(t) ? !1 : ![...r].some(
({ onChangeOptOut: a }) => a === t || a instanceof Array && a.includes(t)
);
}
function vt(r, t, o) {
return !t || o === "all" || o === t || o instanceof Array && o.includes(t) ? !1 : ![...r].some(
({ onBlurOptOut: a }) => a === t || a instanceof Array && a.includes(t)
);
}
function Le(r, t) {
return [...r].reduce((o, { transformers: a }) => ({ ...o, ...a }), t);
}
function gt(r, t, o, a) {
return { ...[...r].reduce((i, { defaultValues: l }) => ({ ...i, ...l }), t), ...o, ...a };
}
function hr(r, t, o, a) {
let n = {};
a && (n = Object.fromEntries(
Ne(a).map((f) => [
G(f).name,
G(f).getAttribute("value")
])
));
const i = Array.from(r.changedFields), l = i.filter((f) => o[f] !== void 0 ? t[f] !== o[f] : n[f] != null ? t[f] !== n[f] : t[f] !== ""), u = Array.from(r.touchedFields);
return {
...r,
changedFields: i,
dirtyFields: l,
isChanged: i.length > 0,
isDirty: l.length > 0,
isPristine: l.length === 0,
isSubmitted: r.submitCount > 0,
isTouched: u.length > 0,
touchedFields: u
};
}
function Er(r, t) {
const o = t instanceof Array ? t : [t], a = r.changedFields.filter(
(l) => o.includes(l)
), n = r.dirtyFields.filter(
(l) => o.includes(l)
), i = r.touchedFields.filter(
(l) => o.includes(l)
);
return {
...r,
changedFields: a,
dirtyFields: n,
isChanged: a.length > 0,
isDirty: n.length > 0,
isPristine: n.length === 0,
isSubmitted: r.submitCount > 0,
isTouched: i.length > 0,
touchedFields: i
};
}
function Rt(r) {
const {
Component: t = "div",
errorPath: o = "main",
global: a,
...n
} = r, { errors: i } = de(ue), l = Gr(i, o);
if (!l)
return null;
let u;
if (typeof l == "string" && !a)
u = l;
else if ((Rr(l) || fr(l)) && (!a || l.global))
u = l.error;
else {
const f = Object.values(l)[0];
typeof f == "string" && !a ? u = f : fr(f) && (!a || f.global) && (u = f.error);
}
return u ? /* @__PURE__ */ we.jsx(t, { "data-testid": "rsf-error", ...n, children: u }) : null;
}
function bt(r = {}) {
const {
defaultValues: t,
errorCallback: o = console.error,
filterLocalErrors: a = !0,
focusOnError: n = !0,
form: i = null,
messages: l,
mode: u = "submit",
onBlurOptOut: f,
onChangeOptOut: h,
onReset: m,
onSubmit: E,
onSubmitError: C,
revalidateMode: p = "submit",
transformers: x,
useNativeValidation: D = !0,
validators: M
} = r, y = U(i), R = U(/* @__PURE__ */ new Set()), $ = U(t ?? {}), re = U({}), L = U({}), X = U(null), te = U({}), V = U({}), F = U({}), j = U({
...ze,
changedFields: /* @__PURE__ */ new Set(),
isReady: !1,
touchedFields: /* @__PURE__ */ new Set()
}), A = U(ee), [q, P] = Ae(ee), K = U(/* @__PURE__ */ new Set()), le = k((d) => (K.current.has(d) || K.current.add(d), () => K.current.delete(d)), []), Z = k(() => {
for (const d of K.current.values())
d(
hr(
j.current,
L.current,
$.current,
y.current
)
);
}, []), ye = U(
/* @__PURE__ */ new Map()
), je = k(
(d, b) => {
const v = b === void 0 || b instanceof Array ? b : [b];
return ye.current.set(d, v), () => ye.current.delete(d);
},
[]
), me = k(() => {
if (y.current) {
const d = Xe({
form: y.current,
transformers: Le(R.current, x),
values: L.current
});
for (const [b, v] of ye.current.entries()) {
const O = fe(
d,
([Y]) => !v || v.includes(Y)
), W = fe(
re.current,
([Y]) => !v || v.includes(Y)
);
b({
names: v,
prevValues: W,
values: O
});
}
re.current = { ...d };
}
}, [x]), J = k(
async (d = !1, b = !1, v = !1, O) => {
var Y;
if (!y.current)
return [!1, ee];
j.current.isValidating = !0, Z();
let W;
try {
const he = lt(R.current), Q = gr(
R.current,
M,
l
);
W = await it({
display: d,
errors: te.current,
filterLocalErrors: a,
focusOnError: v,
form: y.current,
localFields: he,
messages: l,
names: O instanceof Array ? O : O ? [O] : void 0,
revalidate: b,
setErrors: P,
transformers: Le(R.current, x),
useNativeValidation: D,
validators: Q,
values: L.current
});
} finally {
j.current.isValidating = !1, j.current.isValid = !!((Y = y.current) != null && Y.checkValidity()), Z();
}
return [j.current.isValid, W];
},
[
a,
l,
Z,
D,
x,
M
]
), Re = U(), H = k(
(d, b, v, O) => {
clearTimeout(Re.current), Re.current = setTimeout(
() => void J(d, b, v, O),
0
);
},
[J]
);
Ee(() => () => clearTimeout(Re.current), []);
const ne = k(
(d) => {
if (y.current) {
for (const b of Ne(y.current)) {
const v = G(b), { name: O } = v;
O && d[O] !== void 0 && d[O] !== null && (Cr(v) ? v.checked = !!d[O] : v.value = String(d[O]));
}
me();
}
},
[me]
), ke = k(
(d) => {
R.current.add(d), d.defaultValues && ($.current = {
...$.current,
...d.defaultValues
}, ne(d.defaultValues)), H();
},
[H, ne]
), De = k(
(d) => {
R.current.delete(d), H();
},
[H]
), ce = k(
(d) => {
P(ee);
const b = gr(
R.current,
M,
l
);
for (const v of b)
Qe(v) && v.setErrors(ee);
j.current = {
...ze,
changedFields: /* @__PURE__ */ new Set(),
isReady: j.current.isReady,
touchedFields: /* @__PURE__ */ new Set()
}, L.current = {}, $.current = gt(
R.current,
t,
d,
X.current
), X.current = null;
for (const v of Object.values(F.current))
v();
setTimeout(() => {
ne($.current), H();
});
},
[H, t, l, ne, M]
), oe = k((d) => {
var b;
X.current = d, (b = y.current) == null || b.reset();
}, []), ae = k(
(d, b) => {
const v = pr(d) ?? b;
if (!v)
throw new Error(
"react-swift-form was unable to retrieve the field name"
);
j.current.changedFields.add(v);
const O = Le(R.current, x), W = dt(d, O == null ? void 0 : O[v]);
return L.current[v] = W, me(), H(
u === "all" || u === "change",
p === "change",
!1,
v
), [v, W];
},
[H, u, p, x, me]
);
let ve = !1;
const se = k(
async (d, b, v) => {
if (!ve) {
d.preventDefault();
try {
j.current.isSubmitting = !0;
const [O, W] = await J(!0, !1, n);
O && y.current ? (y.current.getAttribute("action") !== null && (ve = !0, setTimeout(() => {
var Y;
(Y = y.current) == null || Y.requestSubmit(), ve = !1;
})), b && await b(
d,
Xe({
form: y.current,
transformers: Le(R.current, x),
values: L.current
}),
oe
)) : v && await v(d, W, oe);
} catch (O) {
o(O);
} finally {
j.current.isSubmitting = !1, j.current.submitCount++, Z();
}
}
},
[n, oe, x, J]
), Me = k(
(d) => {
const b = pr(d);
b && V.current[b] ? V.current[b] = !1 : mt(R.current, b, h) && ae(d);
},
[ae, h]
), Ce = k(
(d) => {
const b = m == null ? void 0 : m(d, L.current);
ce(b);
},
[m, ce]
), Ie = k(
(d) => se(d, E, C),
[E, C, se]
);
Ee(() => {
$.current = { ...$.current, ...t };
for (const d of Object.values(F.current))
d();
ne($.current);
}, [t, ne]), Ee(() => {
if (A.current !== q) {
A.current = q;
return;
}
J().then(() => {
y.current && (j.current.isReady = !0, Z(), y.current.dataset.rsf = "init");
});
}), Ee(() => {
if (y.current) {
const d = y.current, b = (v) => {
v.target && wr(v.target) && (j.current.touchedFields.add(v.target.name), (u === "all" || u === "blur" || p === "blur") && vt(R.current, v.target.name, f) ? J(
u === "all" || u === "blur",
p === "blur",
!1,
v.target.name
) : Z());
};
return d.addEventListener("focusout", b), () => d.removeEventListener("focusout", b);
}
}, [u, f, p, Z, J]);
const Se = k(
(d) => (b) => {
te.current[d] = b, H(
u === "all" || u === "change",
p === "change",
!1,
d
);
},
[H, u, p]
), ge = k(
(d, b = {}) => {
const { getError: v, name: O } = b;
if (O) {
const W = () => {
L.current[O] === void 0 && $.current[O] !== void 0 && (L.current[O] = $.current[O]);
};
F.current[O] = W, W();
}
return (W, ...Y) => {
const [he, Q] = ae(W, O);
V.current[he] = !0, v && Se(he)(v(Q, ...Y)), d(Q, ...Y);
};
},
[ae, Se]
), be = k(
(d) => (b) => {
const v = d == null ? void 0 : d(b, L.current);
ce(v);
},
[ce]
), Te = k(
(d, b) => async (v) => se(v, d, b),
[se]
), Fe = k(
(d, b) => je(({ prevValues: v, values: O }) => {
$e(O, v) || d(O);
}, b),
[je]
), pe = xe(
() => ({
noValidate: !D,
onChange: Me,
onReset: Ce,
onSubmit: Ie,
ref: y
}),
[Me, Ce, Ie, D]
);
return xe(
() => ({
errors: q,
form: y,
formProps: pe,
messages: l,
mode: u,
onChange: ge,
onError: Se,
onReset: be,
onSubmit: Te,
register: ke,
reset: oe,
revalidateMode: p,
states: hr(
j.current,
L.current,
$.current,
y.current
),
subscribe: le,
unregister: De,
useNativeValidation: D,
validate: J,
watch: Fe
}),
[
q,
pe,
l,
u,
ge,
Se,
be,
Te,
ke,
oe,
p,
le,
De,
D,
J,
Fe
]
);
}
function St() {
return de(ue);
}
function Ot() {
const { errors: r } = de(ue);
return r;
}
function Vr(r) {
const { subscribe: t } = de(ue);
Ee(() => t(r), [t]);
}
function _t(r) {
const { states: t } = de(ue), [o, a] = Ae(
r ? Er(t, r) : t
);
return Vr(
(n) => a(r ? Er(n, r) : n)
), o;
}
function pt() {
const { states: r } = de(ue), [t, o] = Ae(r.isValid);
return Vr(({ isValid: a }) => o(a)), t;
}
function ht(r) {
const {
defaultValues: t,
id: o,
messages: a,
names: n,
onBlurOptOut: i,
onChangeOptOut: l,
transformers: u,
validators: f
} = r, { onChange: h, onError: m, onReset: E, onSubmit: C, register: p, unregister: x, watch: D } = de(ue), [M, y] = Ae(ee);
return Ee(() => {
const R = {
defaultValues: t,
id: o ?? String(n),
messages: a,
names: n,
onBlurOptOut: i,
onChangeOptOut: l,
setErrors: y,
transformers: u,
validators: f
};
return p(R), () => x(R);
}, [
t,
o,
a,
n,
i,
l,
p,
u,
x,
f
]), {
error: M.main,
errors: M,
onChange: h,
onError: m,
onReset: E,
onSubmit: C,
watch: D
};
}
function wt(r) {
const {
defaultValue: t,
id: o,
messages: a,
name: n,
onBlurOptOut: i,
onChangeOptOut: l,
transformer: u,
validator: f
} = r, h = xe(() => [n], [n]), m = xe(
() => t !== void 0 ? { [n]: t } : void 0,
[t, n]
), E = xe(
() => u !== void 0 ? { [n]: u } : void 0,
[n, u]
), { errors: C, ...p } = ht({
defaultValues: m,
id: o ?? n,
messages: a,
names: h,
onBlurOptOut: i ? n : void 0,
onChangeOptOut: l ? n : void 0,
transformers: E,
validators: f
});
return { ...p, error: C.main, errors: C };
}
function jt(r) {
const [t, o] = Ae({}), { watch: a } = de(ue);
return Ee(() => a(o, r), [r, a]), t;
}
function Et(r) {
const { children: t, ...o } = r;
return /* @__PURE__ */ we.jsx(ue.Provider, { value: o, children: t });
}
function Ct(r) {
const {
children: t,
defaultValues: o,
filterLocalErrors: a,
messages: n,
mode: i,
onBlurOptOut: l,
onChangeOptOut: u,
onReset: f,
onSubmit: h,
onSubmitError: m,
revalidateMode: E,
transformers: C,
useNativeValidation: p = !0,
validators: x,
...D
} = r, { formProps: M, ...y } = bt({
defaultValues: o,
filterLocalErrors: a,
messages: n,
mode: i,
onBlurOptOut: l,
onChangeOptOut: u,
onReset: f,
onSubmit: h,
onSubmitError: m,
revalidateMode: E,
transformers: C,
useNativeValidation: p,
validators: x
});
return /* @__PURE__ */ we.jsx(Et, { ...y, children: /* @__PURE__ */ we.jsx("form", { "data-testid": "rsf-form", ...D, ...M, children: typeof t == "function" ? t(y) : t }) });
}
function Tt(r) {
const { ...t } = r;
return /* @__PURE__ */ we.jsx("input", { "data-testid": "rsf-reset", type: "reset", ...t });
}
function Ft(r) {
const { disableOnError: t, ...o } = r, a = pt();
return /* @__PURE__ */ we.jsx(
"input",
{
"data-testid": "rsf-submit",
disabled: t ? !a : !1,
type: "submit",
...o
}
);
}
export {
Rt as Error,
Ct as Form,
Et as FormProvider,
Tt as Reset,
Ft as Submit,
$e as areObjectEquals,
st as displayErrors,
fe as filterObject,
vr as focusError,
ue as formContext,
Sr as getAllError,
_r as getCustomMessage,
Xe as getData,
gt as getDefaultValues,
Or as getErrorObject,
Qr as getFieldMessages,
Er as getFieldStates,
Ke as getFilteredErrors,
G as getFormInput,
Ne as getFormInputs,
hr as getFormStates,
Fr as getInputValue,
lt as getLocalFields,
nt as getManualError,
pr as getName,
rt as getNativeError,
Zr as getNativeErrorKey,
tt as getNativeErrors,
Gr as getProperty,
Le as getTransformers,
ot as getValidatorError,
et as getValidatorIds,
gr as getValidators,
dt as getValue,
mr as hasError,
Ge as intersection,
Cr as isCheckbox,
jr as isEvent,
Tr as isField,
br as isFileInput,
wr as isFormElement,
Qe as isLocalValidator,
Rr as isMainError,
ct as isSelect,
qe as isValidator,
fr as isValidatorError,
Xr as isValidatorObject,
dr as mergeErrors,
We as setMainError,
at as setValidatorError,
vt as shouldBlur,
mt as shouldChange,
bt as useForm,
St as useFormContext,
Ot as useFormErrors,
_t as useFormStates,
pt as useFormValid,
wt as useInput,
ht as useInputs,
Vr as useSubscribe,
jt as useWatch,
it as validateForm
};
//# sourceMappingURL=index.js.map