all-purpose-table 1.0.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.
- package/README.md +288 -0
- package/dist/index.d.mts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +580 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +577 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { useState, useRef, useMemo, useEffect, Fragment as Fragment$1 } from 'react';
|
|
2
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
// #style-inject:#style-inject
|
|
5
|
+
function styleInject(css, { insertAt } = {}) {
|
|
6
|
+
if (typeof document === "undefined") return;
|
|
7
|
+
const head = document.head || document.getElementsByTagName("head")[0];
|
|
8
|
+
const style = document.createElement("style");
|
|
9
|
+
style.type = "text/css";
|
|
10
|
+
if (insertAt === "top") {
|
|
11
|
+
if (head.firstChild) {
|
|
12
|
+
head.insertBefore(style, head.firstChild);
|
|
13
|
+
} else {
|
|
14
|
+
head.appendChild(style);
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
head.appendChild(style);
|
|
18
|
+
}
|
|
19
|
+
if (style.styleSheet) {
|
|
20
|
+
style.styleSheet.cssText = css;
|
|
21
|
+
} else {
|
|
22
|
+
style.appendChild(document.createTextNode(css));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/styles.css
|
|
27
|
+
styleInject(":root {\n --apt-color-primary: #1f2937;\n --apt-color-border: #d1d5db;\n --apt-color-border-dark: #333;\n --apt-color-bg: white;\n --apt-color-bg-secondary: #e5e7eb;\n --apt-color-bg-hover: #f3f4f6;\n --apt-color-bg-dark: #1e1e1e;\n --apt-color-bg-dark-secondary: #333;\n --apt-color-bg-dark-hover: rgba(136, 136, 136, 0.2);\n --apt-color-text: #1f2937;\n --apt-color-text-dark: #eee;\n --apt-color-text-muted: #6b7280;\n --apt-color-text-muted-dark: #444;\n --apt-color-accent: #9333ea;\n --apt-color-accent-dark: #a855f7;\n --apt-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);\n --apt-shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);\n}\n.apt-table-container {\n color: var(--apt-color-text);\n border-radius: 6px;\n margin: 0 6px;\n border: 1px solid var(--apt-color-border);\n background-color: var(--apt-color-bg);\n box-shadow: var(--apt-shadow);\n display: flex;\n flex-direction: column;\n overflow: hidden;\n}\n@media (prefers-color-scheme: dark) {\n .apt-table-container {\n color: var(--apt-color-text-dark);\n border-color: var(--apt-color-border-dark);\n background-color: var(--apt-color-bg-dark);\n }\n}\n.apt-empty-state {\n flex-grow: 1;\n display: flex;\n align-items: center;\n justify-content: center;\n text-align: center;\n padding: 48px 0;\n color: #9ca3af;\n}\n@media (prefers-color-scheme: dark) {\n .apt-empty-state {\n color: var(--apt-color-text-muted-dark);\n }\n}\n.apt-scroll-area {\n flex-grow: 1;\n overflow: auto;\n}\n.apt-table {\n width: 100%;\n text-align: left;\n font-size: 0.75rem;\n line-height: 1rem;\n table-layout: fixed;\n}\n.apt-thead {\n position: sticky;\n top: 0;\n background-color: var(--apt-color-bg-secondary);\n z-index: 10;\n}\n@media (prefers-color-scheme: dark) {\n .apt-thead {\n background-color: var(--apt-color-bg-dark-secondary);\n }\n}\n.apt-th {\n position: relative;\n padding: 12px 8px;\n}\n.apt-th-sortable {\n cursor: pointer;\n}\n.apt-th-sortable:hover {\n background-color: #d1d5db;\n}\n@media (prefers-color-scheme: dark) {\n .apt-th-sortable:hover {\n background-color: #444;\n }\n}\n.apt-th-content {\n display: flex;\n align-items: center;\n justify-content: center;\n}\n.apt-resizer {\n position: absolute;\n right: 0;\n top: 0;\n height: 100%;\n width: 4px;\n cursor: col-resize;\n user-select: none;\n touch-action: none;\n background: transparent;\n}\n.apt-resizer:hover {\n background-color: var(--apt-color-accent);\n}\n@media (prefers-color-scheme: dark) {\n .apt-resizer:hover {\n background-color: var(--apt-color-accent-dark);\n }\n}\n.apt-tbody {\n border-top: 1px solid var(--apt-color-border);\n}\n@media (prefers-color-scheme: dark) {\n .apt-tbody {\n border-color: var(--apt-color-border-dark);\n }\n}\n.apt-tbody tr {\n border-bottom: 1px solid var(--apt-color-border);\n}\n@media (prefers-color-scheme: dark) {\n .apt-tbody tr {\n border-color: var(--apt-color-border-dark);\n }\n}\n.apt-row {\n transition: background-color 0.15s;\n}\n.apt-row:hover {\n background-color: var(--apt-color-bg-hover);\n}\n@media (prefers-color-scheme: dark) {\n .apt-row:hover {\n background-color: var(--apt-color-bg-dark-hover);\n }\n}\n.apt-row-expanded {\n background-color: #f9fafb;\n}\n@media (prefers-color-scheme: dark) {\n .apt-row-expanded {\n background-color: #2b2b2b;\n }\n}\n.apt-td {\n padding: 4px 8px;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.apt-td-actions {\n padding: 4px 8px;\n}\n.apt-footer {\n display: grid;\n grid-template-columns: 1fr 1fr 1fr;\n align-items: center;\n padding: 8px;\n background-color: var(--apt-color-bg-secondary);\n}\n@media (prefers-color-scheme: dark) {\n .apt-footer {\n background-color: var(--apt-color-bg-dark-secondary);\n }\n}\n.apt-footer-left {\n display: flex;\n align-items: center;\n gap: 8px;\n}\n.apt-footer-center {\n justify-self: center;\n font-size: 0.875rem;\n color: #374151;\n}\n@media (prefers-color-scheme: dark) {\n .apt-footer-center {\n color: #d1d5db;\n }\n}\n.apt-footer-right {\n justify-self: end;\n display: flex;\n align-items: center;\n gap: 8px;\n}\n.apt-rows-label {\n font-size: 0.75rem;\n text-transform: uppercase;\n letter-spacing: 0.05em;\n color: #4b5563;\n}\n@media (prefers-color-scheme: dark) {\n .apt-rows-label {\n color: #d1d5db;\n }\n}\n.apt-rows-select {\n padding: 4px 8px;\n font-size: 0.875rem;\n border-radius: 4px;\n background-color: white;\n border: 1px solid var(--apt-color-border);\n}\n@media (prefers-color-scheme: dark) {\n .apt-rows-select {\n background-color: #444;\n border-color: #555;\n color: var(--apt-color-text-dark);\n }\n}\n.apt-btn {\n padding: 4px 12px;\n font-size: 0.875rem;\n border-radius: 4px;\n background-color: white;\n border: 1px solid var(--apt-color-border);\n cursor: pointer;\n transition: background-color 0.15s;\n}\n.apt-btn:hover:not(:disabled) {\n background-color: #d1d5db;\n}\n.apt-btn:disabled {\n opacity: 0.5;\n cursor: not-allowed;\n}\n@media (prefers-color-scheme: dark) {\n .apt-btn {\n background-color: #444;\n color: var(--apt-color-text-dark);\n border-color: #555;\n }\n .apt-btn:hover:not(:disabled) {\n background-color: #555;\n }\n}\n.apt-sort-icon {\n display: inline-block;\n margin-left: 4px;\n width: 12px;\n height: 12px;\n opacity: 0.3;\n}\n.apt-sort-icon-active {\n opacity: 1;\n}\n.apt-column-toggle-container {\n position: relative;\n}\n.apt-column-toggle-btn {\n display: flex;\n align-items: center;\n gap: 8px;\n border-radius: 8px;\n border: 1px solid var(--apt-color-border);\n background-color: #f9fafb;\n padding: 8px 12px;\n font-size: 0.875rem;\n font-weight: 500;\n color: var(--apt-color-primary);\n box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);\n cursor: pointer;\n transition: background-color 0.15s;\n}\n.apt-column-toggle-btn:hover {\n background-color: #f3f4f6;\n}\n.apt-column-toggle-btn:focus {\n outline: none;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-btn {\n border-color: #444;\n background-color: #333;\n color: var(--apt-color-text-dark);\n }\n .apt-column-toggle-btn:hover {\n background-color: #444;\n }\n}\n.apt-column-toggle-icon {\n font-size: 1.125rem;\n}\n.apt-column-toggle-count {\n font-size: 0.75rem;\n opacity: 0.7;\n}\n.apt-column-toggle-menu {\n position: absolute;\n right: 0;\n margin-top: 8px;\n width: 288px;\n border-radius: 8px;\n border: 1px solid var(--apt-color-border);\n background-color: white;\n box-shadow: var(--apt-shadow-lg);\n z-index: 50;\n max-height: 384px;\n overflow-y: auto;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-menu {\n border-color: #444;\n background-color: #222;\n }\n}\n.apt-column-toggle-menu-inner {\n padding: 12px;\n}\n.apt-column-toggle-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n margin-bottom: 12px;\n border-bottom: 1px solid #e5e7eb;\n padding-bottom: 8px;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-header {\n border-color: #444;\n }\n}\n.apt-column-toggle-title {\n font-size: 0.875rem;\n font-weight: 600;\n color: var(--apt-color-primary);\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-title {\n color: var(--apt-color-text-dark);\n }\n}\n.apt-column-toggle-select-all {\n font-size: 0.75rem;\n color: var(--apt-color-accent);\n background: none;\n border: none;\n cursor: pointer;\n text-decoration: none;\n}\n.apt-column-toggle-select-all:hover {\n text-decoration: underline;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-select-all {\n color: var(--apt-color-accent-dark);\n }\n}\n.apt-column-toggle-list {\n display: flex;\n flex-direction: column;\n gap: 4px;\n}\n.apt-column-toggle-item {\n display: flex;\n align-items: center;\n gap: 8px;\n cursor: pointer;\n padding: 6px 8px;\n border-radius: 4px;\n transition: background-color 0.15s;\n}\n.apt-column-toggle-item:hover {\n background-color: #f3f4f6;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-item:hover {\n background-color: #333;\n }\n}\n.apt-column-toggle-checkbox {\n width: 16px;\n height: 16px;\n color: var(--apt-color-accent);\n border-radius: 12px;\n cursor: pointer;\n accent-color: var(--apt-color-accent);\n}\n.apt-column-toggle-label {\n font-size: 0.875rem;\n color: var(--apt-color-primary);\n flex: 1;\n}\n@media (prefers-color-scheme: dark) {\n .apt-column-toggle-label {\n color: var(--apt-color-text-dark);\n }\n}\n.apt-flex {\n display: flex;\n}\n.apt-items-center {\n align-items: center;\n}\n.apt-gap-2 {\n gap: 8px;\n}\n");
|
|
28
|
+
var SortIcon = ({ className }) => /* @__PURE__ */ jsx(
|
|
29
|
+
"svg",
|
|
30
|
+
{
|
|
31
|
+
className,
|
|
32
|
+
width: "12",
|
|
33
|
+
height: "12",
|
|
34
|
+
viewBox: "0 0 12 12",
|
|
35
|
+
fill: "currentColor",
|
|
36
|
+
children: /* @__PURE__ */ jsx("path", { d: "M6 3L3 6h6L6 3zM6 9l3-3H3l3 3z" })
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
var SortUpIcon = ({ className }) => /* @__PURE__ */ jsx(
|
|
40
|
+
"svg",
|
|
41
|
+
{
|
|
42
|
+
className,
|
|
43
|
+
width: "12",
|
|
44
|
+
height: "12",
|
|
45
|
+
viewBox: "0 0 12 12",
|
|
46
|
+
fill: "currentColor",
|
|
47
|
+
children: /* @__PURE__ */ jsx("path", { d: "M6 2L2 7h8L6 2z" })
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
var SortDownIcon = ({ className }) => /* @__PURE__ */ jsx(
|
|
51
|
+
"svg",
|
|
52
|
+
{
|
|
53
|
+
className,
|
|
54
|
+
width: "12",
|
|
55
|
+
height: "12",
|
|
56
|
+
viewBox: "0 0 12 12",
|
|
57
|
+
fill: "currentColor",
|
|
58
|
+
children: /* @__PURE__ */ jsx("path", { d: "M6 10l4-5H2l4 5z" })
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
var Table = ({
|
|
62
|
+
manualHeaders,
|
|
63
|
+
manualRowData,
|
|
64
|
+
initialSort,
|
|
65
|
+
height = "100%",
|
|
66
|
+
rowHeight = 40,
|
|
67
|
+
rowsPerPage = 60,
|
|
68
|
+
shouldPaginate = true,
|
|
69
|
+
rowClassName,
|
|
70
|
+
onRowClick,
|
|
71
|
+
minColWidth = 50,
|
|
72
|
+
mobileAutoSizeOnHeaderClick = false,
|
|
73
|
+
mobileBreakpoint = 768,
|
|
74
|
+
columnWidthsStorageKey,
|
|
75
|
+
rowsPerPageOptions = [20, 50, 100],
|
|
76
|
+
onRowsPerPageChange,
|
|
77
|
+
expandedRowId,
|
|
78
|
+
renderExpandedRow,
|
|
79
|
+
renderFullRow
|
|
80
|
+
}) => {
|
|
81
|
+
const [sortConfig, setSortConfig] = useState(
|
|
82
|
+
initialSort || null
|
|
83
|
+
);
|
|
84
|
+
const [currentPage, setCurrentPage] = useState(1);
|
|
85
|
+
const [columnWidths, setColumnWidths] = useState({});
|
|
86
|
+
const [expandedColumns, setExpandedColumns] = useState(
|
|
87
|
+
() => /* @__PURE__ */ new Set()
|
|
88
|
+
);
|
|
89
|
+
const initialColumnWidthsRef = useRef({});
|
|
90
|
+
const tableRef = useRef(null);
|
|
91
|
+
const textMeasureContextRef = useRef(null);
|
|
92
|
+
const [isMobile, setIsMobile] = useState(() => {
|
|
93
|
+
if (typeof window === "undefined") return false;
|
|
94
|
+
return window.innerWidth < mobileBreakpoint;
|
|
95
|
+
});
|
|
96
|
+
const headers = useMemo(() => manualHeaders || [], [manualHeaders]);
|
|
97
|
+
const loadStoredColumnWidths = (fallback) => {
|
|
98
|
+
if (!columnWidthsStorageKey || typeof window === "undefined") {
|
|
99
|
+
return fallback;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
const raw = window.localStorage.getItem(columnWidthsStorageKey);
|
|
103
|
+
if (!raw) return fallback;
|
|
104
|
+
const parsed = JSON.parse(raw);
|
|
105
|
+
if (!parsed || typeof parsed !== "object") return fallback;
|
|
106
|
+
const next = { ...fallback };
|
|
107
|
+
Object.keys(next).forEach((key) => {
|
|
108
|
+
const val = parsed[key];
|
|
109
|
+
if (typeof val === "number" || typeof val === "string") {
|
|
110
|
+
next[key] = val;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return next;
|
|
114
|
+
} catch {
|
|
115
|
+
return fallback;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const initialWidths = {};
|
|
120
|
+
headers.forEach((header) => {
|
|
121
|
+
initialWidths[header.accessor] = header.width;
|
|
122
|
+
});
|
|
123
|
+
initialColumnWidthsRef.current = initialWidths;
|
|
124
|
+
setColumnWidths(
|
|
125
|
+
loadStoredColumnWidths(initialWidths)
|
|
126
|
+
);
|
|
127
|
+
setExpandedColumns(/* @__PURE__ */ new Set());
|
|
128
|
+
}, [headers, columnWidthsStorageKey]);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
if (!columnWidthsStorageKey || typeof window === "undefined") return;
|
|
131
|
+
if (Object.keys(columnWidths).length === 0) return;
|
|
132
|
+
window.localStorage.setItem(
|
|
133
|
+
columnWidthsStorageKey,
|
|
134
|
+
JSON.stringify(columnWidths)
|
|
135
|
+
);
|
|
136
|
+
}, [columnWidths, columnWidthsStorageKey]);
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (typeof window === "undefined") return void 0;
|
|
139
|
+
const handleResize = () => setIsMobile(window.innerWidth < mobileBreakpoint);
|
|
140
|
+
window.addEventListener("resize", handleResize);
|
|
141
|
+
return () => window.removeEventListener("resize", handleResize);
|
|
142
|
+
}, [mobileBreakpoint]);
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
setCurrentPage(1);
|
|
145
|
+
}, [rowsPerPage]);
|
|
146
|
+
const measureTextWidth = (text) => {
|
|
147
|
+
if (typeof document === "undefined") return String(text ?? "").length * 8;
|
|
148
|
+
const ctx = textMeasureContextRef.current || (() => {
|
|
149
|
+
const canvas = document.createElement("canvas");
|
|
150
|
+
const context = canvas.getContext("2d");
|
|
151
|
+
if (tableRef.current && context) {
|
|
152
|
+
const font = window.getComputedStyle(tableRef.current).font;
|
|
153
|
+
context.font = font || "12px sans-serif";
|
|
154
|
+
}
|
|
155
|
+
textMeasureContextRef.current = context;
|
|
156
|
+
return context;
|
|
157
|
+
})();
|
|
158
|
+
return ctx ? ctx.measureText(String(text ?? "")).width : 0;
|
|
159
|
+
};
|
|
160
|
+
const parseWidthValue = (value, fallback) => {
|
|
161
|
+
if (typeof value === "number" && !Number.isNaN(value)) return value;
|
|
162
|
+
if (typeof value === "string") {
|
|
163
|
+
const parsed = parseFloat(value);
|
|
164
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
165
|
+
}
|
|
166
|
+
return fallback;
|
|
167
|
+
};
|
|
168
|
+
const rows = useMemo(() => {
|
|
169
|
+
if (!Array.isArray(manualRowData)) return [];
|
|
170
|
+
const src = manualRowData.filter(Boolean).filter((r) => typeof r === "object");
|
|
171
|
+
const makeSafeId = (full, idx) => {
|
|
172
|
+
const rawId = full?.id;
|
|
173
|
+
const v1 = rawId !== void 0 && rawId !== null && String(rawId).trim() !== "" ? String(rawId).trim() : "";
|
|
174
|
+
const v2 = [
|
|
175
|
+
full?.order_id,
|
|
176
|
+
full?.bom_id,
|
|
177
|
+
full?.bom_workstation_status_id,
|
|
178
|
+
full?.bom_data_id,
|
|
179
|
+
full?.unique_task_info_id,
|
|
180
|
+
full?.order_data_id
|
|
181
|
+
].filter((v) => v !== void 0 && v !== null && String(v) !== "").join("-");
|
|
182
|
+
return v1 || `${v2}-row${idx}`;
|
|
183
|
+
};
|
|
184
|
+
return src.map((full, idx) => {
|
|
185
|
+
const id = makeSafeId(full, idx);
|
|
186
|
+
const base = { id, "#": idx + 1, _meta: full };
|
|
187
|
+
headers.forEach((h) => {
|
|
188
|
+
if (!["#", "details_meta"].includes(h.accessor)) {
|
|
189
|
+
base[h.accessor] = full?.[h.accessor] ?? "";
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return base;
|
|
193
|
+
});
|
|
194
|
+
}, [manualRowData, headers]);
|
|
195
|
+
const sortedRows = useMemo(() => {
|
|
196
|
+
if (!sortConfig || !sortConfig.key) {
|
|
197
|
+
return rows;
|
|
198
|
+
}
|
|
199
|
+
const sortableRows = [...rows];
|
|
200
|
+
sortableRows.sort((a, b) => {
|
|
201
|
+
const aVal = a[sortConfig.key];
|
|
202
|
+
const bVal = b[sortConfig.key];
|
|
203
|
+
if (aVal < bVal) {
|
|
204
|
+
return sortConfig.direction === "asc" ? -1 : 1;
|
|
205
|
+
}
|
|
206
|
+
if (aVal > bVal) {
|
|
207
|
+
return sortConfig.direction === "asc" ? 1 : -1;
|
|
208
|
+
}
|
|
209
|
+
return 0;
|
|
210
|
+
});
|
|
211
|
+
return sortableRows;
|
|
212
|
+
}, [rows, sortConfig]);
|
|
213
|
+
const effectiveShouldPaginate = shouldPaginate && sortedRows.length > rowsPerPage;
|
|
214
|
+
const totalPages = effectiveShouldPaginate ? Math.ceil(sortedRows.length / rowsPerPage) : 1;
|
|
215
|
+
if (currentPage > totalPages && totalPages > 0) {
|
|
216
|
+
setCurrentPage(1);
|
|
217
|
+
}
|
|
218
|
+
const paginatedRows = useMemo(() => {
|
|
219
|
+
if (!effectiveShouldPaginate) return sortedRows;
|
|
220
|
+
const startIndex = (currentPage - 1) * rowsPerPage;
|
|
221
|
+
return sortedRows.slice(startIndex, startIndex + rowsPerPage);
|
|
222
|
+
}, [sortedRows, currentPage, rowsPerPage, effectiveShouldPaginate]);
|
|
223
|
+
const handleSort = (key, isSortable) => {
|
|
224
|
+
if (!isSortable) return;
|
|
225
|
+
let direction = "asc";
|
|
226
|
+
if (sortConfig && sortConfig.key === key && sortConfig.direction === "asc") {
|
|
227
|
+
direction = "desc";
|
|
228
|
+
}
|
|
229
|
+
setSortConfig({ key, direction });
|
|
230
|
+
setCurrentPage(1);
|
|
231
|
+
};
|
|
232
|
+
const autoSizeColumn = (colIndex, header) => {
|
|
233
|
+
if (!tableRef.current) return;
|
|
234
|
+
const tableEl = tableRef.current;
|
|
235
|
+
let maxWidth = measureTextWidth(header?.label || "");
|
|
236
|
+
const collectCellWidth = (cell) => {
|
|
237
|
+
if (!cell) return;
|
|
238
|
+
const scrollWidth = cell.scrollWidth || cell.offsetWidth || 0;
|
|
239
|
+
const style = window.getComputedStyle(cell);
|
|
240
|
+
const borderWidth = parseFloat(style.borderLeftWidth || "0") + parseFloat(style.borderRightWidth || "0");
|
|
241
|
+
maxWidth = Math.max(maxWidth, Math.ceil(scrollWidth + borderWidth));
|
|
242
|
+
};
|
|
243
|
+
const headerCell = tableEl.tHead?.rows?.[0]?.cells?.[colIndex] || tableEl.querySelector(`thead th:nth-child(${colIndex + 1})`);
|
|
244
|
+
collectCellWidth(headerCell);
|
|
245
|
+
const bodyRows = tableEl.tBodies?.[0]?.rows || [];
|
|
246
|
+
for (const row of bodyRows) {
|
|
247
|
+
collectCellWidth(row.cells?.[colIndex]);
|
|
248
|
+
}
|
|
249
|
+
rows.forEach((r) => {
|
|
250
|
+
maxWidth = Math.max(
|
|
251
|
+
maxWidth,
|
|
252
|
+
Math.ceil(measureTextWidth(r[header.accessor]))
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
const minWidthPx = parseWidthValue(
|
|
256
|
+
header?.minWidth,
|
|
257
|
+
parseWidthValue(minColWidth, 50)
|
|
258
|
+
);
|
|
259
|
+
const finalWidth = Math.max(maxWidth + 12, minWidthPx);
|
|
260
|
+
setColumnWidths((prev) => ({
|
|
261
|
+
...prev,
|
|
262
|
+
[header.accessor]: `${finalWidth}px`
|
|
263
|
+
}));
|
|
264
|
+
};
|
|
265
|
+
const resetColumnWidth = (accessor) => {
|
|
266
|
+
const regularWidth = initialColumnWidthsRef.current[accessor];
|
|
267
|
+
setColumnWidths((prev) => {
|
|
268
|
+
if (regularWidth === void 0) {
|
|
269
|
+
const next = { ...prev };
|
|
270
|
+
delete next[accessor];
|
|
271
|
+
return next;
|
|
272
|
+
}
|
|
273
|
+
return { ...prev, [accessor]: regularWidth };
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
const handleHeaderClick = (header, colIndex) => {
|
|
277
|
+
if (mobileAutoSizeOnHeaderClick && isMobile) {
|
|
278
|
+
const isExpanded = expandedColumns.has(header.accessor);
|
|
279
|
+
if (isExpanded) {
|
|
280
|
+
resetColumnWidth(header.accessor);
|
|
281
|
+
setExpandedColumns((prev) => {
|
|
282
|
+
const next = new Set(prev);
|
|
283
|
+
next.delete(header.accessor);
|
|
284
|
+
return next;
|
|
285
|
+
});
|
|
286
|
+
} else {
|
|
287
|
+
autoSizeColumn(colIndex, header);
|
|
288
|
+
setExpandedColumns((prev) => {
|
|
289
|
+
const next = new Set(prev);
|
|
290
|
+
next.add(header.accessor);
|
|
291
|
+
return next;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
handleSort(header.accessor, header.isSortable);
|
|
297
|
+
};
|
|
298
|
+
const getSortIcon = (key) => {
|
|
299
|
+
if (mobileAutoSizeOnHeaderClick && isMobile) return null;
|
|
300
|
+
if (!sortConfig || sortConfig.key !== key) {
|
|
301
|
+
return /* @__PURE__ */ jsx(SortIcon, { className: "apt-sort-icon" });
|
|
302
|
+
}
|
|
303
|
+
return sortConfig.direction === "asc" ? /* @__PURE__ */ jsx(SortUpIcon, { className: "apt-sort-icon apt-sort-icon-active" }) : /* @__PURE__ */ jsx(SortDownIcon, { className: "apt-sort-icon apt-sort-icon-active" });
|
|
304
|
+
};
|
|
305
|
+
const handleMouseDown = (e, accessor) => {
|
|
306
|
+
e.preventDefault();
|
|
307
|
+
const startX = e.clientX;
|
|
308
|
+
const th = e.target.closest("th");
|
|
309
|
+
if (!th) return;
|
|
310
|
+
const header = headers.find((h) => h.accessor === accessor);
|
|
311
|
+
const minResizeWidth = parseWidthValue(header?.minWidth, minColWidth);
|
|
312
|
+
const startWidth = th.offsetWidth;
|
|
313
|
+
document.body.style.cursor = "col-resize";
|
|
314
|
+
document.body.style.userSelect = "none";
|
|
315
|
+
const handleMouseMove = (moveEvent) => {
|
|
316
|
+
const deltaX = moveEvent.clientX - startX;
|
|
317
|
+
const newWidth = startWidth + deltaX;
|
|
318
|
+
const finalWidth = Math.max(newWidth, minResizeWidth);
|
|
319
|
+
setColumnWidths((prev) => ({
|
|
320
|
+
...prev,
|
|
321
|
+
[accessor]: `${finalWidth}px`
|
|
322
|
+
}));
|
|
323
|
+
};
|
|
324
|
+
const handleMouseUp = () => {
|
|
325
|
+
document.body.style.cursor = "";
|
|
326
|
+
document.body.style.userSelect = "";
|
|
327
|
+
document.removeEventListener("mousemove", handleMouseMove);
|
|
328
|
+
document.removeEventListener("mouseup", handleMouseUp);
|
|
329
|
+
};
|
|
330
|
+
document.addEventListener("mousemove", handleMouseMove);
|
|
331
|
+
document.addEventListener("mouseup", handleMouseUp);
|
|
332
|
+
};
|
|
333
|
+
const containerStyle = {
|
|
334
|
+
height: sortedRows.length === 0 ? height : !effectiveShouldPaginate ? "auto" : height,
|
|
335
|
+
maxHeight: !effectiveShouldPaginate ? height : void 0
|
|
336
|
+
};
|
|
337
|
+
return /* @__PURE__ */ jsx("div", { className: "apt-table-container", style: containerStyle, children: sortedRows.length === 0 ? /* @__PURE__ */ jsx("div", { className: "apt-empty-state", children: "No rows to display." }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
338
|
+
/* @__PURE__ */ jsx("div", { className: "apt-scroll-area", children: /* @__PURE__ */ jsxs("table", { className: "apt-table", ref: tableRef, children: [
|
|
339
|
+
/* @__PURE__ */ jsx("colgroup", { children: headers.map((header) => /* @__PURE__ */ jsx(
|
|
340
|
+
"col",
|
|
341
|
+
{
|
|
342
|
+
style: {
|
|
343
|
+
width: columnWidths[header.accessor],
|
|
344
|
+
minWidth: `${parseWidthValue(
|
|
345
|
+
header.minWidth,
|
|
346
|
+
minColWidth
|
|
347
|
+
)}px`
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
`col-${header.accessor}`
|
|
351
|
+
)) }),
|
|
352
|
+
/* @__PURE__ */ jsx("thead", { className: "apt-thead", children: /* @__PURE__ */ jsx("tr", { children: headers.map((header, idx) => {
|
|
353
|
+
const isHeaderClickable = mobileAutoSizeOnHeaderClick && isMobile || header.isSortable;
|
|
354
|
+
return /* @__PURE__ */ jsxs(
|
|
355
|
+
"th",
|
|
356
|
+
{
|
|
357
|
+
onClick: () => handleHeaderClick(header, idx),
|
|
358
|
+
className: `apt-th ${isHeaderClickable ? "apt-th-sortable" : ""}`,
|
|
359
|
+
children: [
|
|
360
|
+
/* @__PURE__ */ jsxs("div", { className: "apt-th-content", children: [
|
|
361
|
+
header.label,
|
|
362
|
+
header.isSortable && !(mobileAutoSizeOnHeaderClick && isMobile) && getSortIcon(header.accessor)
|
|
363
|
+
] }),
|
|
364
|
+
/* @__PURE__ */ jsx(
|
|
365
|
+
"div",
|
|
366
|
+
{
|
|
367
|
+
className: "apt-resizer",
|
|
368
|
+
onMouseDown: (e) => {
|
|
369
|
+
e.stopPropagation();
|
|
370
|
+
handleMouseDown(e, header.accessor);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
)
|
|
374
|
+
]
|
|
375
|
+
},
|
|
376
|
+
header.accessor
|
|
377
|
+
);
|
|
378
|
+
}) }) }),
|
|
379
|
+
/* @__PURE__ */ jsx("tbody", { className: "apt-tbody", children: paginatedRows.map((row) => {
|
|
380
|
+
const isExpanded = expandedRowId === row.id;
|
|
381
|
+
const isFullRow = typeof renderFullRow === "function" && row?._meta?.fullRow;
|
|
382
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
383
|
+
isFullRow ? /* @__PURE__ */ jsx(
|
|
384
|
+
"tr",
|
|
385
|
+
{
|
|
386
|
+
className: `apt-row ${rowClassName ? rowClassName(row) : ""}`,
|
|
387
|
+
onClick: () => onRowClick && onRowClick(row),
|
|
388
|
+
children: /* @__PURE__ */ jsx(
|
|
389
|
+
"td",
|
|
390
|
+
{
|
|
391
|
+
colSpan: headers.length,
|
|
392
|
+
className: "apt-td",
|
|
393
|
+
style: { padding: 0 },
|
|
394
|
+
children: renderFullRow(row)
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
) : /* @__PURE__ */ jsx(
|
|
399
|
+
"tr",
|
|
400
|
+
{
|
|
401
|
+
className: `apt-row ${rowClassName ? rowClassName(row) : ""}`,
|
|
402
|
+
onClick: () => onRowClick && onRowClick(row),
|
|
403
|
+
style: { height: `${rowHeight}px` },
|
|
404
|
+
children: headers.map((header) => {
|
|
405
|
+
const value = row[header.accessor];
|
|
406
|
+
const cellContent = header.cellRenderer ? header.cellRenderer({ row, value }) : value;
|
|
407
|
+
return /* @__PURE__ */ jsx(
|
|
408
|
+
"td",
|
|
409
|
+
{
|
|
410
|
+
className: header.accessor === "actions" ? "apt-td-actions" : "apt-td",
|
|
411
|
+
children: header.accessor === "actions" ? /* @__PURE__ */ jsx("div", { onClick: (e) => e.stopPropagation(), children: cellContent }) : cellContent
|
|
412
|
+
},
|
|
413
|
+
`${row.id}-${header.accessor}`
|
|
414
|
+
);
|
|
415
|
+
})
|
|
416
|
+
}
|
|
417
|
+
),
|
|
418
|
+
isExpanded && typeof renderExpandedRow === "function" && /* @__PURE__ */ jsx("tr", { className: "apt-row-expanded", children: /* @__PURE__ */ jsx(
|
|
419
|
+
"td",
|
|
420
|
+
{
|
|
421
|
+
colSpan: headers.length,
|
|
422
|
+
className: "apt-td",
|
|
423
|
+
style: { padding: 0 },
|
|
424
|
+
children: renderExpandedRow(row)
|
|
425
|
+
}
|
|
426
|
+
) })
|
|
427
|
+
] }, row.id);
|
|
428
|
+
}) })
|
|
429
|
+
] }) }),
|
|
430
|
+
shouldPaginate && totalPages > 1 || Array.isArray(rowsPerPageOptions) && rowsPerPageOptions.length ? /* @__PURE__ */ jsxs("div", { className: "apt-footer", children: [
|
|
431
|
+
/* @__PURE__ */ jsx("div", { className: "apt-footer-left", children: Array.isArray(rowsPerPageOptions) && rowsPerPageOptions.length > 0 && onRowsPerPageChange && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
432
|
+
/* @__PURE__ */ jsx("label", { className: "apt-rows-label", children: "Rows" }),
|
|
433
|
+
/* @__PURE__ */ jsx(
|
|
434
|
+
"select",
|
|
435
|
+
{
|
|
436
|
+
value: rowsPerPage,
|
|
437
|
+
onChange: (e) => onRowsPerPageChange(Number(e.target.value)),
|
|
438
|
+
className: "apt-rows-select",
|
|
439
|
+
children: rowsPerPageOptions.map((opt) => /* @__PURE__ */ jsx("option", { value: opt, children: opt }, opt))
|
|
440
|
+
}
|
|
441
|
+
)
|
|
442
|
+
] }) }),
|
|
443
|
+
/* @__PURE__ */ jsx("div", { className: "apt-footer-center", children: shouldPaginate && totalPages > 1 ? `Page ${currentPage} of ${totalPages}` : "" }),
|
|
444
|
+
/* @__PURE__ */ jsx("div", { className: "apt-footer-right", children: shouldPaginate && totalPages > 1 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
445
|
+
/* @__PURE__ */ jsx(
|
|
446
|
+
"button",
|
|
447
|
+
{
|
|
448
|
+
onClick: () => setCurrentPage((p) => Math.max(1, p - 1)),
|
|
449
|
+
disabled: currentPage === 1,
|
|
450
|
+
className: "apt-btn",
|
|
451
|
+
children: "Previous"
|
|
452
|
+
}
|
|
453
|
+
),
|
|
454
|
+
/* @__PURE__ */ jsx(
|
|
455
|
+
"button",
|
|
456
|
+
{
|
|
457
|
+
onClick: () => setCurrentPage((p) => Math.min(totalPages, p + 1)),
|
|
458
|
+
disabled: currentPage === totalPages,
|
|
459
|
+
className: "apt-btn",
|
|
460
|
+
children: "Next"
|
|
461
|
+
}
|
|
462
|
+
)
|
|
463
|
+
] }) : null })
|
|
464
|
+
] }) : null
|
|
465
|
+
] }) });
|
|
466
|
+
};
|
|
467
|
+
var Table_default = Table;
|
|
468
|
+
var ViewColumnIcon = ({ className }) => /* @__PURE__ */ jsx(
|
|
469
|
+
"svg",
|
|
470
|
+
{
|
|
471
|
+
className,
|
|
472
|
+
width: "18",
|
|
473
|
+
height: "18",
|
|
474
|
+
viewBox: "0 0 24 24",
|
|
475
|
+
fill: "currentColor",
|
|
476
|
+
children: /* @__PURE__ */ jsx("path", { d: "M10 18h5V5h-5v13zm-6 0h5V5H4v13zM16 5v13h5V5h-5z" })
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
var ColumnVisibilityToggle = ({
|
|
480
|
+
availableColumns,
|
|
481
|
+
visibleColumns,
|
|
482
|
+
onColumnsChange,
|
|
483
|
+
storageKey
|
|
484
|
+
}) => {
|
|
485
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
486
|
+
const menuRef = useRef(null);
|
|
487
|
+
useEffect(() => {
|
|
488
|
+
const handleClickOutside = (event) => {
|
|
489
|
+
if (menuRef.current && !menuRef.current.contains(event.target)) {
|
|
490
|
+
setIsOpen(false);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
if (isOpen) {
|
|
494
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
495
|
+
}
|
|
496
|
+
return () => {
|
|
497
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
498
|
+
};
|
|
499
|
+
}, [isOpen]);
|
|
500
|
+
const saveToStorage = (columns) => {
|
|
501
|
+
if (storageKey && typeof window !== "undefined") {
|
|
502
|
+
try {
|
|
503
|
+
window.localStorage.setItem(storageKey, JSON.stringify(columns));
|
|
504
|
+
} catch (error) {
|
|
505
|
+
console.warn(
|
|
506
|
+
"Failed to save column visibility to localStorage:",
|
|
507
|
+
error
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
const handleToggleColumn = (columnKey) => {
|
|
513
|
+
const newVisibleColumns = visibleColumns.includes(columnKey) ? visibleColumns.filter((key) => key !== columnKey) : [...visibleColumns, columnKey];
|
|
514
|
+
saveToStorage(newVisibleColumns);
|
|
515
|
+
onColumnsChange(newVisibleColumns);
|
|
516
|
+
};
|
|
517
|
+
const handleSelectAll = () => {
|
|
518
|
+
const allKeys = availableColumns.map((col) => col.key);
|
|
519
|
+
saveToStorage(allKeys);
|
|
520
|
+
onColumnsChange(allKeys);
|
|
521
|
+
};
|
|
522
|
+
return /* @__PURE__ */ jsxs("div", { className: "apt-column-toggle-container", ref: menuRef, children: [
|
|
523
|
+
/* @__PURE__ */ jsxs(
|
|
524
|
+
"button",
|
|
525
|
+
{
|
|
526
|
+
type: "button",
|
|
527
|
+
onClick: () => setIsOpen(!isOpen),
|
|
528
|
+
className: "apt-column-toggle-btn",
|
|
529
|
+
"aria-label": "Toggle column visibility",
|
|
530
|
+
"aria-expanded": isOpen,
|
|
531
|
+
children: [
|
|
532
|
+
/* @__PURE__ */ jsx(ViewColumnIcon, { className: "apt-column-toggle-icon" }),
|
|
533
|
+
/* @__PURE__ */ jsx("span", { children: "Columns" }),
|
|
534
|
+
/* @__PURE__ */ jsxs("span", { className: "apt-column-toggle-count", children: [
|
|
535
|
+
"(",
|
|
536
|
+
visibleColumns.length,
|
|
537
|
+
")"
|
|
538
|
+
] })
|
|
539
|
+
]
|
|
540
|
+
}
|
|
541
|
+
),
|
|
542
|
+
isOpen && /* @__PURE__ */ jsx("div", { className: "apt-column-toggle-menu", children: /* @__PURE__ */ jsxs("div", { className: "apt-column-toggle-menu-inner", children: [
|
|
543
|
+
/* @__PURE__ */ jsxs("div", { className: "apt-column-toggle-header", children: [
|
|
544
|
+
/* @__PURE__ */ jsx("h3", { className: "apt-column-toggle-title", children: "Toggle Columns" }),
|
|
545
|
+
/* @__PURE__ */ jsx(
|
|
546
|
+
"button",
|
|
547
|
+
{
|
|
548
|
+
type: "button",
|
|
549
|
+
onClick: handleSelectAll,
|
|
550
|
+
className: "apt-column-toggle-select-all",
|
|
551
|
+
children: "Select All"
|
|
552
|
+
}
|
|
553
|
+
)
|
|
554
|
+
] }),
|
|
555
|
+
/* @__PURE__ */ jsx("div", { className: "apt-column-toggle-list", children: availableColumns.map((column) => {
|
|
556
|
+
const isVisible = visibleColumns.includes(column.key);
|
|
557
|
+
return /* @__PURE__ */ jsxs("label", { className: "apt-column-toggle-item", children: [
|
|
558
|
+
/* @__PURE__ */ jsx(
|
|
559
|
+
"input",
|
|
560
|
+
{
|
|
561
|
+
type: "checkbox",
|
|
562
|
+
checked: isVisible,
|
|
563
|
+
onChange: () => handleToggleColumn(column.key),
|
|
564
|
+
className: "apt-column-toggle-checkbox"
|
|
565
|
+
}
|
|
566
|
+
),
|
|
567
|
+
/* @__PURE__ */ jsx("span", { className: "apt-column-toggle-label", children: column.label })
|
|
568
|
+
] }, column.key);
|
|
569
|
+
}) })
|
|
570
|
+
] }) })
|
|
571
|
+
] });
|
|
572
|
+
};
|
|
573
|
+
var ColumnVisibilityToggle_default = ColumnVisibilityToggle;
|
|
574
|
+
|
|
575
|
+
export { ColumnVisibilityToggle_default as ColumnVisibilityToggle, Table_default as Table };
|
|
576
|
+
//# sourceMappingURL=index.mjs.map
|
|
577
|
+
//# sourceMappingURL=index.mjs.map
|