@@ -2,7 +2,13 @@ import { useCallback, useEffect, useRef } from 'react';
2
2
import get from 'lodash/get' ;
3
3
import isEqual from 'lodash/isEqual' ;
4
4
import { indexById , removeEmpty , useSafeSetState } from '../util' ;
5
- import { Identifier , Record , RecordMap , SortPayload } from '../types' ;
5
+ import {
6
+ FilterPayload ,
7
+ Identifier ,
8
+ Record ,
9
+ RecordMap ,
10
+ SortPayload ,
11
+ } from '../types' ;
6
12
import usePaginationState from './usePaginationState' ;
7
13
import useSortState from './useSortState' ;
8
14
import useSelectionState from './useSelectionState' ;
@@ -38,31 +44,28 @@ import { ListControllerProps } from '.';
38
44
* );
39
45
* };
40
46
*
41
- * @param {UseListOptions } props Also optionally accepts all the ListController props
42
- * @param {Record[] } props.initialData An array of records
43
- * @param {Identifier[] } props.initialIds An array of the record identifiers
47
+ * @param {UseListOptions } props
48
+ * @param {Record[] } props.data An array of records
49
+ * @param {Identifier[] } props.ids An array of the record identifiers
44
50
* @param {Boolean } props.loaded: A boolean indicating whether the data has been loaded at least once
45
51
* @param {Boolean } props.loading: A boolean indicating whether the data is being loaded
46
52
* @param {Error | String } props.error: Optional. The error if any occured while loading the data
47
53
* @param {Object } props.filter: Optional. An object containing the filters applied on the data
48
- * @param {Number } props.initialPage : Optional. The initial page index
49
- * @param {Number } props.initialPerPage : Optional. The initial page size
50
- * @param {SortPayload } props.initialSort : Optional. The initial sort (field and order)
54
+ * @param {Number } props.page : Optional. The initial page index
55
+ * @param {Number } props.perPage : Optional. The initial page size
56
+ * @param {SortPayload } props.sort : Optional. The initial sort (field and order)
51
57
*/
52
58
export const useList = ( props : UseListOptions ) : UseListValue => {
53
59
const {
54
60
data,
55
61
error,
56
62
filter = defaultFilter ,
57
63
ids,
58
- initialData,
59
- initialIds,
60
64
loaded,
61
65
loading,
62
- initialPage = 1 ,
63
- initialPerPage = 1000 ,
64
- initialSort = defaultSort ,
65
- total,
66
+ page : initialPage = 1 ,
67
+ perPage : initialPerPage = 1000 ,
68
+ sort : initialSort = defaultSort ,
66
69
} = props ;
67
70
const [ loadingState , setLoadingState ] = useSafeSetState < boolean > ( loading ) ;
68
71
const [ loadedState , setLoadedState ] = useSafeSetState < boolean > ( loaded ) ;
@@ -71,8 +74,8 @@ export const useList = (props: UseListOptions): UseListValue => {
71
74
data : RecordMap ;
72
75
ids : Identifier [ ] ;
73
76
} > ( ( ) => ( {
74
- data : indexById ( initialData ) ,
75
- ids : initialIds ,
77
+ data : indexById ( data ) ,
78
+ ids,
76
79
} ) ) ;
77
80
78
81
// pagination logic
@@ -156,13 +159,9 @@ export const useList = (props: UseListOptions): UseListValue => {
156
159
// We do all the data processing (filtering, sorting, paginating) client-side
157
160
useEffect ( ( ) => {
158
161
if ( ! loaded ) return ;
159
- // Assume that if setFilters is provided then so are methods for pagination and sorting
160
- if ( props . setFilters ) {
161
- return ;
162
- }
163
162
164
163
// 1. filter
165
- let tempData = initialData . filter ( record =>
164
+ let tempData = data . filter ( record =>
166
165
Object . entries ( filterValues ) . every ( ( [ filterName , filterValue ] ) => {
167
166
const recordValue = get ( record , filterName ) ;
168
167
const result = Array . isArray ( recordValue )
@@ -199,12 +198,11 @@ export const useList = (props: UseListOptions): UseListValue => {
199
198
ids : finalIds ,
200
199
} ) ;
201
200
} , [
202
- initialData ,
201
+ data ,
203
202
filterValues ,
204
203
loaded ,
205
204
page ,
206
205
perPage ,
207
- props . setFilters ,
208
206
setFinalItems ,
209
207
sort . field ,
210
208
sort . order ,
@@ -223,43 +221,40 @@ export const useList = (props: UseListOptions): UseListValue => {
223
221
} , [ loading , loadingState , setLoadingState ] ) ;
224
222
225
223
return {
226
- currentSort : props . currentSort || sort ,
227
- data : data || finalItems . data ,
224
+ currentSort : sort ,
225
+ data : finalItems . data ,
228
226
error,
229
- displayedFilters : props . displayedFilters || displayedFilters ,
230
- filterValues : props . filterValues || filterValues ,
231
- hideFilter : props . hideFilter || hideFilter ,
232
- ids : ids || finalItems . ids ,
227
+ displayedFilters,
228
+ filterValues,
229
+ hideFilter,
230
+ ids : finalItems . ids ,
233
231
loaded : loadedState ,
234
232
loading : loadingState ,
235
- onSelect : props . onSelect || onSelect ,
236
- onToggleItem : props . onToggleItem || onToggleItem ,
237
- onUnselectItems : props . onUnselectItems || onUnselectItems ,
238
- page : props . page || page ,
239
- perPage : props . perPage || perPage ,
240
- selectedIds : props . selectedIds || selectedIds ,
241
- setFilters : props . setFilters || setFilters ,
242
- setPage : props . setPage || setPage ,
243
- setPerPage : props . setPerPage || setPerPage ,
244
- setSort : props . setSort || setSort ,
245
- showFilter : props . showFilter || showFilter ,
246
- total : total || finalItems . ids . length ,
233
+ onSelect,
234
+ onToggleItem,
235
+ onUnselectItems,
236
+ page,
237
+ perPage,
238
+ selectedIds,
239
+ setFilters,
240
+ setPage,
241
+ setPerPage,
242
+ setSort,
243
+ showFilter,
244
+ total : finalItems . ids . length ,
247
245
} ;
248
246
} ;
249
247
250
- export interface UseListOptions
251
- extends Partial <
252
- Omit < ListControllerProps , 'resource' | 'basePath' | 'refetch' >
253
- > {
248
+ export interface UseListOptions < RecordType extends Record = Record > {
249
+ data : RecordType [ ] ;
250
+ ids : Identifier [ ] ;
254
251
error ?: any ;
255
- filter ?: any ;
256
- initialPage ?: number ;
257
- initialPerPage ?: number ;
258
- initialSort ?: SortPayload ;
259
- initialData : Record [ ] ;
260
- initialIds : Identifier [ ] ;
261
- loaded : boolean ;
252
+ filter ?: FilterPayload ;
262
253
loading : boolean ;
254
+ loaded : boolean ;
255
+ page ?: number ;
256
+ perPage ?: number ;
257
+ sort ?: SortPayload ;
263
258
}
264
259
265
260
export type UseListValue = Omit <
0 commit comments