@@ -98,7 +98,7 @@ type DataTableProps<TData, TValue> = {
98
98
data : TData [ ] ,
99
99
toolbarRender ?: ( table : TableType < TData > ) => React . ReactNode ,
100
100
defaultVisibility ?: VisibilityState ,
101
- defaultFilters ?: ColumnFiltersState ,
101
+ defaultColumnFilters ?: ColumnFiltersState ,
102
102
defaultSorting ?: SortingState ,
103
103
}
104
104
@@ -107,18 +107,18 @@ export function DataTable<TData, TValue>({
107
107
data,
108
108
toolbarRender,
109
109
defaultVisibility,
110
- defaultFilters ,
110
+ defaultColumnFilters ,
111
111
defaultSorting,
112
112
} : DataTableProps < TData , TValue > ) {
113
113
const [ sorting , setSorting ] = React . useState < SortingState > ( defaultSorting || [ ] ) ;
114
- const [ columnFilters , setColumnFilters ] = React . useState < ColumnFiltersState > ( defaultFilters || [ ] ) ;
114
+ const [ columnFilters , setColumnFilters ] = React . useState < ColumnFiltersState > ( defaultColumnFilters || [ ] ) ;
115
115
const [ pagination , setPagination ] = React . useState < PaginationState > ( {
116
116
pageIndex : 0 ,
117
117
pageSize : 10 ,
118
118
} ) ;
119
119
const [ globalFilter , setGlobalFilter ] = React . useState < any > ( ) ;
120
120
121
- return < DataTableManual
121
+ return < DataTableBase
122
122
columns = { columns }
123
123
data = { data }
124
124
toolbarRender = { toolbarRender }
@@ -136,7 +136,78 @@ export function DataTable<TData, TValue>({
136
136
/> ;
137
137
}
138
138
139
- type DataTableServerProps < TData , TValue > = DataTableProps < TData , TValue > & {
139
+ type DataTableManualPaginationProps < TData , TValue > = DataTableProps < TData , TValue > & {
140
+ onUpdate : ( options : {
141
+ cursor : string ,
142
+ limit : number ,
143
+ sorting : SortingState ,
144
+ columnFilters : ColumnFiltersState ,
145
+ globalFilters : any ,
146
+ } ) => Promise < { nextCursor : string | null } > ,
147
+ }
148
+
149
+ export function DataTableManualPagination < TData , TValue > ( {
150
+ columns,
151
+ data,
152
+ toolbarRender,
153
+ defaultVisibility,
154
+ defaultColumnFilters,
155
+ defaultSorting,
156
+ onUpdate,
157
+ } : DataTableManualPaginationProps < TData , TValue > ) {
158
+ const [ sorting , setSorting ] = React . useState < SortingState > ( defaultSorting || [ ] ) ;
159
+ const [ pagination , setPagination ] = React . useState ( { pageIndex : 0 , pageSize : 10 } ) ;
160
+ const [ cursors , setCursors ] = React . useState < Record < number , string > > ( { } ) ;
161
+ const [ columnFilters , setColumnFilters ] = React . useState < ColumnFiltersState > ( defaultColumnFilters || [ ] ) ;
162
+ const [ globalFilter , setGlobalFilter ] = React . useState < any > ( ) ;
163
+ const [ refreshCounter , setRefreshCounter ] = React . useState ( 0 ) ;
164
+
165
+ React . useEffect ( ( ) => {
166
+ onUpdate ( {
167
+ cursor : cursors [ pagination . pageIndex ] ,
168
+ limit : pagination . pageSize ,
169
+ sorting,
170
+ columnFilters,
171
+ globalFilters : globalFilter ,
172
+ } ) . then ( ( { nextCursor } ) => {
173
+ setCursors ( c => nextCursor ? { ...c , [ pagination . pageIndex + 1 ] : nextCursor } : c ) ;
174
+ } ) . catch ( console . error ) ;
175
+ } , [ pagination , sorting , columnFilters , refreshCounter ] ) ;
176
+
177
+ // Reset to first page when filters change
178
+ React . useEffect ( ( ) => {
179
+ setPagination ( pagination => ( { ...pagination , pageIndex : 0 } ) ) ;
180
+ setCursors ( { } ) ;
181
+ } , [ columnFilters , sorting , pagination . pageSize ] ) ;
182
+
183
+ // Refresh the users when the global filter changes. Delay to prevent unnecessary re-renders.
184
+ React . useEffect ( ( ) => {
185
+ const timer = setTimeout ( ( ) => {
186
+ setRefreshCounter ( x => x + 1 ) ;
187
+ } , 500 ) ;
188
+ return ( ) => clearTimeout ( timer ) ;
189
+ } , [ globalFilter ] ) ;
190
+
191
+ return < DataTableBase
192
+ columns = { columns }
193
+ data = { data }
194
+ toolbarRender = { toolbarRender }
195
+ sorting = { sorting }
196
+ setSorting = { setSorting }
197
+ pagination = { pagination }
198
+ setPagination = { setPagination }
199
+ columnFilters = { columnFilters }
200
+ setColumnFilters = { setColumnFilters }
201
+ rowCount = { pagination . pageSize * Object . keys ( cursors ) . length + ( cursors [ pagination . pageIndex + 1 ] ? 1 : 0 ) }
202
+ globalFilter = { globalFilter }
203
+ setGlobalFilter = { setGlobalFilter }
204
+ defaultColumnFilters = { defaultColumnFilters }
205
+ defaultSorting = { defaultSorting }
206
+ defaultVisibility = { defaultVisibility }
207
+ /> ;
208
+ }
209
+
210
+ type DataTableBaseProps < TData , TValue > = DataTableProps < TData , TValue > & {
140
211
sorting ?: SortingState ,
141
212
setSorting ?: OnChangeFn < SortingState > ,
142
213
pagination ?: PaginationState ,
@@ -150,7 +221,7 @@ type DataTableServerProps<TData, TValue> = DataTableProps<TData, TValue> & {
150
221
setGlobalFilter ?: OnChangeFn < any > ,
151
222
}
152
223
153
- export function DataTableManual < TData , TValue > ( {
224
+ function DataTableBase < TData , TValue > ( {
154
225
columns,
155
226
data,
156
227
toolbarRender,
@@ -166,7 +237,7 @@ export function DataTableManual<TData, TValue>({
166
237
setGlobalFilter,
167
238
manualPagination = true ,
168
239
manualFiltering = true ,
169
- } : DataTableServerProps < TData , TValue > ) {
240
+ } : DataTableBaseProps < TData , TValue > ) {
170
241
const [ rowSelection , setRowSelection ] = React . useState ( { } ) ;
171
242
const [ columnVisibility , setColumnVisibility ] = React . useState < VisibilityState > ( defaultVisibility || { } ) ;
172
243
0 commit comments