@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
343 lines (342 loc) • 10.5 kB
JavaScript
import { FieldPresentation as e, useFieldPresentation as t } from "../../fields/FieldPresentation/FieldPresentation.js";
import { formatErrorText as n, getFormRuntime as r, isInactive as i, toFormApi as a } from "../../../runtime/formRuntime.js";
import { pickErrors as o } from "../../../runtime/errors.js";
import { focusTarget as s } from "../../../runtime/focus.js";
import { useIsomorphicLayoutEffect as c } from "../../../utils/env.js";
import { bindFields as l } from "../../../kit/bindFields.js";
import { FieldViewListBoundary as u } from "../../fields/FieldView/FieldView.js";
import { scopeChain as d, useScopeNode as f } from "../FieldScope/FieldScope.js";
import { areErrorsVisible as p, isQuietMeta as m } from "../../../runtime/reveal.js";
import { useAnnouncer as ee } from "../internal/announcer.js";
import { readLegend as te } from "../../../runtime/labels.js";
import { warnOnce as ne } from "../../../schema/render/warn.js";
import { Children as h, Fragment as g, isValidElement as _, useCallback as re, useEffect as ie, useId as v, useReducer as ae, useRef as y } from "react";
import { getBy as b, useSelector as x } from "@tanstack/react-form";
import { jsx as S, jsxs as C } from "react/jsx-runtime";
import { Button as oe, Card as w, ChevronDownIcon as se, ChevronUpIcon as ce, CloseIcon as le, Fieldset as T, Grid as E, IconButton as D, Inline as O, PlusIcon as ue, Stack as k, Table as A, Text as de, VisuallyHidden as fe } from "@mitcsutt/kiln-ui";
//#region src/components/layouts/Repeater/Repeater.tsx
function j() {
return /* @__PURE__ */ S(D, {
label: "",
icon: null,
size: "sm",
disabled: !0,
"aria-hidden": "true",
tabIndex: -1
});
}
function M(e) {
let t = [];
for (let n of h.toArray(e)) _(n) && n.type === g ? t.push(...M(n.props.children)) : t.push(n);
return t;
}
function N(e, t) {
let n = r(e).registry;
if (!n) throw Error("[@mitcsutt/kiln-forms] Repeater item.fields needs a kit form (from useAppForm).");
return l(e, n, t);
}
function P({ props: t, count: n, messages: r, itemOf: i, labelOf: a, actionsOf: o, setItemElement: s, dragAndDrop: c }) {
let { variant: l = "list", empty: u, columns: d, children: f } = t, p = Array.from({ length: n }, (e, t) => t);
if (n === 0) return u ?? null;
let m = c && s ? (e, t) => /* @__PURE__ */ S(c.Item, {
id: String(e),
index: e,
label: a(e),
setItemElement: s,
children: ({ ref: e, props: n }) => t(e, n)
}) : (e, t) => t(s?.(e), void 0);
if (l === "table") return /* @__PURE__ */ S(e, {
labelHidden: !0,
layout: "inline",
children: /* @__PURE__ */ C(A, { children: [/* @__PURE__ */ S(A.Head, { children: /* @__PURE__ */ C(A.Row, { children: [(d ?? []).map((e, t) => /* @__PURE__ */ S(A.HeaderCell, {
width: e.width,
children: e.header
}, t)), o ? /* @__PURE__ */ S(A.HeaderCell, {
align: "end",
width: "min",
children: /* @__PURE__ */ S(fe, { children: r.actions })
}) : null] }) }), /* @__PURE__ */ S(A.Body, { children: p.map((e) => {
let t = i(e);
return /* @__PURE__ */ S(g, { children: m(e, (n, r) => /* @__PURE__ */ C(A.Row, {
ref: n,
...r,
"aria-label": a(e),
children: [M(f(t)).map((e, t) => /* @__PURE__ */ S(A.Cell, { children: e }, t)), o ? /* @__PURE__ */ S(A.Cell, {
align: "end",
children: o(t)
}) : null]
})) }, t.key);
}) })] })
});
if (l === "cards") {
let e = p.map((e) => {
let t = i(e);
return /* @__PURE__ */ S(g, { children: m(e, (n, r) => /* @__PURE__ */ S(w, {
ref: n,
...r,
variant: "outline",
children: /* @__PURE__ */ S(T, {
variant: "section",
legend: a(e),
children: /* @__PURE__ */ C(k, {
gap: 4,
children: [f(t), o?.(t)]
})
})
})) }, t.key);
});
return c ? /* @__PURE__ */ S(k, {
gap: 4,
children: e
}) : /* @__PURE__ */ S(E, {
columns: {
base: 1,
md: 2
},
gap: 4,
children: e
});
}
return /* @__PURE__ */ S(k, {
gap: 5,
dividers: !0,
children: p.map((e) => {
let t = i(e);
return /* @__PURE__ */ S(g, { children: m(e, (n, r) => /* @__PURE__ */ S(T, {
ref: n,
...r,
legend: a(e),
children: /* @__PURE__ */ C(k, {
gap: 4,
children: [f(t), o?.(t)]
})
})) }, t.key);
})
});
}
function F({ field: e, props: t }) {
let { form: l, name: u, label: h, description: g, newItem: _, min: b = 0, max: w = Infinity, reorderable: E = !1, itemLabel: A, addLabel: fe } = t, M = a(l), F = M, I = r(M), L = I.options.messages, R = E === "drag" ? I.dragAndDrop : null, z = E === "drag" && !R;
ie(() => {
z && ne("Repeater reorderable=\"drag\" needs the kit's dragAndDrop from @mitcsutt/kiln-forms/dnd; it shows the move buttons alone.");
}, [z]);
let B = A ?? L.item, V = (e.state.value ?? []).length, H = V >= w, [pe, U] = ee(), W = v(), G = v(), K = x(M.store, (e) => e.submissionAttempts > 0), q = e.state.meta, J = o(q.errorMap)[0], me = p(I, {
...q,
isBlurred: q.isBlurred || q.isTouched
}, K), he = J && me && !i(I, u) ? n(I, J) : void 0, Y = y(null), ge = f();
c(() => {
let e = d(ge), t = e.map((e) => e.register(u)), n = {
id: W,
element: () => Y.current,
scopes: e,
focus: () => {
let e = Y.current;
e && s(e)?.focus();
},
getLabel: () => te(Y.current, u)
};
return I.fields.set(u, n), () => {
for (let e of t) e();
I.fields.get(u) === n && I.fields.delete(u);
};
}, [
I,
u,
W,
ge
]);
let _e = y(_);
c(() => {
_e.current = _;
}), c(() => {
let e = () => {
let e = _e.current;
return typeof e == "function" ? e() : e;
};
return I.itemTemplates.set(u, e), () => {
I.itemTemplates.get(u) === e && I.itemTemplates.delete(u);
};
}, [I, u]);
let X = y(/* @__PURE__ */ new Map()), ve = y(null), Z = y(null);
c(() => {
let e = Z.current;
Z.current = null, e?.();
});
let ye = re((e) => (t) => {
t && X.current.set(e, t);
}, []), Q = (e) => {
let t = X.current.get(e), n = t?.isConnected ? s(t) : null;
return n?.focus(), !!n;
}, be = () => {
if (H) return;
let e = typeof _ == "function" ? _() : _, t = V;
Z.current = () => {
Q(t);
}, F.pushFieldValue(u, e), U(L.itemAdded(B(t)));
}, xe = (e) => {
let t = B(e);
Z.current = () => {
Q(e) || e > 0 && Q(e - 1) || ve.current?.focus();
}, F.removeFieldValue(u, e), U(L.itemRemoved(t));
}, [, Se] = ae((e) => e + 1, 0), Ce = (e, t, n) => {
t < 0 || t >= V || t === e || (n === "keyboard" && (Z.current = () => {
X.current.get(t)?.querySelector("[data-repeater-action=\"drag\"]")?.focus();
}), F.moveFieldValues(u, e, t), Se());
}, $ = (e, t, n) => {
if (t < 0 || t >= V || t === e) return;
let r = B(e);
Z.current = () => {
let e = X.current.get(t), r = e?.querySelector(`[data-repeater-action="${n}"]`), i = e?.querySelector(`[data-repeater-action="${n === "up" ? "down" : "up"}"]`);
(r ?? i)?.focus();
}, F.moveFieldValues(u, e, t), U(L.itemMoved(r, t + 1));
}, we = P({
props: t,
count: V,
messages: L,
itemOf: (e) => ({
index: e,
count: V,
key: String(e),
name: `${u}[${String(e)}]`,
fields: N(l, `${u}[${String(e)}].`),
remove: () => {
xe(e);
},
move: (t) => {
$(e, t, t < e ? "up" : "down");
},
canRemove: V > b,
canMoveUp: E !== !1 && e > 0,
canMoveDown: E !== !1 && e < V - 1
}),
labelOf: B,
actionsOf: (e) => {
let t = B(e.index), n = E !== !1;
return /* @__PURE__ */ C(O, {
gap: 1,
justify: "end",
wrap: !1,
children: [
R ? V > 1 ? /* @__PURE__ */ S(R.Handle, { "data-repeater-action": "drag" }) : /* @__PURE__ */ S(j, {}) : null,
e.canMoveUp ? /* @__PURE__ */ S(D, {
label: L.moveUp(t),
icon: /* @__PURE__ */ S(ce, {}),
size: "sm",
"data-repeater-action": "up",
onClick: () => {
$(e.index, e.index - 1, "up");
}
}) : n ? /* @__PURE__ */ S(j, {}) : null,
e.canMoveDown ? /* @__PURE__ */ S(D, {
label: L.moveDown(t),
icon: /* @__PURE__ */ S(se, {}),
size: "sm",
"data-repeater-action": "down",
onClick: () => {
$(e.index, e.index + 1, "down");
}
}) : n ? /* @__PURE__ */ S(j, {}) : null,
e.canRemove ? /* @__PURE__ */ S(D, {
label: L.remove(t),
icon: /* @__PURE__ */ S(le, {}),
size: "sm",
"data-repeater-action": "remove",
onClick: () => {
xe(e.index);
}
}) : /* @__PURE__ */ S(j, {})
]
});
},
setItemElement: ye,
dragAndDrop: R
}), Te = R ? /* @__PURE__ */ S(R.List, {
label: typeof h == "string" ? h : u,
messages: L.drag,
onMove: Ce,
children: we
}) : we;
return /* @__PURE__ */ S(T, {
ref: Y,
id: W,
legend: h,
description: g,
error: he,
errorLive: !K && !m(q),
children: /* @__PURE__ */ C(k, {
gap: 4,
children: [
Te,
/* @__PURE__ */ C(O, {
gap: 3,
align: "center",
children: [/* @__PURE__ */ S(oe, {
ref: ve,
variant: "outline",
tone: "neutral",
leadingIcon: /* @__PURE__ */ S(ue, {}),
"aria-disabled": H || void 0,
"aria-describedby": H ? G : void 0,
onClick: be,
children: fe ?? L.add
}), H ? /* @__PURE__ */ S(de, {
id: G,
size: "sm",
tone: "muted",
children: L.maxItems(w)
}) : null]
}),
pe
]
})
});
}
function I({ props: e }) {
let { form: t, name: n, label: i, description: o, itemLabel: s } = e, c = a(t), l = r(c).options.messages, u = s ?? l.item, d = x(c.store, (e) => {
let t = b(e.values, n);
return Array.isArray(t) ? t.length : 0;
}), f = () => void 0;
return /* @__PURE__ */ S(T, {
legend: i,
description: o,
children: P({
props: e,
count: d,
messages: l,
itemOf: (e) => ({
index: e,
count: d,
key: String(e),
name: `${n}[${String(e)}]`,
fields: N(t, `${n}[${String(e)}].`),
remove: f,
move: f,
canRemove: !1,
canMoveUp: !1,
canMoveDown: !1
}),
labelOf: u
})
});
}
function L({ props: e }) {
let t = e.form.Field;
if (!t) throw Error("[@mitcsutt/kiln-forms] Repeater needs a React form (from useAppForm / useForm).");
let n = {
name: e.name,
mode: "array"
};
return e.validators !== void 0 && (n.validators = e.validators), /* @__PURE__ */ S(t, {
...n,
children: (t) => /* @__PURE__ */ S(F, {
field: t,
props: e
})
});
}
function R(e) {
let n = e, r = t().mode;
return /* @__PURE__ */ S(u, { children: S(r === "view" ? I : L, { props: n }) });
}
//#endregion
export { R as Repeater };
//# sourceMappingURL=Repeater.js.map