@okjt/dashboard 0.9.0 → 0.10.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.
@@ -0,0 +1,275 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useRef, useState } from "react";
3
+ import {
4
+ useReactTable,
5
+ getCoreRowModel,
6
+ getFilteredRowModel,
7
+ getPaginationRowModel,
8
+ getSortedRowModel,
9
+ flexRender
10
+ } from "@tanstack/react-table";
11
+ import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, Search, ArrowUpDown, Eye } from "lucide-react";
12
+ import { Modal } from "@okjt/ui";
13
+ import EmptyState from "./EmptyState.js";
14
+ const columnLabel = (column) => column.columnDef.meta?.label ?? (typeof column.columnDef.header === "string" ? column.columnDef.header : column.id);
15
+ const ACTION_VARIANTS = {
16
+ success: "hover:bg-positive/10 hover:text-positive-dark",
17
+ warning: "hover:bg-amber-500/10 hover:text-amber-700",
18
+ danger: "hover:bg-red-500/10 hover:text-red-600"
19
+ };
20
+ const DETAIL_ACTION_VARIANTS = {
21
+ default: "bg-contrast/5 text-ink hover:bg-contrast/10",
22
+ success: "bg-positive/10 text-positive-dark hover:bg-positive/20",
23
+ warning: "bg-amber-500/10 text-amber-700 hover:bg-amber-500/20",
24
+ danger: "bg-red-500/10 text-red-600 hover:bg-red-500/20"
25
+ };
26
+ const identity = (text) => text;
27
+ const cx = (...parts) => parts.filter(Boolean).join(" ");
28
+ const checkboxClass = "h-4 w-4 cursor-pointer rounded border-contrast/20 text-accent focus:ring-accent/20 focus:ring-offset-0";
29
+ const pageButtonClass = "rounded-lg p-2 hover:bg-contrast/5 disabled:cursor-not-allowed disabled:opacity-30";
30
+ function AdminDataTable({
31
+ data,
32
+ columns,
33
+ title = null,
34
+ searchPlaceholder = "Search...",
35
+ searchValue = "",
36
+ serverPagination = null,
37
+ onPageChange,
38
+ onSearch = null,
39
+ enableSelection = false,
40
+ selectedIds = null,
41
+ onSelectionChange = null,
42
+ getRowId = (row) => row.id,
43
+ emptyMessage = "No results found",
44
+ emptyIcon,
45
+ emptyAction = null,
46
+ rowActions = [],
47
+ detailTitle = null,
48
+ translate = identity
49
+ }) {
50
+ const t = translate;
51
+ const [sorting, setSorting] = useState([]);
52
+ const [globalFilter, setGlobalFilter] = useState("");
53
+ const [rowSelection, setRowSelection] = useState({});
54
+ const isSelectionControlled = Array.isArray(selectedIds);
55
+ const selectionState = isSelectionControlled ? Object.fromEntries(selectedIds.map((id) => [String(id), true])) : rowSelection;
56
+ const handleRowSelectionChange = (updater) => {
57
+ const next = typeof updater === "function" ? updater(selectionState) : updater;
58
+ if (!isSelectionControlled) {
59
+ setRowSelection(next);
60
+ return;
61
+ }
62
+ onSelectionChange?.(data.map(getRowId).filter((id) => next[String(id)]));
63
+ };
64
+ const selectionColumn = enableSelection ? [{
65
+ id: "select",
66
+ header: ({ table: table2 }) => /* @__PURE__ */ jsx(
67
+ "input",
68
+ {
69
+ type: "checkbox",
70
+ "aria-label": "Select all",
71
+ checked: table2.getIsAllPageRowsSelected(),
72
+ onChange: (e) => table2.toggleAllPageRowsSelected(e.target.checked),
73
+ className: checkboxClass
74
+ }
75
+ ),
76
+ cell: ({ row }) => /* @__PURE__ */ jsx(
77
+ "input",
78
+ {
79
+ type: "checkbox",
80
+ "aria-label": "Select row",
81
+ checked: row.getIsSelected(),
82
+ onChange: (e) => row.toggleSelected(e.target.checked),
83
+ className: checkboxClass
84
+ }
85
+ ),
86
+ size: 40,
87
+ enableSorting: false,
88
+ enableHiding: false
89
+ }] : [];
90
+ const [detailRow, setDetailRow] = useState(null);
91
+ const actionColumn = rowActions.length > 0 || detailTitle ? [{
92
+ id: "actions",
93
+ header: "Actions",
94
+ enableSorting: false,
95
+ enableGlobalFilter: false,
96
+ cell: ({ row }) => /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-1 lg:justify-start", children: [
97
+ detailTitle && /* @__PURE__ */ jsx("button", { type: "button", className: "rounded-lg p-2 text-soft transition-colors hover:bg-contrast/5 hover:text-ink", title: "View details", "aria-label": "View details", onClick: () => setDetailRow(row.original), children: /* @__PURE__ */ jsx(Eye, { size: 14 }) }),
98
+ rowActions.map((action) => /* @__PURE__ */ jsx(
99
+ "button",
100
+ {
101
+ type: "button",
102
+ className: cx("rounded-lg p-2 text-soft transition-colors", ACTION_VARIANTS[action.variant] ?? "hover:bg-contrast/5 hover:text-ink"),
103
+ title: t(action.label),
104
+ "aria-label": t(action.label),
105
+ onClick: () => action.onClick(row.original),
106
+ children: action.icon
107
+ },
108
+ action.key
109
+ ))
110
+ ] })
111
+ }] : [];
112
+ const allColumns = [...selectionColumn, ...columns, ...actionColumn];
113
+ const columnVisibility = Object.fromEntries(columns.filter((c) => c.meta?.detailOnly).map((c) => [c.id ?? c.accessorKey, false]));
114
+ const table = useReactTable({
115
+ data,
116
+ columns: allColumns,
117
+ state: {
118
+ sorting,
119
+ globalFilter,
120
+ columnVisibility,
121
+ ...enableSelection ? { rowSelection: selectionState } : {}
122
+ },
123
+ onSortingChange: setSorting,
124
+ onGlobalFilterChange: setGlobalFilter,
125
+ ...enableSelection ? { onRowSelectionChange: handleRowSelectionChange } : {},
126
+ getCoreRowModel: getCoreRowModel(),
127
+ getFilteredRowModel: getFilteredRowModel(),
128
+ getPaginationRowModel: serverPagination ? void 0 : getPaginationRowModel(),
129
+ getSortedRowModel: getSortedRowModel(),
130
+ manualPagination: !!serverPagination,
131
+ pageCount: serverPagination ? serverPagination.last_page : void 0,
132
+ getRowId
133
+ });
134
+ useEffect(() => {
135
+ if (enableSelection && onSelectionChange && !isSelectionControlled) {
136
+ onSelectionChange(table.getSelectedRowModel().rows.map((r) => r.original));
137
+ }
138
+ }, [rowSelection]);
139
+ const typed = useRef(false);
140
+ useEffect(() => {
141
+ if (!typed.current) {
142
+ typed.current = true;
143
+ return void 0;
144
+ }
145
+ if (!onSearch || serverPagination) return void 0;
146
+ const timeout = setTimeout(() => onSearch(globalFilter), 300);
147
+ return () => clearTimeout(timeout);
148
+ }, [globalFilter]);
149
+ const goToPage = (page) => {
150
+ if (serverPagination) onPageChange?.(page);
151
+ };
152
+ const rows = table.getRowModel().rows;
153
+ const render = (cell) => flexRender(cell.column.columnDef.cell, cell.getContext());
154
+ return /* @__PURE__ */ jsxs("div", { className: "w-full space-y-4", children: [
155
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center justify-between gap-3 border-b border-contrast/5 p-4", children: [
156
+ title && /* @__PURE__ */ jsx("h3", { className: "text-sm font-bold text-ink", children: t(title) }),
157
+ /* @__PURE__ */ jsxs("label", { className: "flex w-full items-center gap-2 rounded-xl bg-contrast/5 px-3 py-2 sm:w-64", children: [
158
+ /* @__PURE__ */ jsx(Search, { size: 16, className: "text-muted", "aria-hidden": "true" }),
159
+ serverPagination && onSearch ? /* @__PURE__ */ jsx(
160
+ "input",
161
+ {
162
+ type: "search",
163
+ defaultValue: searchValue,
164
+ onChange: (e) => onSearch(e.target.value),
165
+ placeholder: searchPlaceholder,
166
+ "aria-label": searchPlaceholder,
167
+ className: "w-full border-none bg-transparent p-0 text-sm font-bold placeholder:text-muted focus:ring-0"
168
+ }
169
+ ) : /* @__PURE__ */ jsx(
170
+ "input",
171
+ {
172
+ type: "search",
173
+ value: globalFilter ?? "",
174
+ onChange: (e) => setGlobalFilter(e.target.value),
175
+ placeholder: searchPlaceholder,
176
+ "aria-label": searchPlaceholder,
177
+ className: "w-full border-none bg-transparent p-0 text-sm font-bold placeholder:text-muted focus:ring-0"
178
+ }
179
+ )
180
+ ] })
181
+ ] }),
182
+ rows.length === 0 && /* @__PURE__ */ jsx(EmptyState, { icon: emptyIcon, message: emptyMessage, action: emptyAction }),
183
+ rows.length > 0 && /* @__PURE__ */ jsx("div", { className: "hidden overflow-x-auto lg:block", "data-view": "table", children: /* @__PURE__ */ jsxs("table", { className: "w-full", children: [
184
+ /* @__PURE__ */ jsx("thead", { children: table.getHeaderGroups().map((headerGroup) => /* @__PURE__ */ jsx("tr", { className: "border-b border-contrast/5 bg-contrast/[0.02]", children: headerGroup.headers.map((header) => {
185
+ const checkbox = header.id === "select";
186
+ const sortable = !checkbox && header.column.getCanSort();
187
+ return /* @__PURE__ */ jsx(
188
+ "th",
189
+ {
190
+ scope: "col",
191
+ "aria-sort": { asc: "ascending", desc: "descending" }[header.column.getIsSorted()],
192
+ className: cx("px-4 py-3 text-left text-xs font-semibold text-soft select-none", checkbox ? "w-10 cursor-default text-center" : sortable && "cursor-pointer transition-colors hover:bg-contrast/5"),
193
+ onClick: sortable ? header.column.getToggleSortingHandler() : void 0,
194
+ children: /* @__PURE__ */ jsxs("div", { className: "group flex items-center gap-2", children: [
195
+ flexRender(header.column.columnDef.header, header.getContext()),
196
+ sortable && ({
197
+ asc: /* @__PURE__ */ jsx(ArrowUpDown, { size: 12, className: "rotate-180" }),
198
+ desc: /* @__PURE__ */ jsx(ArrowUpDown, { size: 12 })
199
+ }[header.column.getIsSorted()] ?? /* @__PURE__ */ jsx(ArrowUpDown, { size: 12, className: "opacity-0 group-hover:opacity-50" }))
200
+ ] })
201
+ },
202
+ header.id
203
+ );
204
+ }) }, headerGroup.id)) }),
205
+ /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-contrast/5", children: rows.map((row) => /* @__PURE__ */ jsx("tr", { className: cx("transition-colors", row.getIsSelected() ? "bg-accent/5 hover:bg-accent/10" : "hover:bg-contrast/[0.02]"), children: row.getVisibleCells().map((cell) => /* @__PURE__ */ jsx("td", { className: cx("px-4 py-3 text-sm font-medium text-body", cell.column.id === "select" && "w-10 text-center"), children: render(cell) }, cell.id)) }, row.id)) })
206
+ ] }) }),
207
+ rows.length > 0 && /* @__PURE__ */ jsx("ul", { className: "space-y-3 px-4 lg:hidden", "data-view": "cards", children: rows.map((row) => {
208
+ const cells = row.getVisibleCells();
209
+ const select = cells.find((c) => c.column.id === "select");
210
+ const actions = cells.find((c) => c.column.id === "actions");
211
+ const [primary, ...fields] = cells.filter((c) => c !== select && c !== actions);
212
+ return /* @__PURE__ */ jsxs("li", { className: cx("rounded-2xl border p-4", row.getIsSelected() ? "border-accent/30 bg-accent/5" : "border-contrast/5 bg-surface"), children: [
213
+ /* @__PURE__ */ jsxs("div", { className: "flex items-start gap-3", children: [
214
+ select && /* @__PURE__ */ jsx("div", { className: "pt-1", children: render(select) }),
215
+ /* @__PURE__ */ jsx("div", { className: "min-w-0 flex-1 text-sm font-semibold text-ink", children: primary && render(primary) })
216
+ ] }),
217
+ fields.map((cell) => /* @__PURE__ */ jsxs("div", { className: "mt-2 flex items-center justify-between gap-4 border-t border-contrast/5 py-2", children: [
218
+ /* @__PURE__ */ jsx("span", { className: "text-xs font-semibold uppercase tracking-widest text-muted", children: t(columnLabel(cell.column)) }),
219
+ /* @__PURE__ */ jsx("div", { className: "min-w-0 text-right text-sm font-semibold text-body [overflow-wrap:anywhere]", children: render(cell) })
220
+ ] }, cell.id)),
221
+ actions && /* @__PURE__ */ jsx("div", { className: "mt-2 flex justify-end border-t border-contrast/5 pt-2", children: render(actions) })
222
+ ] }, row.id);
223
+ }) }),
224
+ detailTitle && /* @__PURE__ */ jsx(Modal, { open: !!detailRow, onClose: () => setDetailRow(null), title: t(detailTitle), maxWidth: "lg", children: detailRow && (() => {
225
+ const row = table.getCoreRowModel().rows.find((r) => r.original === detailRow);
226
+ const cells = row ? row.getAllCells().filter((c) => !["select", "actions"].includes(c.column.id)) : [];
227
+ return /* @__PURE__ */ jsxs("div", { children: [
228
+ /* @__PURE__ */ jsx("dl", { className: "divide-y divide-contrast/5", children: cells.map((cell) => /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between gap-4 py-3", children: [
229
+ /* @__PURE__ */ jsx("dt", { className: "text-xs font-semibold uppercase tracking-widest text-muted", children: t(columnLabel(cell.column)) }),
230
+ /* @__PURE__ */ jsx("dd", { className: "min-w-0 text-right text-sm font-semibold text-body [overflow-wrap:anywhere]", children: render(cell) })
231
+ ] }, cell.id)) }),
232
+ rowActions.length > 0 && /* @__PURE__ */ jsx("div", { className: "mt-4 flex flex-wrap justify-end gap-2", children: rowActions.map((action) => /* @__PURE__ */ jsxs(
233
+ "button",
234
+ {
235
+ type: "button",
236
+ className: cx("inline-flex items-center gap-2 rounded-xl px-4 py-2 text-sm font-semibold transition-colors", DETAIL_ACTION_VARIANTS[action.variant] ?? DETAIL_ACTION_VARIANTS.default),
237
+ onClick: () => {
238
+ setDetailRow(null);
239
+ action.onClick(detailRow);
240
+ },
241
+ children: [
242
+ action.icon,
243
+ " ",
244
+ t(action.label)
245
+ ]
246
+ },
247
+ action.key
248
+ )) })
249
+ ] });
250
+ })() }),
251
+ rows.length > 0 && /* @__PURE__ */ jsxs("nav", { "aria-label": "Pages", className: "flex flex-wrap items-center justify-between gap-2 border-t border-contrast/5 px-4 py-4", children: [
252
+ /* @__PURE__ */ jsx("div", { className: "flex-1 text-xs font-bold text-soft", children: serverPagination ? `Showing ${serverPagination.from}-${serverPagination.to} of ${serverPagination.total}` : `Page ${table.getState().pagination.pageIndex + 1} of ${table.getPageCount()}` }),
253
+ /* @__PURE__ */ jsx("div", { className: "flex items-center gap-2", children: serverPagination ? /* @__PURE__ */ jsxs(Fragment, { children: [
254
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "First page", className: pageButtonClass, onClick: () => goToPage(1), disabled: !serverPagination.prev_page_url, children: /* @__PURE__ */ jsx(ChevronsLeft, { size: 16 }) }),
255
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Previous page", className: pageButtonClass, onClick: () => goToPage(serverPagination.current_page - 1), disabled: !serverPagination.prev_page_url, children: /* @__PURE__ */ jsx(ChevronLeft, { size: 16 }) }),
256
+ /* @__PURE__ */ jsxs("span", { className: "px-4 py-2 text-xs font-semibold text-soft", children: [
257
+ "Page ",
258
+ serverPagination.current_page,
259
+ " of ",
260
+ serverPagination.last_page
261
+ ] }),
262
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Next page", className: pageButtonClass, onClick: () => goToPage(serverPagination.current_page + 1), disabled: !serverPagination.next_page_url, children: /* @__PURE__ */ jsx(ChevronRight, { size: 16 }) }),
263
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Last page", className: pageButtonClass, onClick: () => goToPage(serverPagination.last_page), disabled: !serverPagination.next_page_url, children: /* @__PURE__ */ jsx(ChevronsRight, { size: 16 }) })
264
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
265
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "First page", className: pageButtonClass, onClick: () => table.setPageIndex(0), disabled: !table.getCanPreviousPage(), children: /* @__PURE__ */ jsx(ChevronsLeft, { size: 16 }) }),
266
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Previous page", className: pageButtonClass, onClick: () => table.previousPage(), disabled: !table.getCanPreviousPage(), children: /* @__PURE__ */ jsx(ChevronLeft, { size: 16 }) }),
267
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Next page", className: pageButtonClass, onClick: () => table.nextPage(), disabled: !table.getCanNextPage(), children: /* @__PURE__ */ jsx(ChevronRight, { size: 16 }) }),
268
+ /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Last page", className: pageButtonClass, onClick: () => table.setPageIndex(table.getPageCount() - 1), disabled: !table.getCanNextPage(), children: /* @__PURE__ */ jsx(ChevronsRight, { size: 16 }) })
269
+ ] }) })
270
+ ] })
271
+ ] });
272
+ }
273
+ export {
274
+ AdminDataTable as default
275
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okjt/dashboard",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Dashboard pages for OKJTech projects: page layout, hero, summary cards, floating toolbar with search and filters, side panel, lists, bulk actions, one way to save, and working together: presence, version history, sharing; the dashboard shell, sign-in pages and account menu",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,7 +11,8 @@
11
11
  },
12
12
  "exports": {
13
13
  ".": "./dist/index.js",
14
- "./toolbar-glass.css": "./dist/toolbar-glass.css"
14
+ "./toolbar-glass.css": "./dist/toolbar-glass.css",
15
+ "./table": "./dist/AdminDataTable.js"
15
16
  },
16
17
  "files": [
17
18
  "dist",
@@ -37,9 +38,18 @@
37
38
  "react-dom": ">=18",
38
39
  "react-hook-form": ">=7",
39
40
  "react-hot-toast": ">=2",
40
- "react-router-dom": ">=6.4"
41
+ "react-router-dom": ">=6.4",
42
+ "@tanstack/react-table": ">=8"
41
43
  },
42
44
  "publishConfig": {
43
45
  "access": "public"
46
+ },
47
+ "devDependencies": {
48
+ "@tanstack/react-table": "^8.21.3"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "@tanstack/react-table": {
52
+ "optional": true
53
+ }
44
54
  }
45
55
  }