Skip to content

Commit 7f359f1

Browse files
committed
[ui-react] Refactor into separate files
1 parent f5efff8 commit 7f359f1

39 files changed

+592
-553
lines changed

src/ui-react-dom/EditableCellView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
useSetCellCallback,
88
useStoreOrStoreById,
99
} from '../ui-react/hooks.ts';
10-
import {EDITABLE, EditableThing} from './common.tsx';
10+
import {EditableThing} from './common/components.tsx';
11+
import {EDITABLE} from './common/index.tsx';
1112

1213
export const EditableCellView: typeof EditableCellViewDecl = ({
1314
tableId,

src/ui-react-dom/EditableValueView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
useStoreOrStoreById,
88
useValue,
99
} from '../ui-react/hooks.ts';
10-
import {EDITABLE, EditableThing} from './common.tsx';
10+
import {EditableThing} from './common/components.tsx';
11+
import {EDITABLE} from './common/index.tsx';
1112

1213
export const EditableValueView: typeof EditableValueViewDecl = ({
1314
valueId,

src/ui-react-dom/RelationshipInHtmlTable.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import {
1717
} from '../ui-react/hooks.ts';
1818
import {CellView} from '../ui-react/index.ts';
1919
import {EditableCellView} from './EditableCellView.tsx';
20+
import {useCells, useParams} from './common/hooks.tsx';
2021
import {
2122
extraHeaders,
2223
extraRowCells,
2324
RelationshipInHtmlRowParams,
24-
useCells,
25-
useParams,
26-
} from './common.tsx';
25+
} from './common/index.tsx';
2726

2827
const useDottedCellIds = (tableId: Id | undefined, store: Store | undefined) =>
2928
arrayMap(

src/ui-react-dom/ResultSortedTableInHtmlTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
useResultTableCellIds,
1010
} from '../ui-react/hooks.ts';
1111
import {ResultCellView} from '../ui-react/index.ts';
12+
import {HtmlTable} from './common/components.tsx';
1213
import {
13-
HtmlTable,
1414
useCells,
1515
useParams,
1616
useQueriesCellComponentProps,
17-
useSortingAndPagination,
18-
} from './common.tsx';
17+
} from './common/hooks.tsx';
18+
import {useSortingAndPagination} from './SortedTablePaginator.tsx';
1919

2020
export const ResultSortedTableInHtmlTable: typeof ResultSortedTableInHtmlTableDecl =
2121
({

src/ui-react-dom/ResultTableInHtmlTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type {
55
} from '../@types/ui-react-dom/index.js';
66
import {useResultRowIds, useResultTableCellIds} from '../ui-react/hooks.ts';
77
import {ResultCellView} from '../ui-react/index.ts';
8+
import {HtmlTable} from './common/components.tsx';
89
import {
9-
HtmlTable,
1010
useCells,
1111
useParams,
1212
useQueriesCellComponentProps,
13-
} from './common.tsx';
13+
} from './common/hooks.tsx';
1414

1515
export const ResultTableInHtmlTable: typeof ResultTableInHtmlTableDecl = ({
1616
queryId,

src/ui-react-dom/SliceInHtmlTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import {
1111
useTableCellIds,
1212
} from '../ui-react/hooks.ts';
1313
import {CellView} from '../ui-react/index.ts';
14-
import {EditableCellView} from './EditableCellView.tsx';
14+
import {HtmlTable} from './common/components.tsx';
1515
import {
16-
HtmlTable,
1716
useCells,
1817
useParams,
1918
useStoreCellComponentProps,
20-
} from './common.tsx';
19+
} from './common/hooks.tsx';
20+
import {EditableCellView} from './EditableCellView.tsx';
2121

2222
export const SliceInHtmlTable: typeof SliceInHtmlTableDecl = ({
2323
indexId,

src/ui-react-dom/SortedTableInHtmlTable.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {
99
useTableCellIds,
1010
} from '../ui-react/hooks.ts';
1111
import {CellView} from '../ui-react/index.ts';
12-
import {EditableCellView} from './EditableCellView.tsx';
12+
import {HtmlTable} from './common/components.tsx';
1313
import {
14-
HtmlTable,
1514
useCells,
1615
useParams,
17-
useSortingAndPagination,
1816
useStoreCellComponentProps,
19-
} from './common.tsx';
17+
} from './common/hooks.tsx';
18+
import {EditableCellView} from './EditableCellView.tsx';
19+
import {useSortingAndPagination} from './SortedTablePaginator.tsx';
2020

2121
export const SortedTableInHtmlTable: typeof SortedTableInHtmlTableDecl = ({
2222
tableId,

src/ui-react-dom/SortedTablePaginator.tsx

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,83 @@ import type {
33
SortedTablePaginatorProps,
44
} from '../@types/ui-react-dom/index.js';
55
import {mathMin} from '../common/other.ts';
6-
import {useCallbackOrUndefined} from './common.tsx';
6+
import {useCallbackOrUndefined} from './common/hooks.tsx';
7+
8+
import type {ComponentType, ReactNode} from 'react';
9+
import type {Id} from '../@types/index.js';
10+
import {useCallback, useMemo, useState} from '../common/react.ts';
11+
import {HandleSort, SortAndOffset} from './common/index.tsx';
712

813
const LEFT_ARROW = '\u2190';
914
const RIGHT_ARROW = '\u2192';
1015

16+
export const useSortingAndPagination = (
17+
cellId: Id | undefined,
18+
descending = false,
19+
sortOnClick: boolean | undefined,
20+
offset = 0,
21+
limit: number | undefined,
22+
total: number,
23+
paginator: boolean | ComponentType<SortedTablePaginatorProps>,
24+
onChange?: (sortAndOffset: SortAndOffset) => void,
25+
): [
26+
sortAndOffset: SortAndOffset,
27+
handleSort: HandleSort,
28+
paginatorComponent: ReactNode | undefined,
29+
] => {
30+
const [[currentCellId, currentDescending, currentOffset], setState] =
31+
useState<SortAndOffset>([cellId, descending, offset]);
32+
const setStateAndChange = useCallback(
33+
(sortAndOffset: SortAndOffset) => {
34+
setState(sortAndOffset);
35+
onChange?.(sortAndOffset);
36+
},
37+
[onChange],
38+
);
39+
const handleSort = useCallbackOrUndefined(
40+
(cellId: Id | undefined) =>
41+
setStateAndChange([
42+
cellId,
43+
cellId == currentCellId ? !currentDescending : false,
44+
currentOffset,
45+
]),
46+
[setStateAndChange, currentCellId, currentDescending, currentOffset],
47+
sortOnClick,
48+
);
49+
const handleChangeOffset = useCallback(
50+
(offset: number) =>
51+
setStateAndChange([currentCellId, currentDescending, offset]),
52+
[setStateAndChange, currentCellId, currentDescending],
53+
);
54+
const PaginatorComponent =
55+
paginator === true
56+
? SortedTablePaginator
57+
: (paginator as ComponentType<SortedTablePaginatorProps>);
58+
return [
59+
[currentCellId, currentDescending, currentOffset],
60+
handleSort,
61+
useMemo(
62+
() =>
63+
paginator === false ? null : (
64+
<PaginatorComponent
65+
offset={currentOffset}
66+
limit={limit}
67+
total={total}
68+
onChange={handleChangeOffset}
69+
/>
70+
),
71+
[
72+
paginator,
73+
PaginatorComponent,
74+
currentOffset,
75+
limit,
76+
total,
77+
handleChangeOffset,
78+
],
79+
),
80+
];
81+
};
82+
1183
export const SortedTablePaginator: typeof SortedTablePaginatorDecl = ({
1284
onChange,
1385
total,

src/ui-react-dom/TableInHtmlTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import type {
55
} from '../@types/ui-react-dom/index.js';
66
import {useRowIds, useTableCellIds} from '../ui-react/hooks.ts';
77
import {CellView} from '../ui-react/index.ts';
8-
import {EditableCellView} from './EditableCellView.tsx';
8+
import {HtmlTable} from './common/components.tsx';
99
import {
10-
HtmlTable,
1110
useCells,
1211
useParams,
1312
useStoreCellComponentProps,
14-
} from './common.tsx';
13+
} from './common/hooks.tsx';
14+
import {EditableCellView} from './EditableCellView.tsx';
1515

1616
export const TableInHtmlTable: typeof TableInHtmlTableDecl = ({
1717
tableId,

src/ui-react-dom/ValuesInHtmlTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {EXTRA, VALUE} from '../common/strings.ts';
1111
import {useValueIds} from '../ui-react/hooks.ts';
1212
import {ValueView} from '../ui-react/index.ts';
1313
import {EditableValueView} from './EditableValueView.tsx';
14-
import {extraHeaders, extraKey} from './common.tsx';
14+
import {extraHeaders, extraKey} from './common/index.tsx';
1515

1616
const extraValueCells = (
1717
extraValueCells: ExtraValueCell[] = [],

0 commit comments

Comments
 (0)