UNPKG

koval-ui

Version:

React components collection with minimalistic design. Supports theming, layout, and input validation.

95 lines (94 loc) 2.54 kB
"use client"; import { useState as C, useCallback as n } from "react"; import w from "awesome-debounce-promise"; import { ValidationState as l } from "./ValidationProps.js"; import { defaultValidator as A } from "./defaultValidator.js"; import { useHandleFormReset as E } from "./useHandleFormReset.js"; import { getFormState as V } from "./getFormState.js"; const g = (t, e) => e === "interactive" ? t.target.checked : t.target.value, b = ({ validatorFn: t, reportValidity: e, setValidity: d }) => { const i = n( (c, r) => { const o = g(r, c), y = V(r.target.form), u = t == null ? void 0 : t( o, r.target.validity, y ); r.target.setCustomValidity(u), e(r); }, [t, e] ), m = n(() => { }, []), s = n(w(t, 1e3), [ t ]), f = n( async (c, r) => { r.target.setCustomValidity(""); const o = g(r, c), y = V(r.target.form); d(l.inProgress); let u = ""; try { u = await s( o, r.target.validity, y ); } catch (a) { r.target.setCustomValidity(a); } r.target.setCustomValidity(u), e(r); }, [d, s, e] ); return { createValidatorSync: i, createValidatorAsync: f, createValidatorExternal: m }; }, h = (t) => typeof t == "function" ? t : A, k = (t) => (t == null ? void 0 : t.constructor.name) === "AsyncFunction" ? "async" : typeof t == "string" ? "external" : "sync", T = ({ validation: t, hasValidators: e }) => { const d = h(t), i = k(t), [m, s] = C( l.pristine ); E(s); const f = n( (a) => { const p = a.target.reportValidity(), x = e ? l.valid : l.pristine, S = p ? x : l.error; (e || m === l.error) && s(S); }, [e, m] ), { createValidatorAsync: c, createValidatorSync: r, createValidatorExternal: o } = b({ validatorFn: d, reportValidity: f, setValidity: s }), y = n( (a) => { switch (i) { case "sync": return r("interactive", a); case "async": return c("interactive", a); case "external": return o(); } }, [c, o, r, i] ), u = n( (a) => { switch (i) { case "sync": return r("textual", a); case "async": return c("textual", a); case "external": return o(); } }, [i, r, c, o] ); return { validateInteractive: y, validateTextual: u, validity: m, setValidity: s }; }; export { T as useValidation }; //# sourceMappingURL=useValidation.js.map