@speles7172/controls 0.5.1 → 0.6.1

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.
Files changed (59) hide show
  1. package/README.md +101 -1
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/ChipRow.d.ts +50 -0
  4. package/dist/ChipRow.d.ts.map +1 -0
  5. package/dist/ChipRow.js +35 -0
  6. package/dist/ChipRow.js.map +1 -0
  7. package/dist/DataTable.d.ts +123 -2
  8. package/dist/DataTable.d.ts.map +1 -1
  9. package/dist/DataTable.js +173 -36
  10. package/dist/DataTable.js.map +1 -1
  11. package/dist/DateRangeFilter.d.ts +21 -1
  12. package/dist/DateRangeFilter.d.ts.map +1 -1
  13. package/dist/DateRangeFilter.js +90 -19
  14. package/dist/DateRangeFilter.js.map +1 -1
  15. package/dist/ItemList.d.ts +54 -0
  16. package/dist/ItemList.d.ts.map +1 -0
  17. package/dist/ItemList.js +29 -0
  18. package/dist/ItemList.js.map +1 -0
  19. package/dist/Modal.d.ts +46 -1
  20. package/dist/Modal.d.ts.map +1 -1
  21. package/dist/Modal.js +155 -12
  22. package/dist/Modal.js.map +1 -1
  23. package/dist/Pagination.d.ts +25 -0
  24. package/dist/Pagination.d.ts.map +1 -0
  25. package/dist/Pagination.js +19 -0
  26. package/dist/Pagination.js.map +1 -0
  27. package/dist/SearchableMultiSelect.d.ts.map +1 -1
  28. package/dist/SearchableMultiSelect.js +1 -1
  29. package/dist/SearchableMultiSelect.js.map +1 -1
  30. package/dist/Toast.d.ts +36 -0
  31. package/dist/Toast.d.ts.map +1 -0
  32. package/dist/Toast.js +68 -0
  33. package/dist/Toast.js.map +1 -0
  34. package/dist/csv.d.ts +21 -0
  35. package/dist/csv.d.ts.map +1 -0
  36. package/dist/csv.js +56 -0
  37. package/dist/csv.js.map +1 -0
  38. package/dist/icons.d.ts +1 -1
  39. package/dist/icons.d.ts.map +1 -1
  40. package/dist/icons.js +2 -2
  41. package/dist/icons.js.map +1 -1
  42. package/dist/index.d.ts +11 -3
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +10 -2
  45. package/dist/index.js.map +1 -1
  46. package/dist/styles.css +734 -21
  47. package/dist/useDensity.d.ts +24 -0
  48. package/dist/useDensity.d.ts.map +1 -0
  49. package/dist/useDensity.js +61 -0
  50. package/dist/useDensity.js.map +1 -0
  51. package/dist/usePagination.d.ts +25 -0
  52. package/dist/usePagination.d.ts.map +1 -0
  53. package/dist/usePagination.js +59 -0
  54. package/dist/usePagination.js.map +1 -0
  55. package/dist/useTableSort.d.ts +50 -0
  56. package/dist/useTableSort.d.ts.map +1 -0
  57. package/dist/useTableSort.js +73 -0
  58. package/dist/useTableSort.js.map +1 -0
  59. package/package.json +2 -2
@@ -0,0 +1,24 @@
1
+ /** How tightly a table packs its rows. */
2
+ export type Density = 'comfortable' | 'compact';
3
+ export interface DensityControl {
4
+ density: Density;
5
+ /** `true` when the reader has chosen the tighter row height. */
6
+ compact: boolean;
7
+ setDensity(density: Density): void;
8
+ /** Flip between comfortable and compact. */
9
+ toggle(): void;
10
+ }
11
+ /**
12
+ * The reader's row-density choice, remembered in `localStorage` like the page
13
+ * size — set it once and every list that offers the control honours it, on this
14
+ * screen and the next.
15
+ *
16
+ * The write fans out to every mounted table through a module-level subscriber
17
+ * set, carrying the chosen value: a `storage` event would keep two *tabs* in
18
+ * step but says nothing to the other tables in this one, and re-reading storage
19
+ * on each change would drop the choice whenever the write failed. Passing the
20
+ * value instead means a blocked or full store costs only the memory of the
21
+ * choice, not the toggle itself.
22
+ */
23
+ export declare function useDensity(): DensityControl;
24
+ //# sourceMappingURL=useDensity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDensity.d.ts","sourceRoot":"","sources":["../src/useDensity.ts"],"names":[],"mappings":"AAEA,0CAA0C;AAC1C,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,SAAS,CAAC;AAkChD,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IACnC,4CAA4C;IAC5C,MAAM,IAAI,IAAI,CAAC;CAChB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,IAAI,cAAc,CAiB3C"}
@@ -0,0 +1,61 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ /** One key for every list, so the reader's chosen density follows them across screens. */
3
+ const STORAGE_KEY = 'ctl:table:density';
4
+ /** In-document subscribers, so a change on one table moves every other on the page. */
5
+ const listeners = new Set();
6
+ function readStored() {
7
+ try {
8
+ const raw = localStorage.getItem(STORAGE_KEY);
9
+ if (raw === 'compact' || raw === 'comfortable')
10
+ return raw;
11
+ }
12
+ catch {
13
+ /* Private mode or blocked storage: comfortable is a fine default. */
14
+ }
15
+ return 'comfortable';
16
+ }
17
+ function applyDensity(density) {
18
+ // Try to remember it — but the choice takes effect this session either way, so
19
+ // a blocked or full `localStorage` costs only the memory of it, never the
20
+ // toggle. (Persist first, then fan out; the fan-out carries the value so a
21
+ // failed write here cannot strand the tables on the old one.)
22
+ try {
23
+ localStorage.setItem(STORAGE_KEY, density);
24
+ }
25
+ catch {
26
+ /* Not remembering the choice is not worth failing over. */
27
+ }
28
+ // Fan the new value out to every mounted table so a page's lists move
29
+ // together — carrying the value, not a signal to re-read storage (which a
30
+ // failed write above would leave on the old value).
31
+ for (const listener of listeners)
32
+ listener(density);
33
+ }
34
+ /**
35
+ * The reader's row-density choice, remembered in `localStorage` like the page
36
+ * size — set it once and every list that offers the control honours it, on this
37
+ * screen and the next.
38
+ *
39
+ * The write fans out to every mounted table through a module-level subscriber
40
+ * set, carrying the chosen value: a `storage` event would keep two *tabs* in
41
+ * step but says nothing to the other tables in this one, and re-reading storage
42
+ * on each change would drop the choice whenever the write failed. Passing the
43
+ * value instead means a blocked or full store costs only the memory of the
44
+ * choice, not the toggle itself.
45
+ */
46
+ export function useDensity() {
47
+ const [density, setDensity] = useState(readStored);
48
+ useEffect(() => {
49
+ const listener = (next) => setDensity(next);
50
+ listeners.add(listener);
51
+ return () => {
52
+ listeners.delete(listener);
53
+ };
54
+ }, []);
55
+ const set = useCallback((next) => applyDensity(next), []);
56
+ // Flip from what this table currently shows; a re-render (from the fan-out
57
+ // above) refreshes the closure before the next click.
58
+ const toggle = useCallback(() => applyDensity(density === 'compact' ? 'comfortable' : 'compact'), [density]);
59
+ return { density, compact: density === 'compact', setDensity: set, toggle };
60
+ }
61
+ //# sourceMappingURL=useDensity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDensity.js","sourceRoot":"","sources":["../src/useDensity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAKzD,0FAA0F;AAC1F,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,uFAAuF;AACvF,MAAM,SAAS,GAAG,IAAI,GAAG,EAA8B,CAAC;AAExD,SAAS,UAAU;IACjB,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC9C,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,aAAa;YAAE,OAAO,GAAG,CAAC;IAC7D,CAAC;IAAC,MAAM,CAAC;QACP,qEAAqE;IACvE,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,SAAS,YAAY,CAAC,OAAgB;IACpC,+EAA+E;IAC/E,0EAA0E;IAC1E,2EAA2E;IAC3E,8DAA8D;IAC9D,IAAI,CAAC;QACH,YAAY,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,2DAA2D;IAC7D,CAAC;IACD,sEAAsE;IACtE,0EAA0E;IAC1E,oDAAoD;IACpD,KAAK,MAAM,QAAQ,IAAI,SAAS;QAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;AACtD,CAAC;AAWD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,UAAU;IACxB,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAU,UAAU,CAAC,CAAC;IAE5D,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,CAAC,IAAa,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACrD,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACxB,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,GAAG,GAAG,WAAW,CAAC,CAAC,IAAa,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;IACnE,2EAA2E;IAC3E,sDAAsD;IACtD,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAE7G,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,KAAK,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,25 @@
1
+ export interface Pagination {
2
+ page: number;
3
+ pageSize: number;
4
+ totalPages: number;
5
+ /** The zero-based index of the first row on the page, for a "showing X–Y" line. */
6
+ startIndex: number;
7
+ /** One past the last row on the page (clamped to the total). */
8
+ endIndex: number;
9
+ setPage(page: number): void;
10
+ setPageSize(size: number): void;
11
+ /** The current page's slice of an in-memory array. */
12
+ paginate<T>(items: T[]): T[];
13
+ }
14
+ /**
15
+ * Client-side pagination state for a {@link DataTable}: which page, how many
16
+ * rows, and the slice of the array they name. The chosen page size is remembered
17
+ * in `localStorage`, so a reader who wants 100 rows says so once.
18
+ *
19
+ * The page is kept honest as the data underneath it moves: a filter that
20
+ * shortens the list drops the reader back to the first page rather than
21
+ * stranding them on one that no longer exists, and a page past the end (the last
22
+ * row on it was removed) clamps back in.
23
+ */
24
+ export declare function usePagination(totalItems: number, initialPageSize?: number): Pagination;
25
+ //# sourceMappingURL=usePagination.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePagination.d.ts","sourceRoot":"","sources":["../src/usePagination.ts"],"names":[],"mappings":"AAmBA,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,mFAAmF;IACnF,UAAU,EAAE,MAAM,CAAC;IACnB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,sDAAsD;IACtD,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;CAC9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,MAAM,GAAG,UAAU,CAmCtF"}
@@ -0,0 +1,59 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ const DEFAULT_PAGE_SIZE = 20;
3
+ /** One key for every list, so the reader's chosen page size follows them across screens. */
4
+ const STORAGE_KEY = 'ctl:pagination:pageSize';
5
+ function readStoredPageSize(fallback) {
6
+ try {
7
+ const raw = localStorage.getItem(STORAGE_KEY);
8
+ if (raw) {
9
+ const parsed = Number(raw);
10
+ if (Number.isFinite(parsed) && parsed > 0)
11
+ return parsed;
12
+ }
13
+ }
14
+ catch {
15
+ /* Private mode or blocked storage: the fallback is a fine page size. */
16
+ }
17
+ return fallback;
18
+ }
19
+ /**
20
+ * Client-side pagination state for a {@link DataTable}: which page, how many
21
+ * rows, and the slice of the array they name. The chosen page size is remembered
22
+ * in `localStorage`, so a reader who wants 100 rows says so once.
23
+ *
24
+ * The page is kept honest as the data underneath it moves: a filter that
25
+ * shortens the list drops the reader back to the first page rather than
26
+ * stranding them on one that no longer exists, and a page past the end (the last
27
+ * row on it was removed) clamps back in.
28
+ */
29
+ export function usePagination(totalItems, initialPageSize) {
30
+ const [pageSize, setPageSizeState] = useState(() => readStoredPageSize(initialPageSize ?? DEFAULT_PAGE_SIZE));
31
+ const [page, setPage] = useState(1);
32
+ const previousTotal = useRef(totalItems);
33
+ const totalPages = Math.max(1, Math.ceil(totalItems / pageSize));
34
+ useEffect(() => {
35
+ if (previousTotal.current !== totalItems) {
36
+ previousTotal.current = totalItems;
37
+ setPage(1);
38
+ }
39
+ }, [totalItems]);
40
+ useEffect(() => {
41
+ if (page > totalPages)
42
+ setPage(totalPages);
43
+ }, [page, totalPages]);
44
+ const setPageSize = useCallback((size) => {
45
+ setPageSizeState(size);
46
+ setPage(1);
47
+ try {
48
+ localStorage.setItem(STORAGE_KEY, String(size));
49
+ }
50
+ catch {
51
+ /* Not remembering the choice is not worth failing the render over. */
52
+ }
53
+ }, []);
54
+ const startIndex = (page - 1) * pageSize;
55
+ const endIndex = Math.min(startIndex + pageSize, totalItems);
56
+ const paginate = useCallback((items) => items.slice(startIndex, endIndex), [startIndex, endIndex]);
57
+ return { page, pageSize, totalPages, startIndex, endIndex, setPage, setPageSize, paginate };
58
+ }
59
+ //# sourceMappingURL=usePagination.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePagination.js","sourceRoot":"","sources":["../src/usePagination.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEjE,MAAM,iBAAiB,GAAG,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AAC,UAAU,CAAC,CAAC;IAC7C,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAEvB,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,IAAY,EAAE,EAAE;QAC/C,gBAAgB,CAAC,IAAI,CAAC,CAAC;QACvB,OAAO,CAAC,CAAC,CAAC,CAAC;QACX,IAAI,CAAC;YACH,YAAY,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QAClD,CAAC;QAAC,MAAM,CAAC;YACP,sEAAsE;QACxE,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,UAAU,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC;IACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,GAAG,QAAQ,EAAE,UAAU,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAG,WAAW,CAC1B,CAAK,KAAU,EAAO,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,QAAQ,CAAC,EAC1D,CAAC,UAAU,EAAE,QAAQ,CAAC,CACvB,CAAC;IAEF,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC;AAC9F,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { SortDirection } from './DataTable.js';
2
+ /**
3
+ * The value a column is ordered by.
4
+ *
5
+ * A column's `render` returns markup, which cannot be compared — so a sortable
6
+ * column says how to rank its rows with a plain value instead. `null`/`undefined`
7
+ * is an absent value, not the smallest one: it sorts to the end either way.
8
+ */
9
+ export type SortValue = string | number | bigint | boolean | Date | null | undefined;
10
+ export interface TableSortState {
11
+ key: string | null;
12
+ direction: SortDirection;
13
+ }
14
+ export interface UseTableSortOptions<T> {
15
+ /** The column sorted by before the reader touches a head. Unsorted by default. */
16
+ defaultKey?: string | null;
17
+ defaultDirection?: SortDirection;
18
+ /**
19
+ * The comparable value for a row under a column key. Defaults to the raw
20
+ * `item[key]`, which is right when the key names a plain field and wrong when
21
+ * the cell is derived — so a derived column passes its own.
22
+ */
23
+ getSortValue?(item: T, key: string): SortValue;
24
+ }
25
+ export interface TableSort<T> {
26
+ sortKey: string | null;
27
+ sortDirection: SortDirection;
28
+ /** Sort by a column; clicking the column already sorted flips the direction. */
29
+ toggle(key: string): void;
30
+ /** The items in the current order — a copy, never the caller's array in place. */
31
+ sort(items: T[]): T[];
32
+ }
33
+ export declare function compareSortValues(a: SortValue, b: SortValue): number;
34
+ /**
35
+ * Client-side sort state for a {@link DataTable}, the same way {@link useOptionSelect}
36
+ * is the state behind a select: the table renders the head and the arrow, and
37
+ * this holds which column and which way and does the ordering.
38
+ *
39
+ * In memory, over the array the caller already has — a list that pages
40
+ * server-side over a keyset sorts in its query instead and never reaches here.
41
+ *
42
+ * ```tsx
43
+ * const sort = useTableSort<Row>({
44
+ * getSortValue: (row, key) => (key === 'name' ? row.displayName : row[key]),
45
+ * });
46
+ * <DataTable rows={sort.sort(rows)} sortKey={sort.sortKey} sortDirection={sort.sortDirection} onSort={sort.toggle} … />
47
+ * ```
48
+ */
49
+ export declare function useTableSort<T>(options?: UseTableSortOptions<T>): TableSort<T>;
50
+ //# sourceMappingURL=useTableSort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTableSort.d.ts","sourceRoot":"","sources":["../src/useTableSort.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC;AAErF,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,SAAS,EAAE,aAAa,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAmB,CAAC,CAAC;IACpC,kFAAkF;IAClF,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,gBAAgB,CAAC,EAAE,aAAa,CAAC;IACjC;;;;OAIG;IACH,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAChD;AAED,MAAM,WAAW,SAAS,CAAC,CAAC;IAC1B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,aAAa,EAAE,aAAa,CAAC;IAC7B,gFAAgF;IAChF,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,kFAAkF;IAClF,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;CACvB;AAYD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAUpE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAE,mBAAmB,CAAC,CAAC,CAAM,GAAG,SAAS,CAAC,CAAC,CAAC,CAgClF"}
@@ -0,0 +1,73 @@
1
+ import { useCallback, useState } from 'react';
2
+ /**
3
+ * Compare two sort values: text by locale, dates by instant, booleans false
4
+ * before true, everything else by `<`. Absent values (`null`/`undefined`) go
5
+ * last, so an empty cell never floats to the top of an ascending sort.
6
+ */
7
+ /** `null` or `undefined` — a cell with nothing to sort by. */
8
+ function absent(value) {
9
+ return value === null || value === undefined;
10
+ }
11
+ export function compareSortValues(a, b) {
12
+ if (absent(a) && absent(b))
13
+ return 0;
14
+ if (absent(a))
15
+ return 1;
16
+ if (absent(b))
17
+ return -1;
18
+ if (typeof a === 'string' && typeof b === 'string')
19
+ return a.localeCompare(b);
20
+ if (a instanceof Date && b instanceof Date)
21
+ return a.getTime() - b.getTime();
22
+ if (typeof a === 'boolean' && typeof b === 'boolean')
23
+ return a === b ? 0 : a ? 1 : -1;
24
+ // `bigint` compares exactly — money in minor units can run past a safe integer.
25
+ if (typeof a === 'bigint' && typeof b === 'bigint')
26
+ return a < b ? -1 : a > b ? 1 : 0;
27
+ return a < b ? -1 : a > b ? 1 : 0;
28
+ }
29
+ /**
30
+ * Client-side sort state for a {@link DataTable}, the same way {@link useOptionSelect}
31
+ * is the state behind a select: the table renders the head and the arrow, and
32
+ * this holds which column and which way and does the ordering.
33
+ *
34
+ * In memory, over the array the caller already has — a list that pages
35
+ * server-side over a keyset sorts in its query instead and never reaches here.
36
+ *
37
+ * ```tsx
38
+ * const sort = useTableSort<Row>({
39
+ * getSortValue: (row, key) => (key === 'name' ? row.displayName : row[key]),
40
+ * });
41
+ * <DataTable rows={sort.sort(rows)} sortKey={sort.sortKey} sortDirection={sort.sortDirection} onSort={sort.toggle} … />
42
+ * ```
43
+ */
44
+ export function useTableSort(options = {}) {
45
+ const { defaultKey = null, defaultDirection = 'asc', getSortValue } = options;
46
+ const [state, setState] = useState({ key: defaultKey, direction: defaultDirection });
47
+ const toggle = useCallback((key) => {
48
+ setState((prev) => ({
49
+ key,
50
+ direction: prev.key === key && prev.direction === 'asc' ? 'desc' : 'asc',
51
+ }));
52
+ }, []);
53
+ const sort = useCallback((items) => {
54
+ const { key, direction } = state;
55
+ if (!key)
56
+ return items;
57
+ const value = getSortValue ?? ((item, k) => item[k]);
58
+ // A copy: the caller's array is likely a query cache page, not ours to reorder.
59
+ return [...items].sort((a, b) => {
60
+ const av = value(a, key);
61
+ const bv = value(b, key);
62
+ // Absent values sort last in *either* direction, so negating the whole
63
+ // comparison for a descending sort would be wrong: it would float the
64
+ // empty cells to the top. Only the present-vs-present order flips.
65
+ if (absent(av) || absent(bv))
66
+ return compareSortValues(av, bv);
67
+ const cmp = compareSortValues(av, bv);
68
+ return direction === 'asc' ? cmp : -cmp;
69
+ });
70
+ }, [state, getSortValue]);
71
+ return { sortKey: state.key, sortDirection: state.dire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,EAAE;YAC9B,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;YACzB,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;YACzB,uEAAuE;YACvE,sEAAsE;YACtE,mEAAmE;YACnE,IAAI,MAAM,CAAC,EAAE,CAAC,IAAI,MAAM,CAAC,EAAE,CAAC;gBAAE,OAAO,iBAAiB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YAC/D,MAAM,GAAG,GAAG,iBAAiB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YACtC,OAAO,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QAC1C,CAAC,CAAC,CAAC;IACL,CAAC,EACD,CAAC,KAAK,EAAE,YAAY,CAAC,CACtB,CAAC;IAEF,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC9E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speles7172/controls",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "Reusable React building blocks — a data table, searchable selects, a date range filter with a Gregorian and Hebrew calendar, a modal, badges and the loading and empty states — over static options or a transport you supply.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  "prepublishOnly": "npm run build"
34
34
  },
35
35
  "dependencies": {
36
- "@speles7172/controls-client": "^0.2.0",
36
+ "@speles7172/controls-client": "^0.3.0",
37
37
  "lucide-react": "^1.33.0"
38
38
  },
39
39
  "peerDependencies": {