UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

140 lines (139 loc) 3.85 kB
var j = Object.defineProperty; var I = (r, t, a) => t in r ? j(r, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : r[t] = a; var o = (r, t, a) => I(r, typeof t != "symbol" ? t + "" : t, a); import { jsx as S } from "react/jsx-runtime"; import { useState as O, createContext as D, useContext as H, useImperativeHandle as M } from "react"; import { u as w } from "./useUpdateEffect-4dQLu1cU.js"; import { d as U, a as q, g as z } from "./object-CQtn__eR.js"; import { u as A } from "./service-D3PLMJZC.js"; const B = (r) => { const { validation: { schema: t, delay: a = 100, initOnMount: d }, initValues: u } = r, [e, i] = O({ values: u, error: {}, touchedFields: [], ok: !1, isValid: !1, isSubmitting: !1 }), { touchedFields: n, isValid: f, isSubmitting: m } = e, v = U(u), { culture: E } = A(), g = new x(); let V = 0; function P(s) { return { "field-name": s }; } function N(s) { return s["field-name"]; } const R = async (s, F) => (clearTimeout(V), new Promise((l) => { V = setTimeout(async () => { n.includes(s) || (e.touchedFields = [...n, s]); try { e.values = { ...e.values, [s]: F }; const c = await t.validate(e.values); c.ok ? (e.isValid = !0, e.error = {}, e.values = c.data, e.ok || (e.ok = !0, i({ ...e }))) : (e.isValid && (e.isValid = !1, e.ok && (e.ok = !1, i({ ...e }))), e.error = c.validation.getFieldError(n)); } catch { e.error = {}; } l({ name: s, error: z(e.error, s) }); }, a); })), T = async (s) => { e.isSubmitting = !0, i({ ...e }), await s(e.values, { reset: p, clear: y }), e.isSubmitting = !1, i({ ...e }); }, p = () => { e.values = { ...v }, b(), g.resetNotify(); }, y = () => { e.values = { ...q(v) }, b(), g.clearNotify(); }, b = () => { e.ok = !1, e.touchedFields = [], e.error = {}, d && h(), i({ ...e }); }, h = (s) => { const F = setTimeout(async () => { const l = await t.validate(e.values); l.ok ? (e.isValid = !0, e.error = {}, e.values = l.data) : (e.isValid = !1, e.error = l.validation.getFieldError()), i({ ...e }), clearTimeout(F); }, s ?? a); return e; }; return w(() => { d && h(); }, []), w(() => { n.length > 0 && h(0); }, [E]), { validateField: R, initValues: v, values: e.values, error: e.error, isValid: f, isSubmitting: m, event: g, submit: T, reset: p, clear: y, registerField: P, getRegisteredField: N }; }; class x { constructor() { o(this, "clears", /* @__PURE__ */ new Set()); o(this, "resets", /* @__PURE__ */ new Set()); o(this, "onClear", (t) => { this.clears.add(t); }); o(this, "clearNotify", () => { this.clears.forEach((t) => { t(); }), console.log(this.clears); }); o(this, "onReset", (t) => (this.resets.add(t), () => this.clears.delete(t))); o(this, "resetNotify", () => { this.resets.forEach((t) => { t(); }); }); } } const C = { registerField: () => ({ "field-name": "" }), validateField: () => new Promise(() => { }), getRegisteredField: () => "", clear: () => { }, reset: () => { }, error: {}, initValues: {}, values: {}, isValid: !1, isSubmitting: !1, event: new x() }, k = D(C); function X() { return H(k) ?? C; } function Y(r) { const { children: t, innerRef: a, initValues: d, onSubmit: u, ...e } = r, i = B({ ...r }); M(a, () => ({ ...i })); const n = async (m) => { m.preventDefault(), await i.submit(u); }, f = { ...i }; return /* @__PURE__ */ S(k.Provider, { value: f, children: /* @__PURE__ */ S("form", { autoComplete: "off", ...e, onSubmit: n, children: t }) }); } export { x as F, Y as a, B as b, X as u };