@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
105 lines (104 loc) • 2.8 kB
JavaScript
import { useIsomorphicLayoutEffect as e } from "../../../utils/env.js";
import { FieldViewListBoundary as t } from "../../fields/FieldView/FieldView.js";
import { FieldScope as n, useFieldScope as r } from "../FieldScope/FieldScope.js";
import { ErrorBadge as i } from "../internal/ErrorBadge.js";
import { useOrderedRegistry as a } from "../internal/registry.js";
import { createContext as o, useCallback as s, useContext as c, useMemo as l, useRef as u, useState as d } from "react";
import { jsx as f, jsxs as p } from "react/jsx-runtime";
import { Inline as m, Stack as h, Tabs as g } from "@mitcsutt/kiln-ui";
import { flushSync as _ } from "react-dom";
//#region src/components/layouts/FormTabs/FormTabs.tsx
var v = o(null);
function y() {
let e = c(v);
if (!e) throw Error("[@mitcsutt/kiln-forms] <FormTabs.Tab> must be rendered inside <FormTabs>.");
return e;
}
function b({ entry: e }) {
return /* @__PURE__ */ f(g.Trigger, {
value: e.value,
children: /* @__PURE__ */ p(m, {
as: "span",
gap: 2,
align: "center",
wrap: !1,
children: [/* @__PURE__ */ f("span", { children: e.label }), /* @__PURE__ */ f(i, { scope: e.scope })]
})
});
}
function x({ label: t, value: n, defaultValue: r, onValueChange: i, variant: o, children: c }) {
let [m, h] = a(), [_, y] = d(r), x = n !== void 0, S = _ !== void 0 && m.some((e) => e.value === _), C = x ? n : S ? _ : m[0]?.value ?? _, w = u(i);
e(() => {
w.current = i;
});
let T = s((e) => {
x || y(e), w.current?.(e);
}, [x]), E = l(() => ({
current: C,
select: T,
register: h
}), [
C,
T,
h
]);
return /* @__PURE__ */ f(v.Provider, {
value: E,
children: /* @__PURE__ */ p(g, {
value: C ?? "",
onValueChange: T,
variant: o,
children: [/* @__PURE__ */ f(g.List, {
"aria-label": t,
children: m.map((e) => /* @__PURE__ */ f(b, { entry: e }, e.key))
}), c]
})
});
}
function S({ value: t, label: n, children: i }) {
let { current: a, register: o } = y(), s = r(), c = u(null);
return e(() => o({
key: t,
value: t,
label: n,
scope: s,
element: () => c.current
}), [
o,
t,
n,
s
]), /* @__PURE__ */ f(g.Content, {
ref: c,
value: t,
forceMount: !0,
hidden: a !== t,
children: /* @__PURE__ */ f(h, {
gap: 5,
children: i
})
});
}
function C({ value: e, label: t, scopeNames: r, children: i }) {
let { select: a } = y(), o = s(() => {
_(() => {
a(e);
});
}, [a, e]);
return /* @__PURE__ */ f(n, {
reveal: o,
names: r,
children: /* @__PURE__ */ f(S, {
value: e,
label: t,
children: i
})
});
}
function w(e) {
return /* @__PURE__ */ f(t, { children: /* @__PURE__ */ f(x, { ...e }) });
}
var T = Object.assign(w, { Tab: C });
//#endregion
export { C as FormTab, T as FormTabs };
//# sourceMappingURL=FormTabs.js.map