ehscan-react-table 0.0.7 → 0.0.8

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,2 +1,5 @@
1
1
  export { Table } from './elements/Table';
2
2
  export { Pagination } from './elements/Pagination';
3
+ export { useSortOrder } from './elements/SortOrderUtils';
4
+ export { sortRows } from './elements/SortOrderUtils';
5
+ export type { SortOrder } from './elements/SortOrderUtils';
@@ -1,3 +1,5 @@
1
1
  // ehscan-react-table entry
2
2
  export { Table } from './elements/Table';
3
3
  export { Pagination } from './elements/Pagination';
4
+ export { useSortOrder } from './elements/SortOrderUtils';
5
+ export { sortRows } from './elements/SortOrderUtils';
@@ -0,0 +1,11 @@
1
+ export type SortOrder = {
2
+ tag: string;
3
+ dir: 'asc' | 'desc';
4
+ };
5
+ export declare const useSortOrder: (localStorageTag: string | null, defaultSortOrder: SortOrder) => readonly [SortOrder, import("react").Dispatch<import("react").SetStateAction<SortOrder>>];
6
+ type Row = {
7
+ [key: string]: any;
8
+ id: number;
9
+ };
10
+ export declare const sortRows: (rows: Row[], sortOrder: SortOrder) => Row[];
11
+ export {};
@@ -0,0 +1,45 @@
1
+ import { useState } from 'react';
2
+ /**
3
+ * Returns the initial sort order from localStorage or default.
4
+ */
5
+ const getInitialSortOrder = (localStorageTag, defaultSortOrder) => {
6
+ if (!localStorageTag)
7
+ return defaultSortOrder;
8
+ try {
9
+ const savedStorage = JSON.parse(localStorage.getItem(localStorageTag) || '{}');
10
+ if (!savedStorage.tag || !savedStorage.dir)
11
+ return defaultSortOrder;
12
+ return { tag: savedStorage.tag, dir: savedStorage.dir };
13
+ }
14
+ catch (e) {
15
+ console.error('Failed to parse sortOrder from localStorage', e);
16
+ return defaultSortOrder;
17
+ }
18
+ };
19
+ export const useSortOrder = (localStorageTag, defaultSortOrder) => {
20
+ const [sortOrder, setSortOrder] = useState(() => getInitialSortOrder(localStorageTag, defaultSortOrder));
21
+ return [sortOrder, setSortOrder];
22
+ };
23
+ export const sortRows = (rows, sortOrder) => {
24
+ const { tag, dir } = sortOrder;
25
+ if (!tag)
26
+ return rows;
27
+ return [...rows].sort((a, b) => {
28
+ const valA = a[tag];
29
+ const valB = b[tag];
30
+ if (valA === undefined || valA === null)
31
+ return 1;
32
+ if (valB === undefined || valB === null)
33
+ return -1;
34
+ if (typeof valA === 'number' && typeof valB === 'number') {
35
+ return dir === 'asc' ? valA - valB : valB - valA;
36
+ }
37
+ const strA = valA.toString().toLowerCase();
38
+ const strB = valB.toString().toLowerCase();
39
+ if (strA < strB)
40
+ return dir === 'asc' ? -1 : 1;
41
+ if (strA > strB)
42
+ return dir === 'asc' ? 1 : -1;
43
+ return 0;
44
+ });
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ehscan-react-table",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "components",
5
5
  "main": "dist/Components.js",
6
6
  "types": "dist/Components.d.ts",