@skiddph/prui 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,731 +0,0 @@
1
- import { jsx as e, jsxs as u, Fragment as Ne } from "react/jsx-runtime";
2
- import * as d from "react";
3
- import { Routes as we, Route as X, Navigate as Se, MemoryRouter as ke, BrowserRouter as Ce, useLocation as Z, useNavigate as Ee, Outlet as Pe, Link as Te, NavLink as $e } from "react-router-dom";
4
- import { X as je, Menu as Re, Search as De, ChevronDown as Me, Pencil as Oe, Trash2 as Fe, Plus as Le } from "lucide-react";
5
- import { cn as C } from "../core/cn.js";
6
- import { Dropdown as ze } from "../core/dropdown.js";
7
- import { Button as D } from "../core/button.js";
8
- import { readPersistedTheme as Ae, applyTheme as G, PRUI_THEMES as Ie } from "../theme.js";
9
- import { Modal as qe, confirmModal as Be } from "../core/modal.js";
10
- import { b as Ke, D as Ue, a as _e } from "./data-table-toolbar-C9likJBN.js";
11
- import { Textarea as Je, Input as Ve } from "../core/input.js";
12
- import { Label as He } from "../core/label.js";
13
- import { Select as Xe } from "../core/select.js";
14
- import { Switch as Ge } from "../core/switch.js";
15
- import { Card as ee, CardHeader as te, CardDescription as re, CardTitle as ae, CardContent as le } from "../core/card.js";
16
- const Qe = [
17
- { path: "/login", name: "login" },
18
- { path: "/register", name: "register" },
19
- { path: "/forgot-password", name: "forgotPassword" },
20
- { path: "/reset-password", name: "resetPassword" },
21
- { path: "/otp", name: "otp" },
22
- { path: "/logout", name: "logout" }
23
- ], We = [
24
- { path: "/404", name: "notFound" },
25
- { path: "/error", name: "error" },
26
- { path: "/profile", name: "profile" },
27
- { path: "/settings", name: "settings" },
28
- { path: "/admin-setup", name: "adminSetup" }
29
- ];
30
- function Ye({ mode: t, config: l, children: a }) {
31
- const r = t === "auth" || t === "auth+utility", i = t === "utility" || t === "auth+utility", b = [
32
- ...r ? Qe : [],
33
- ...i ? We : []
34
- ], [x, m] = d.useState(null);
35
- return d.useEffect(() => {
36
- let f = !1;
37
- return import("../pages.js").then((g) => {
38
- f || m(g.PagesRegistry);
39
- }), () => {
40
- f = !0;
41
- };
42
- }, []), x ? /* @__PURE__ */ u(we, { children: [
43
- b.map(({ path: f, name: g }) => {
44
- const y = x[g];
45
- return y ? /* @__PURE__ */ e(X, { path: f, element: /* @__PURE__ */ e(y, { ...(l == null ? void 0 : l[g]) ?? {} }) }, f) : null;
46
- }),
47
- a,
48
- r ? /* @__PURE__ */ e(X, { path: "*", element: /* @__PURE__ */ e(Se, { to: "/login", replace: !0 }) }) : null
49
- ] }) : /* @__PURE__ */ e("div", { className: "p-8 text-center text-sm text-[var(--prui-dim)]", children: "Loading…" });
50
- }
51
- function Ze(t) {
52
- const l = t !== !1, a = typeof t == "object" ? t : {}, r = a.persist !== !1, [i, b] = d.useState(() => (r ? Ae() : null) ?? { theme: a.default ?? "control", mode: "dark" }), x = d.useCallback(
53
- (m) => {
54
- b(m), G({ theme: m.theme, mode: m.mode, storageKey: r ? "prui:theme" : null });
55
- },
56
- [r]
57
- );
58
- return d.useEffect(() => {
59
- G({ theme: i.theme, mode: i.mode, storageKey: r ? "prui:theme" : null });
60
- }, [i, r]), { enabled: l, state: i, setTheme: x };
61
- }
62
- function et({ config: t }) {
63
- const { enabled: l, state: a, setTheme: r } = Ze(t);
64
- return l ? /* @__PURE__ */ e(
65
- ze,
66
- {
67
- align: "end",
68
- trigger: /* @__PURE__ */ e(
69
- "button",
70
- {
71
- type: "button",
72
- "aria-label": "Switch theme",
73
- "data-testid": "theme-toggle",
74
- className: "inline-flex h-8 w-8 items-center justify-center rounded-[var(--prui-radius)] text-[var(--prui-dim)] hover:bg-[var(--prui-raise)] hover:text-[var(--prui-fg)] cursor-pointer",
75
- children: /* @__PURE__ */ e("span", { className: "text-xs font-semibold uppercase", children: a.theme.slice(0, 2) })
76
- }
77
- ),
78
- items: Ie.map((i) => ({
79
- label: `${i}${i === a.theme ? " (active)" : ""}`,
80
- onSelect: () => r({ theme: i, mode: i === "daylight" ? "light" : "dark" })
81
- }))
82
- }
83
- ) : null;
84
- }
85
- function Q(t, l) {
86
- return l === "/" ? t === "/" : t === l || t.startsWith(l + "/");
87
- }
88
- function tt({ item: t, onNavigate: l }) {
89
- const { pathname: a } = Z(), r = (t.items ?? []).some(
90
- (m) => m.href ? Q(a, m.href) : (m.items ?? []).some((f) => f.href && Q(a, f.href))
91
- ), [i, b] = d.useState(r);
92
- d.useEffect(() => {
93
- r && b(!0);
94
- }, [r]);
95
- const x = t.icon;
96
- return /* @__PURE__ */ u("li", { "data-testid": "nav-group", children: [
97
- /* @__PURE__ */ u(
98
- "button",
99
- {
100
- type: "button",
101
- "aria-expanded": i,
102
- onClick: () => b((m) => !m),
103
- className: C(
104
- "flex w-full items-center gap-2 rounded-[var(--prui-radius)] px-2.5 py-1.5 text-sm cursor-pointer",
105
- r ? "text-[var(--prui-fg)]" : "text-[var(--prui-dim)] hover:text-[var(--prui-fg)]"
106
- ),
107
- children: [
108
- x ? /* @__PURE__ */ e(x, { className: "h-4 w-4 shrink-0", "aria-hidden": !0 }) : null,
109
- /* @__PURE__ */ e("span", { className: "flex-1 text-left truncate", children: t.label }),
110
- /* @__PURE__ */ e(Me, { className: C("h-4 w-4 transition-transform", !i && "-rotate-90"), "aria-hidden": !0 })
111
- ]
112
- }
113
- ),
114
- i ? /* @__PURE__ */ e("ul", { className: "ml-4 mt-0.5 flex flex-col gap-0.5 border-l border-[var(--prui-line)] pl-2", children: (t.items ?? []).map((m) => /* @__PURE__ */ e(ne, { item: m, onNavigate: l, nested: !0 }, m.href ?? m.label)) }) : null
115
- ] });
116
- }
117
- function rt({ label: t }) {
118
- return /* @__PURE__ */ e("li", { className: "px-2.5 pb-1 pt-3 text-[10px] font-semibold uppercase tracking-[0.1em] text-[var(--prui-dim)]", "data-testid": "nav-subgroup", children: t });
119
- }
120
- function ne({ item: t, onNavigate: l, nested: a }) {
121
- return t.items && t.items.length > 0 ? /* @__PURE__ */ e(tt, { item: t, onNavigate: l }) : t.heading || !t.href && !t.items ? /* @__PURE__ */ e(rt, { label: t.label }) : t.href ? /* @__PURE__ */ e("li", { children: /* @__PURE__ */ u(
122
- $e,
123
- {
124
- to: t.href,
125
- onClick: l,
126
- className: ({ isActive: r }) => C(
127
- "flex items-center gap-2 rounded-[var(--prui-radius)] px-2.5 py-1.5 text-sm",
128
- a && "pl-2.5",
129
- r ? "bg-[var(--prui-brand)]/15 text-[var(--prui-brand)] font-medium" : "text-[var(--prui-dim)] hover:text-[var(--prui-fg)] hover:bg-[var(--prui-raise)]"
130
- ),
131
- children: [
132
- t.icon && !a ? /* @__PURE__ */ e(t.icon, { className: "h-4 w-4 shrink-0", "aria-hidden": !0 }) : null,
133
- /* @__PURE__ */ e("span", { className: "truncate", children: t.label })
134
- ]
135
- }
136
- ) }) : null;
137
- }
138
- function W({ nav: t, onNavigate: l }) {
139
- return /* @__PURE__ */ e("nav", { "aria-label": "Main", className: "prui-app-nav", children: /* @__PURE__ */ e("ul", { className: "flex flex-col gap-0.5", children: t.map((a) => /* @__PURE__ */ e(ne, { item: a, onNavigate: l }, a.href ?? a.label)) }) });
140
- }
141
- function at({ brand: t }) {
142
- if (!t) return null;
143
- const l = /* @__PURE__ */ u(Ne, { children: [
144
- t.mark ? /* @__PURE__ */ e("img", { src: t.mark, alt: "", className: "h-6 w-6 rounded-[var(--prui-radius-1)] object-cover" }) : null,
145
- /* @__PURE__ */ e("span", { className: "truncate font-semibold text-[var(--prui-fg)]", children: t.name })
146
- ] }), a = "flex items-center gap-2 px-1 py-1 min-w-0";
147
- return t.href === null ? /* @__PURE__ */ e("div", { className: a, children: l }) : /* @__PURE__ */ e(Te, { to: t.href ?? "/", className: a, "aria-label": t.name, children: l });
148
- }
149
- function lt({
150
- open: t,
151
- onOpenChange: l,
152
- entries: a,
153
- placeholder: r = "Type a command or search...",
154
- onNavigate: i
155
- }) {
156
- const [b, x] = d.useState(""), [m, f] = d.useState(0), g = d.useMemo(() => {
157
- const c = b.trim().toLowerCase();
158
- return c ? a.filter((h) => h.label.toLowerCase().includes(c)) : a;
159
- }, [a, b]);
160
- if (d.useEffect(() => f(0), [b]), !t) return null;
161
- const y = (c) => {
162
- l(!1), c.onSelect ? c.onSelect() : c.href && (i == null || i(c.href));
163
- };
164
- return /* @__PURE__ */ e(
165
- "div",
166
- {
167
- className: "fixed inset-0 z-50 flex items-start justify-center pt-24",
168
- style: { backgroundColor: "rgba(0,0,0,0.55)" },
169
- onMouseDown: (c) => {
170
- c.target === c.currentTarget && l(!1);
171
- },
172
- children: /* @__PURE__ */ u(
173
- "div",
174
- {
175
- role: "dialog",
176
- "aria-modal": "true",
177
- "aria-label": "Command palette",
178
- "data-testid": "command-palette",
179
- className: "w-full max-w-md rounded-[var(--prui-radius)] border border-[var(--prui-line)] bg-[var(--prui-surface)] shadow-xl",
180
- children: [
181
- /* @__PURE__ */ e(
182
- "input",
183
- {
184
- autoFocus: !0,
185
- type: "text",
186
- role: "combobox",
187
- "aria-expanded": "true",
188
- "aria-label": "Command palette search",
189
- "aria-activedescendant": g[m] ? `palette-option-${m}` : void 0,
190
- placeholder: r,
191
- value: b,
192
- onChange: (c) => x(c.target.value),
193
- onKeyDown: (c) => {
194
- if (c.key === "Escape" && l(!1), c.key === "ArrowDown" && (c.preventDefault(), f((h) => Math.min(h + 1, g.length - 1))), c.key === "ArrowUp" && (c.preventDefault(), f((h) => Math.max(h - 1, 0))), c.key === "Enter") {
195
- const h = g[m];
196
- h && (l(!1), h.onSelect ? h.onSelect() : h.href && (i == null || i(h.href)));
197
- }
198
- },
199
- className: "w-full bg-transparent px-4 py-3 text-sm text-[var(--prui-fg)] placeholder:text-[var(--prui-dim)] outline-none border-b border-[var(--prui-line)] rounded-t-[var(--prui-radius)]"
200
- }
201
- ),
202
- /* @__PURE__ */ e("ul", { role: "listbox", className: "max-h-64 overflow-auto p-1", children: g.length === 0 ? /* @__PURE__ */ e("li", { className: "px-3 py-2 text-sm text-[var(--prui-dim)]", children: "No matches." }) : g.map((c, h) => /* @__PURE__ */ u(
203
- "li",
204
- {
205
- id: `palette-option-${h}`,
206
- role: "option",
207
- "aria-selected": h === m,
208
- onMouseEnter: () => f(h),
209
- onClick: () => y(c),
210
- className: C(
211
- "cursor-pointer rounded-[calc(var(--prui-radius)-1px)] px-3 py-2 text-sm text-[var(--prui-fg)]",
212
- h === m && "bg-[var(--prui-raise)]"
213
- ),
214
- children: [
215
- c.group ? /* @__PURE__ */ e("span", { className: "mr-2 text-xs text-[var(--prui-dim)]", children: c.group }) : null,
216
- c.label
217
- ]
218
- },
219
- `${c.label}-${h}`
220
- )) })
221
- ]
222
- }
223
- )
224
- }
225
- );
226
- }
227
- function nt(t) {
228
- const {
229
- brand: l,
230
- nav: a = [],
231
- search: r = !0,
232
- theme: i = !0,
233
- sidebar: b = {},
234
- header: x,
235
- pages: m,
236
- pagesConfig: f,
237
- children: g
238
- } = t, y = typeof r == "object" ? r : { enabled: r !== !1 }, c = y.enabled !== !1, h = y.hotkey ?? "/", [P, N] = d.useState(!1), [$, S] = d.useState(!1), j = Z(), k = Ee(), s = d.useMemo(() => {
239
- const p = [];
240
- for (const n of a)
241
- if (n.items)
242
- for (const w of n.items)
243
- w.href && p.push({ label: `${n.label} / ${w.label}`, href: w.href, group: n.label });
244
- else n.href && p.push({ label: n.label, href: n.href });
245
- return p;
246
- }, [a]);
247
- d.useEffect(() => {
248
- if (!c) return;
249
- const p = (n) => {
250
- const w = n.target, E = w && (w.tagName === "INPUT" || w.tagName === "TEXTAREA" || w.isContentEditable);
251
- n.key === h && !E && !n.metaKey && !n.ctrlKey && !n.altKey && (n.preventDefault(), S(!0));
252
- };
253
- return document.addEventListener("keydown", p), () => document.removeEventListener("keydown", p);
254
- }, [h, c]), d.useEffect(() => {
255
- N(!1);
256
- }, [j.pathname]), d.useEffect(() => {
257
- if (!P) return;
258
- const p = document.body.style.overflow;
259
- return document.body.style.overflow = "hidden", () => {
260
- document.body.style.overflow = p;
261
- };
262
- }, [P]);
263
- const v = b.width ?? 240;
264
- return /* @__PURE__ */ u("div", { className: "prui-app flex min-h-screen bg-[var(--prui-background)] text-[var(--prui-fg)]", children: [
265
- /* @__PURE__ */ e(
266
- "aside",
267
- {
268
- "data-testid": "sidebar",
269
- className: "hidden md:block md:fixed md:inset-y-0 md:left-0 md:z-30 md:w-[var(--prui-sidebar-width)] md:border-r md:border-[var(--prui-line)] md:bg-[var(--prui-surface)]",
270
- style: { "--prui-sidebar-width": `${v}px` },
271
- children: /* @__PURE__ */ e("div", { className: "h-full overflow-y-auto p-2 pt-4 scrollbar-thin", children: /* @__PURE__ */ e(W, { nav: a }) })
272
- }
273
- ),
274
- P ? /* @__PURE__ */ u("div", { className: "md:hidden fixed inset-0 z-40", "data-testid": "mobile-drawer", children: [
275
- /* @__PURE__ */ e(
276
- "div",
277
- {
278
- className: "absolute inset-0",
279
- style: { backgroundColor: "rgba(0,0,0,0.55)" },
280
- onClick: () => N(!1),
281
- "data-testid": "drawer-backdrop"
282
- }
283
- ),
284
- /* @__PURE__ */ u(
285
- "aside",
286
- {
287
- className: "absolute left-0 top-0 h-full w-64 overflow-y-auto border-r border-[var(--prui-line)] bg-[var(--prui-surface)] p-2",
288
- role: "dialog",
289
- "aria-modal": "true",
290
- "aria-label": "Navigation",
291
- children: [
292
- /* @__PURE__ */ e("div", { className: "mb-2 flex h-10 items-center justify-end px-1", children: /* @__PURE__ */ e(
293
- "button",
294
- {
295
- type: "button",
296
- "aria-label": "Close navigation",
297
- onClick: () => N(!1),
298
- className: "rounded-[var(--prui-radius)] p-1 text-[var(--prui-dim)] hover:text-[var(--prui-fg)] cursor-pointer",
299
- children: /* @__PURE__ */ e(je, { className: "h-4 w-4", "aria-hidden": !0 })
300
- }
301
- ) }),
302
- /* @__PURE__ */ e(W, { nav: a, onNavigate: () => N(!1) })
303
- ]
304
- }
305
- )
306
- ] }) : null,
307
- /* @__PURE__ */ u("div", { className: "flex min-w-0 flex-1 flex-col md:ml-[var(--prui-sidebar-width)]", style: { "--prui-sidebar-width": `${v}px` }, children: [
308
- /* @__PURE__ */ u(
309
- "header",
310
- {
311
- "data-testid": "app-header",
312
- className: "sticky top-0 z-30 grid h-14 grid-cols-[1fr_auto_1fr] items-center gap-4 border-b border-[var(--prui-line)] bg-[var(--prui-surface)] px-4 md:px-6",
313
- children: [
314
- /* @__PURE__ */ e(
315
- D,
316
- {
317
- variant: "ghost",
318
- size: "icon",
319
- "aria-label": "Open navigation",
320
- className: "md:hidden",
321
- onClick: () => N(!0),
322
- "data-testid": "drawer-toggle",
323
- children: /* @__PURE__ */ e(Re, { className: "h-4 w-4", "aria-hidden": !0 })
324
- }
325
- ),
326
- /* @__PURE__ */ e("div", { className: "min-w-0 justify-self-start", children: /* @__PURE__ */ e(at, { brand: l }) }),
327
- c ? /* @__PURE__ */ u(
328
- "button",
329
- {
330
- type: "button",
331
- onClick: () => S(!0),
332
- "data-testid": "search-trigger",
333
- className: "hidden h-8 w-[min(420px,34vw)] items-center gap-2 rounded-[var(--prui-radius)] border border-[var(--prui-line)] bg-[var(--prui-background)] px-3.5 text-sm text-[var(--prui-dim)] hover:border-[var(--prui-dim)] cursor-pointer md:flex",
334
- children: [
335
- /* @__PURE__ */ e(De, { className: "h-4 w-4", "aria-hidden": !0 }),
336
- /* @__PURE__ */ e("span", { className: "flex-1 text-left", children: y.placeholder ?? "Search..." }),
337
- /* @__PURE__ */ e("kbd", { className: "ml-auto rounded-[var(--prui-radius-1)] border border-[var(--prui-line)] bg-[var(--prui-raise)] px-1.5 py-0.5 text-xs", children: h })
338
- ]
339
- }
340
- ) : null,
341
- /* @__PURE__ */ u("div", { className: "flex items-center gap-2 justify-self-end", children: [
342
- x,
343
- /* @__PURE__ */ e(et, { config: i })
344
- ] })
345
- ]
346
- }
347
- ),
348
- /* @__PURE__ */ e("main", { className: "flex-1 p-4 md:p-6", children: m ? /* @__PURE__ */ e(Ye, { mode: m, config: f, children: g }) : g ?? /* @__PURE__ */ e(Pe, {}) })
349
- ] }),
350
- c ? /* @__PURE__ */ e(lt, { open: $, onOpenChange: S, entries: s, placeholder: y.placeholder, onNavigate: k }) : null
351
- ] });
352
- }
353
- function St(t) {
354
- const { router: l = "browser", initialEntries: a } = t;
355
- return /* @__PURE__ */ e(l === "memory" ? ke : Ce, { initialEntries: a, children: /* @__PURE__ */ e(nt, { ...t }) });
356
- }
357
- const kt = {
358
- name: "App",
359
- props: [
360
- { name: "brand", type: "{ name, mark?, href? }", default: "undefined", control: "object" },
361
- { name: "nav", type: "NavItem[]", default: "[]", control: "object" },
362
- { name: "search", type: "boolean | { enabled?, hotkey?, placeholder? }", default: "true", control: "boolean" },
363
- { name: "theme", type: "boolean | { default?, persist? }", default: "true", control: "boolean" },
364
- { name: "sidebar", type: "{ width?, defaultOpen? }", default: "{}", control: "object" },
365
- { name: "router", type: "'browser' | 'memory'", default: "'browser'", control: "select", options: ["browser", "memory"] },
366
- { name: "initialEntries", type: "string[]", default: "undefined", control: "object" },
367
- { name: "header", type: "ReactNode", default: null, control: "none" },
368
- { name: "children", type: "ReactNode", default: "Outlet", control: "none" }
369
- ]
370
- };
371
- function Y(t, l) {
372
- const a = {};
373
- for (const r of t.fields)
374
- l && r.name in l ? a[r.name] = l[r.name] : r.defaultValue !== void 0 ? a[r.name] = r.defaultValue : r.type === "boolean" ? a[r.name] = !1 : (r.type, a[r.name] = "");
375
- return a;
376
- }
377
- function st({
378
- schema: t,
379
- initialValues: l,
380
- onSubmit: a,
381
- onCancel: r,
382
- cancelLabel: i = "Cancel",
383
- submitting: b = !1,
384
- error: x,
385
- className: m
386
- }) {
387
- const [f, g] = d.useState(() => Y(t, l)), [y, c] = d.useState({}), [h, P] = d.useState(null), [N, $] = d.useState(!1);
388
- d.useEffect(() => {
389
- g(Y(t, l));
390
- }, [JSON.stringify(t), JSON.stringify(l ?? {})]);
391
- const S = (s, v) => {
392
- g((p) => ({ ...p, [s]: v })), c((p) => {
393
- if (!p[s]) return p;
394
- const n = { ...p };
395
- return delete n[s], n;
396
- });
397
- }, j = async (s) => {
398
- var p;
399
- s.preventDefault();
400
- const v = {};
401
- for (const n of t.fields) {
402
- if (n.required) {
403
- const E = f[n.name];
404
- (E === "" || E == null || Array.isArray(E) && E.length === 0) && (v[n.name] = n.name === void 0 ? "Required" : `${n.label} is required.`);
405
- }
406
- const w = (p = n.validate) == null ? void 0 : p.call(n, f[n.name], f);
407
- w && (v[n.name] = w);
408
- }
409
- if (c(v), !(Object.keys(v).length > 0)) {
410
- P(null);
411
- try {
412
- const n = a == null ? void 0 : a({ ...f });
413
- n && typeof n.then == "function" && ($(!0), await n);
414
- } catch (n) {
415
- P(n instanceof Error ? n.message : String(n));
416
- } finally {
417
- $(!1);
418
- }
419
- }
420
- }, k = b || N;
421
- return /* @__PURE__ */ u("form", { onSubmit: j, className: C("prui-form flex flex-col gap-4", m), noValidate: !0, children: [
422
- /* @__PURE__ */ e("div", { className: C("grid gap-4", t.columns === 2 && "md:grid-cols-2"), children: t.fields.map((s) => {
423
- const v = y[s.name], p = {
424
- id: `prui-form-${s.name}`,
425
- "aria-invalid": v ? !0 : void 0,
426
- "aria-describedby": v ? `prui-form-${s.name}-error` : void 0
427
- };
428
- return /* @__PURE__ */ u("div", { className: C("flex flex-col gap-1.5", s.colSpan === 2 && "md:col-span-2", s.className), children: [
429
- /* @__PURE__ */ u(He, { htmlFor: p.id, children: [
430
- s.label,
431
- s.required ? /* @__PURE__ */ e("span", { className: "ml-0.5 text-[var(--prui-danger)]", children: "*" }) : null
432
- ] }),
433
- s.type === "textarea" ? /* @__PURE__ */ e(
434
- Je,
435
- {
436
- ...p,
437
- placeholder: s.placeholder,
438
- disabled: s.disabled || k,
439
- value: String(f[s.name] ?? ""),
440
- onChange: (n) => S(s.name, n.target.value)
441
- }
442
- ) : s.type === "select" ? /* @__PURE__ */ e(
443
- Xe,
444
- {
445
- ...p,
446
- options: s.options,
447
- placeholder: s.placeholder,
448
- disabled: s.disabled || k,
449
- value: String(f[s.name] ?? ""),
450
- onChange: (n) => S(s.name, n)
451
- }
452
- ) : s.type === "boolean" ? /* @__PURE__ */ e(
453
- Ge,
454
- {
455
- ...p,
456
- disabled: s.disabled || k,
457
- checked: !!f[s.name],
458
- onChange: (n) => S(s.name, n)
459
- }
460
- ) : /* @__PURE__ */ e(
461
- Ve,
462
- {
463
- ...p,
464
- type: s.type ?? "text",
465
- placeholder: s.placeholder,
466
- disabled: s.disabled || k,
467
- value: String(f[s.name] ?? ""),
468
- onChange: (n) => S(s.name, s.type === "number" ? n.target.value === "" ? "" : Number(n.target.value) : n.target.value)
469
- }
470
- ),
471
- v ? /* @__PURE__ */ e("p", { id: `prui-form-${s.name}-error`, role: "alert", className: "text-xs text-[var(--prui-danger)]", children: v }) : null
472
- ] }, s.name);
473
- }) }),
474
- x ?? h ? /* @__PURE__ */ e("p", { role: "alert", "data-testid": "form-error", className: "rounded-[var(--prui-radius)] border border-[var(--prui-danger)]/40 bg-[var(--prui-danger)]/10 px-3 py-2 text-sm text-[var(--prui-danger)]", children: x ?? h }) : null,
475
- /* @__PURE__ */ u("div", { className: "flex items-center justify-end gap-2", children: [
476
- r ? /* @__PURE__ */ e(D, { type: "button", variant: "ghost", onClick: r, disabled: k, children: i }) : null,
477
- /* @__PURE__ */ e(D, { type: "submit", variant: "primary", loading: k, children: t.submitLabel ?? "Submit" })
478
- ] })
479
- ] });
480
- }
481
- const Ct = {
482
- name: "Form",
483
- props: [
484
- { name: "schema", type: "FormSchema", default: null, control: "object" },
485
- { name: "initialValues", type: "Record<string, unknown>", default: "{}", control: "object" },
486
- { name: "onSubmit", type: "(values) => void | Promise<void>", default: null, control: "none" },
487
- { name: "onCancel", type: "() => void", default: null, control: "none" },
488
- { name: "submitting", type: "boolean", default: "false", control: "boolean" },
489
- { name: "error", type: "string | null", default: "null", control: "text" }
490
- ]
491
- };
492
- function it(t, l) {
493
- return { fields: t.filter((r) => r.formField !== !1 && !r.hidden).filter((r) => r.key !== "id" && r.key !== "actions").map((r) => ({
494
- name: r.key,
495
- label: typeof r.label == "string" ? r.label : r.key,
496
- type: ot(r, l),
497
- required: !1
498
- })), submitLabel: l ? "Save" : "Create" };
499
- }
500
- function ot(t, l) {
501
- return t.filter === "select" ? "select" : typeof (l == null ? void 0 : l[t.key]) == "number" ? "number" : "text";
502
- }
503
- function Et({
504
- name: t,
505
- columns: l,
506
- list: a,
507
- create: r,
508
- update: i,
509
- remove: b,
510
- actions: x,
511
- form: m,
512
- formSchema: f,
513
- rowKey: g,
514
- searchPlaceholder: y,
515
- pageSize: c = 20,
516
- className: h
517
- }) {
518
- const P = x ?? ["create", "edit", "delete"], N = (o) => P.includes(o) && (o === "create" ? !!r : o === "edit" ? !!i : !!b), [$, S] = d.useState([]), [j, k] = d.useState(null), [s, v] = d.useState(1), [p, n] = d.useState(c), [w, E] = d.useState([null]), [M, se] = d.useState(""), [F, ie] = d.useState({}), [A, oe] = d.useState(null), [de, q] = d.useState(!0), [B, K] = d.useState(null), [T, U] = d.useState(null), [ce, O] = d.useState(!1), [ue, _] = d.useState(!1), J = w[s - 1] ?? null, L = d.useCallback(async () => {
519
- q(!0), K(null);
520
- try {
521
- const o = await a({ search: M, cursor: J, pageSize: p, sort: A, filters: F });
522
- S(o.rows), k(o.nextCursor ?? null);
523
- } catch (o) {
524
- K(o instanceof Error ? o.message : String(o)), S([]), k(null);
525
- } finally {
526
- q(!1);
527
- }
528
- }, [M, J, p, A, JSON.stringify(F), a]);
529
- d.useEffect(() => {
530
- L();
531
- }, [L]), d.useEffect(() => {
532
- v(1), E([null]);
533
- }, [M, p, JSON.stringify(F)]);
534
- const me = l.filter((o) => o.filter).map((o) => ({
535
- key: o.key,
536
- label: typeof o.label == "string" ? o.label : o.key,
537
- type: o.filter,
538
- options: o.filterOptions
539
- })), pe = (o, z) => {
540
- ie((I) => ({ ...I, [o]: z }));
541
- }, fe = () => {
542
- U(null), O(!0);
543
- }, he = (o) => {
544
- U(o), O(!0);
545
- }, be = async (o) => {
546
- if (!(!await Be({
547
- title: `Delete ${R}?`,
548
- message: "This action cannot be undone.",
549
- confirmText: "Delete",
550
- cancelText: "Cancel",
551
- type: "danger"
552
- }) || !b)) {
553
- _(!0);
554
- try {
555
- await b(o), await L();
556
- } finally {
557
- _(!1);
558
- }
559
- }
560
- }, ge = (o, z) => {
561
- if (z === "next") {
562
- const I = j;
563
- E((ye) => {
564
- const H = [...ye];
565
- return H[o - 1] = I, H;
566
- });
567
- }
568
- v(o);
569
- }, ve = [
570
- ...l.filter((o) => !o.hidden),
571
- ...N("edit") || N("delete") ? [
572
- {
573
- key: "actions",
574
- label: "",
575
- align: "right",
576
- render: (o) => /* @__PURE__ */ u("div", { className: "flex items-center justify-end gap-1", children: [
577
- N("edit") ? /* @__PURE__ */ e(D, { variant: "ghost", size: "icon", "aria-label": "Edit", onClick: () => he(o), "data-testid": "resource-edit", children: /* @__PURE__ */ e(Oe, { className: "h-3.5 w-3.5", "aria-hidden": !0 }) }) : null,
578
- N("delete") ? /* @__PURE__ */ e(D, { variant: "ghost", size: "icon", "aria-label": "Delete", disabled: ue, onClick: () => void be(o), "data-testid": "resource-delete", children: /* @__PURE__ */ e(Fe, { className: "h-3.5 w-3.5 text-[var(--prui-danger)]", "aria-hidden": !0 }) }) : null
579
- ] })
580
- }
581
- ] : []
582
- ], V = f ?? it(l, T), xe = T ? Object.fromEntries(
583
- V.fields.map((o) => [o.name, T[o.name] ?? ""])
584
- ) : void 0, R = t.replace(/s$/, "");
585
- return /* @__PURE__ */ u("div", { className: C("prui-resource flex flex-col gap-2", h), "data-testid": "resource", children: [
586
- /* @__PURE__ */ e("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ e("h1", { className: "text-xl font-semibold capitalize text-[var(--prui-fg)]", children: t }) }),
587
- /* @__PURE__ */ e(
588
- Ke,
589
- {
590
- searchPlaceholder: y ?? `Search ${t}...`,
591
- searchValue: M,
592
- onSearchChange: se,
593
- filters: me,
594
- filterValues: F,
595
- onFilterChange: pe,
596
- actions: N("create") ? /* @__PURE__ */ u(D, { variant: "primary", size: "sm", onClick: fe, "data-testid": "resource-create", children: [
597
- /* @__PURE__ */ e(Le, { className: "h-4 w-4", "aria-hidden": !0 }),
598
- "New ",
599
- R
600
- ] }) : void 0
601
- }
602
- ),
603
- B ? /* @__PURE__ */ e("div", { role: "alert", "data-testid": "resource-error", className: "rounded-[var(--prui-radius)] border border-[var(--prui-danger)]/40 bg-[var(--prui-danger)]/10 px-3 py-2 text-sm text-[var(--prui-danger)]", children: B }) : null,
604
- /* @__PURE__ */ e(Ue, { columns: ve, rows: $, rowKey: g, sort: A, onSortChange: oe, loading: de }),
605
- /* @__PURE__ */ e(
606
- _e,
607
- {
608
- page: s,
609
- pageSize: p,
610
- hasNextPage: j != null,
611
- hasPreviousPage: s > 1,
612
- onPageChange: ge,
613
- onPageSizeChange: (o) => n(o)
614
- }
615
- ),
616
- /* @__PURE__ */ u(qe, { open: ce, onClose: () => O(!1), size: "sm", ariaLabel: T ? `Edit ${R}` : `New ${R}`, noPadding: !0, children: [
617
- /* @__PURE__ */ e("div", { className: "px-8 pt-8 pb-2", children: /* @__PURE__ */ e("h2", { className: "text-lg font-semibold", style: { color: "var(--prui-fg)" }, children: T ? `Edit ${R}` : `New ${R}` }) }),
618
- /* @__PURE__ */ e("div", { className: "px-8 pb-8", children: m ?? /* @__PURE__ */ e(
619
- st,
620
- {
621
- schema: V,
622
- initialValues: xe,
623
- onCancel: () => O(!1),
624
- onSubmit: async (o) => {
625
- T ? await (i == null ? void 0 : i(T, o)) : await (r == null ? void 0 : r(o)), O(!1), await L();
626
- }
627
- }
628
- ) })
629
- ] })
630
- ] });
631
- }
632
- const Pt = {
633
- name: "Resource",
634
- props: [
635
- { name: "name", type: "string", default: null, control: "text" },
636
- { name: "columns", type: "ResourceColumn<T>[]", default: null, control: "object" },
637
- { name: "list", type: "(query) => Promise<{rows, nextCursor}>", default: null, control: "none" },
638
- { name: "create", type: "(values) => Promise<void>", default: null, control: "none" },
639
- { name: "update", type: "(row, values) => Promise<void>", default: null, control: "none" },
640
- { name: "remove", type: "(row) => Promise<void>", default: null, control: "none" },
641
- { name: "actions", type: "('create' | 'edit' | 'delete')[]", default: "all provided", control: "multiselect", options: ["create", "edit", "delete"] },
642
- { name: "form", type: "ReactNode", default: "built-in schema form", control: "none" },
643
- { name: "formSchema", type: "FormSchema", default: "derived from columns", control: "object" },
644
- { name: "pageSize", type: "number", default: "20", control: "number" }
645
- ]
646
- };
647
- function Tt({ items: t, className: l }) {
648
- return /* @__PURE__ */ e("div", { className: C("prui-stat-row grid gap-3 sm:grid-cols-2 lg:grid-cols-4", l), children: t.map((a, r) => {
649
- const i = a.icon;
650
- return /* @__PURE__ */ u(ee, { "data-testid": "stat-card", children: [
651
- /* @__PURE__ */ u(te, { className: "pb-2", children: [
652
- /* @__PURE__ */ u(re, { className: "flex items-center gap-1.5 text-xs", children: [
653
- i ? /* @__PURE__ */ e(i, { className: "h-3.5 w-3.5", "aria-hidden": !0 }) : null,
654
- a.label
655
- ] }),
656
- /* @__PURE__ */ e(ae, { className: "text-2xl", children: a.value })
657
- ] }),
658
- a.delta ? /* @__PURE__ */ e(le, { className: "pb-3", children: /* @__PURE__ */ e(
659
- "span",
660
- {
661
- className: C(
662
- "text-xs font-medium",
663
- a.trend === "up" && "text-[var(--prui-ok)]",
664
- a.trend === "down" && "text-[var(--prui-danger)]",
665
- (!a.trend || a.trend === "flat") && "text-[var(--prui-dim)]"
666
- ),
667
- children: a.delta
668
- }
669
- ) }) : null
670
- ] }, `${a.label}-${r}`);
671
- }) });
672
- }
673
- const $t = {
674
- name: "StatRow",
675
- props: [
676
- { name: "items", type: "StatItem[]", default: null, control: "object" },
677
- { name: "className", type: "string", default: "undefined", control: "text" }
678
- ]
679
- };
680
- function jt({ title: t = "Settings", sections: l, className: a }) {
681
- return /* @__PURE__ */ u("div", { className: C("prui-settings mx-auto flex w-full max-w-4xl flex-col gap-6", a), children: [
682
- /* @__PURE__ */ e("h1", { className: "text-xl font-semibold text-[var(--prui-fg)]", children: t }),
683
- /* @__PURE__ */ u("div", { className: "flex flex-col gap-6 md:flex-row", children: [
684
- /* @__PURE__ */ e("nav", { "aria-label": "Settings sections", className: "md:w-56 md:shrink-0", children: /* @__PURE__ */ e("ul", { className: "flex flex-row flex-wrap gap-1 md:flex-col", children: l.map((r) => /* @__PURE__ */ e("li", { children: /* @__PURE__ */ e(
685
- "a",
686
- {
687
- href: `#settings-${r.id}`,
688
- className: "block rounded-[var(--prui-radius)] px-2.5 py-1.5 text-sm text-[var(--prui-dim)] hover:bg-[var(--prui-raise)] hover:text-[var(--prui-fg)]",
689
- children: r.title
690
- }
691
- ) }, r.id)) }) }),
692
- /* @__PURE__ */ e("div", { className: "flex min-w-0 flex-1 flex-col gap-4", children: l.map((r) => /* @__PURE__ */ u(ee, { id: `settings-${r.id}`, "data-testid": "settings-section", children: [
693
- /* @__PURE__ */ u(te, { children: [
694
- /* @__PURE__ */ e(ae, { children: r.title }),
695
- r.description ? /* @__PURE__ */ e(re, { children: r.description }) : null
696
- ] }),
697
- /* @__PURE__ */ e(le, { children: r.content ?? /* @__PURE__ */ e("dl", { className: "flex flex-col divide-y divide-[var(--prui-line)]", children: (r.fields ?? []).map((i, b) => /* @__PURE__ */ u("div", { className: "flex flex-col gap-2 py-3 first:pt-0 last:pb-0 sm:flex-row sm:items-center sm:justify-between", children: [
698
- /* @__PURE__ */ u("div", { className: "min-w-0", children: [
699
- /* @__PURE__ */ e("dt", { className: "text-sm font-medium text-[var(--prui-fg)]", children: i.label }),
700
- i.description ? /* @__PURE__ */ e("dd", { className: "text-xs text-[var(--prui-dim)]", children: i.description }) : null
701
- ] }),
702
- i.control ? /* @__PURE__ */ e("div", { className: "shrink-0", children: i.control }) : null
703
- ] }, `${i.label}-${b}`)) }) })
704
- ] }, r.id)) })
705
- ] })
706
- ] });
707
- }
708
- const Rt = {
709
- name: "Settings",
710
- props: [
711
- { name: "title", type: "string", default: "'Settings'", control: "text" },
712
- { name: "sections", type: "SettingsSection[]", default: null, control: "object" }
713
- ]
714
- };
715
- export {
716
- St as A,
717
- lt as C,
718
- st as F,
719
- Et as R,
720
- jt as S,
721
- nt as a,
722
- Ye as b,
723
- W as c,
724
- Tt as d,
725
- kt as e,
726
- Ct as f,
727
- $t as g,
728
- Pt as r,
729
- Rt as s,
730
- Ze as u
731
- };