UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

62 lines (61 loc) 2.16 kB
import { jsxs as V, jsx as n } from "react/jsx-runtime"; import { u as I } from "./form-CzgKUHW5.js"; import { useId as P, useState as b, useRef as S, useEffect as U } from "react"; import { u as E } from "./useUpdateEffect-4dQLu1cU.js"; const D = (u) => { var F; const i = P(), { label: d, icon: m, disabled: o, clearable: N, onChange: f, onFocus: h, ...T } = u, { validateField: R, getRegisteredField: j, error: l, event: v, initValues: g } = I(), r = j(u), [p, x] = b(l[r]), [k, s] = b(""), e = S(null), y = (t) => { var a = t.target.value; B(a), c(a), f && f(t); }, M = (t) => { c(t.target.value), h && h(t); }, c = (t) => { r && R(r, t).then((a) => x(a.error)); }, C = () => { o || (e.current && (e.current.value = ""), e.current && (e.current.defaultValue = ""), s(""), c("")); }, B = (t) => { t.length === 1 && s("x"), t.length === 0 && s(""); }; return U(() => { e.current && (e.current.defaultValue.length > 1 ? s("x") : s("")); }, [(F = e.current) == null ? void 0 : F.defaultValue]), E(() => { r && x(l[r]); }, [l]), E(() => { r && (v.onClear(() => C()), v.onReset(() => { e.current && (e.current.value = g[r]); })); }, []), /* @__PURE__ */ V("div", { className: "control", children: [ d && /* @__PURE__ */ n("label", { htmlFor: i, children: d }), /* @__PURE__ */ n("div", { className: "content", children: /* @__PURE__ */ V("div", { className: "input", children: [ m && /* @__PURE__ */ n("div", { className: "icon", children: m }), /* @__PURE__ */ n( "input", { autoComplete: "off", defaultValue: g[r], ...T, ref: e, onChange: y, onFocus: M, disabled: o, type: "text", id: i } ), N && k && /* @__PURE__ */ n( "button", { className: "clear", type: "button", onClick: C, disabled: o, children: /* @__PURE__ */ n("i", { children: "×" }) } ) ] }) }), p && /* @__PURE__ */ n("span", { className: "error", children: p }) ] }); }; export { D as T };