@jinaan/uikit 0.1.42 → 0.1.43
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/dist/data-table.d.ts +15 -1
- package/dist/data-table.d.ts.map +1 -1
- package/dist/index.js +284 -278
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/data-table.tsx +19 -2
package/package.json
CHANGED
package/src/data-table.tsx
CHANGED
|
@@ -141,6 +141,14 @@ interface DataTableProps<T> {
|
|
|
141
141
|
onRowClick?: (row: T) => void;
|
|
142
142
|
|
|
143
143
|
defaultSort?: { id: string; dir: "asc" | "desc" };
|
|
144
|
+
/**
|
|
145
|
+
* Controlled sort state. Pass together with `onSortChange`: header caret
|
|
146
|
+
* clicks then report through the callback instead of mutating internal
|
|
147
|
+
* state, so an external control (e.g. a toolbar "Sort by" select) and the
|
|
148
|
+
* headers can drive one shared state.
|
|
149
|
+
*/
|
|
150
|
+
sort?: { id: string; dir: "asc" | "desc" };
|
|
151
|
+
onSortChange?: (sort: { id: string; dir: "asc" | "desc" } | null) => void;
|
|
144
152
|
pageSize?: number;
|
|
145
153
|
pageSizeOptions?: number[];
|
|
146
154
|
|
|
@@ -223,6 +231,8 @@ export function DataTable<T>({
|
|
|
223
231
|
rowActions,
|
|
224
232
|
onRowClick,
|
|
225
233
|
defaultSort,
|
|
234
|
+
sort: sortProp,
|
|
235
|
+
onSortChange,
|
|
226
236
|
pageSize: initialPageSize = 25,
|
|
227
237
|
pageSizeOptions = [10, 25, 50, 100],
|
|
228
238
|
pagination = "auto",
|
|
@@ -240,9 +250,15 @@ export function DataTable<T>({
|
|
|
240
250
|
// Column order and runtime pins are per-mount, like `hidden`.
|
|
241
251
|
const [order, setOrder] = useState<string[] | null>(null);
|
|
242
252
|
const [dragId, setDragId] = useState<string | null>(null);
|
|
243
|
-
const [
|
|
253
|
+
const [internalSort, setInternalSort] = useState<{ id: string; dir: "asc" | "desc" } | null>(
|
|
244
254
|
defaultSort ?? null,
|
|
245
255
|
);
|
|
256
|
+
// Controlled when the caller passes onSortChange; uncontrolled otherwise.
|
|
257
|
+
const sort = onSortChange ? sortProp ?? null : internalSort;
|
|
258
|
+
const setSort = (next: { id: string; dir: "asc" | "desc" } | null) => {
|
|
259
|
+
if (onSortChange) onSortChange(next);
|
|
260
|
+
else setInternalSort(next);
|
|
261
|
+
};
|
|
246
262
|
const [page, setPage] = useState(0);
|
|
247
263
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
|
248
264
|
const [selected, setSelected] = useState<Set<string>>(new Set());
|
|
@@ -323,7 +339,8 @@ export function DataTable<T>({
|
|
|
323
339
|
|
|
324
340
|
const toggleSort = (col: Column<T>) => {
|
|
325
341
|
if (!col.sortable) return;
|
|
326
|
-
|
|
342
|
+
const prev = sort;
|
|
343
|
+
setSort(
|
|
327
344
|
prev?.id === col.id
|
|
328
345
|
? prev.dir === "asc"
|
|
329
346
|
? { id: col.id, dir: "desc" }
|