File tree Expand file tree Collapse file tree 1 file changed +21
-11
lines changed
src/blocks/remote-data-container/components/item-list Expand file tree Collapse file tree 1 file changed +21
-11
lines changed Original file line number Diff line number Diff line change @@ -20,18 +20,28 @@ export function ItemList( props: ItemListProps ) {
20
20
21
21
const instanceId = useInstanceId ( ItemList , blockName ) ;
22
22
23
- // ensure each result has an 'id' key
24
23
const data = useMemo ( ( ) => {
25
- return ( results ?? [ ] ) . map ( ( item : Record < string , unknown > ) =>
26
- item . id
27
- ? item
28
- : {
29
- ...item ,
30
- id : Object . keys ( item ) . find ( key => / ( ^ | _ ) ( i d ) $ / i. test ( key ) ) // Regex to match 'id' or part of '_id'
31
- ? item [ Object . keys ( item ) . find ( key => / ( ^ | _ ) ( i d ) $ / i. test ( key ) ) as string ]
32
- : instanceId ,
33
- }
34
- ) as RemoteDataResult [ ] ;
24
+ // remove null values from the data to prevent errors in filterSortAndPaginate
25
+ const removeNullValues = ( obj : Record < string , unknown > ) : Record < string , unknown > => {
26
+ return Object . fromEntries (
27
+ Object . entries ( obj ) . filter ( ( [ _ , value ] ) => value !== null )
28
+ ) ;
29
+ } ;
30
+
31
+ return ( results ?? [ ] ) . map ( ( item : Record < string , unknown > ) => {
32
+ const parsedItem = removeNullValues ( item ) ;
33
+
34
+ if ( parsedItem . id ) {
35
+ return parsedItem ;
36
+ }
37
+
38
+ // ensure each result has an 'id' key
39
+ const idKey = Object . keys ( parsedItem ) . find ( key => / ( ^ | _ ) ( i d ) $ / i. test ( key ) ) ;
40
+ return {
41
+ ...parsedItem ,
42
+ id : idKey ? parsedItem [ idKey ] : instanceId ,
43
+ } ;
44
+ } ) as RemoteDataResult [ ] ;
35
45
} , [ results ] ) ;
36
46
37
47
// get fields from results data to use as columns
You can’t perform that action at this time.
0 commit comments