-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathusePagination.ts
160 lines (132 loc) · 3.42 KB
/
usePagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as React from 'react';
import { useReducerWithMiddleware } from '@table-library/react-table-library/common/util/useReducerWithMiddleware';
import { useSyncControlledState } from '@table-library/react-table-library/common/util//useSyncControlledState';
import { useSyncRefState } from '@table-library/react-table-library/common/util/useSyncRefState';
import { Action, State, StateAndChange } from '@table-library/react-table-library/types/common';
import { Data, ExtendedNode, TableNode } from '@table-library/react-table-library/types/table';
import {
Pages,
Pagination,
PaginationOptions,
} from '@table-library/react-table-library/types/pagination';
const SET = 'SET';
const set = (state: State, action: Action) => ({
...state,
...action.payload,
});
const reducer = (state: State, action: Action) => {
switch (action.type) {
case SET: {
return set(state, action);
}
default:
throw new Error();
}
};
const DEFAULT_STATE = {
page: 0,
size: 10,
};
const DEFAULT_OPTIONS = {
isServer: false,
};
const usePagination = <T extends TableNode>(
data: Data<T>,
primary?: StateAndChange,
options?: PaginationOptions,
context?: any,
): Pagination<T> => {
const controlledState: State = {
...DEFAULT_STATE,
...(primary?.state ?? {}),
};
const onChange = primary?.onChange ? primary.onChange : () => {};
const [state, dispatchWithMiddleware] = useReducerWithMiddleware(
reducer,
controlledState,
[],
[onChange],
context,
);
const onSetPage = React.useCallback(
(value) =>
dispatchWithMiddleware({
type: SET,
payload: { page: value },
}),
[dispatchWithMiddleware],
);
const onSetSize = React.useCallback(
(value) =>
dispatchWithMiddleware({
type: SET,
payload: { size: value, page: 0 },
}),
[dispatchWithMiddleware],
);
useSyncControlledState(controlledState, state, () =>
dispatchWithMiddleware({
type: SET,
payload: controlledState,
}),
);
const getTotalPages = React.useCallback(
(nodes) => Math.ceil(nodes.length / state.size),
[state.size],
);
const getPages = React.useCallback(
(nodes) =>
nodes.reduce((acc: Pages, node: TableNode, index: number) => {
const sizeIndex = Math.floor(index / state.size);
if (!acc[sizeIndex]) {
acc[sizeIndex] = [];
}
acc[sizeIndex].push(node);
return acc;
}, []),
[state.size],
);
const getPageBoundaries = React.useCallback(
(nodes) => {
const start = state.page * state.size + 1;
const end = state.page * state.size + state.size;
return {
start,
end: end > nodes.length ? nodes.length : end,
};
},
[state.page, state.size],
);
const fns = React.useMemo(
() => ({
onSetPage,
onSetSize,
}),
[onSetPage, onSetSize],
);
useSyncRefState('pagination', context, state);
const mergedOptions = {
...DEFAULT_OPTIONS,
...(options ?? {}),
};
const stateAndGetters = {
...state,
getTotalPages,
getPages,
getPageBoundaries,
};
const modifier = (nodes: T[]): ExtendedNode<T>[] => {
if (mergedOptions.isServer) {
return nodes;
}
// TODO tree?
return stateAndGetters.getPages(nodes)[state.page] || [];
};
return {
state: stateAndGetters,
fns,
options: mergedOptions,
modifier,
};
};
export { usePagination };