Skip to content

Commit dd28619

Browse files
committed
add render prop on List component
1 parent 59bc05e commit dd28619

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

packages/ra-ui-materialui/src/list/List.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const List = <RecordType extends RaRecord = any>(
7272
resource,
7373
sort,
7474
storeKey,
75+
render,
7576
...rest
7677
} = useThemeProps({
7778
props: props,
@@ -93,13 +94,13 @@ export const List = <RecordType extends RaRecord = any>(
9394
sort={sort}
9495
storeKey={storeKey}
9596
>
96-
<ListView<RecordType> {...rest} />
97+
<ListView<RecordType> {...rest} render={render} />
9798
</ListBase>
9899
);
99100
};
100101

101102
export interface ListProps<RecordType extends RaRecord = any>
102-
extends Omit<ListBaseProps<RecordType>, 'children'>,
103+
extends Omit<ListBaseProps<RecordType>, 'children' | 'render'>,
103104
ListViewProps {}
104105

105106
const defaultFilter = {};

packages/ra-ui-materialui/src/list/ListView.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import type { ReactElement, ReactNode, ElementType } from 'react';
99
import Card from '@mui/material/Card';
1010
import clsx from 'clsx';
11-
import { useListContext, type RaRecord } from 'ra-core';
11+
import { ListControllerResult, useListContext, type RaRecord } from 'ra-core';
1212

1313
import { Title } from '../layout/Title';
1414
import { ListToolbar } from './ListToolbar';
@@ -36,8 +36,10 @@ export const ListView = <RecordType extends RaRecord = any>(
3636
component: Content = DefaultComponent,
3737
title,
3838
empty = defaultEmpty,
39+
render,
3940
...rest
4041
} = props;
42+
const listContext = useListContext<RecordType>();
4143
const {
4244
defaultTitle,
4345
data,
@@ -48,9 +50,9 @@ export const ListView = <RecordType extends RaRecord = any>(
4850
total,
4951
hasNextPage,
5052
hasPreviousPage,
51-
} = useListContext<RecordType>();
53+
} = listContext;
5254

53-
if (!children || (!data && isPending && emptyWhileLoading)) {
55+
if ((!children && !render) || (!data && isPending && emptyWhileLoading)) {
5456
return null;
5557
}
5658

@@ -63,7 +65,9 @@ export const ListView = <RecordType extends RaRecord = any>(
6365
actions={actions}
6466
/>
6567
)}
66-
<Content className={ListClasses.content}>{children}</Content>
68+
<Content className={ListClasses.content}>
69+
{render ? render(listContext) : children}
70+
</Content>
6771
{!error && pagination !== false && pagination}
6872
</div>
6973
);
@@ -102,7 +106,7 @@ export const ListView = <RecordType extends RaRecord = any>(
102106
);
103107
};
104108

105-
export interface ListViewProps {
109+
export interface ListViewProps<RecordType extends RaRecord = any> {
106110
/**
107111
* The actions to display in the toolbar. defaults to Filter + Create + Export.
108112
*
@@ -193,6 +197,30 @@ export interface ListViewProps {
193197
*/
194198
children: ReactNode;
195199

200+
/**
201+
* A function rendering the list of records. Take the list controller as argument.
202+
*
203+
* @see https://marmelab.com/react-admin/List.html#children
204+
* @example
205+
* import { List } from 'react-admin';
206+
*
207+
* export const BookList = () => (
208+
* <List>
209+
* {(listContext) =>
210+
* listContext.data.map(record => (
211+
* <div key={record.id}>
212+
* <p>{record.id}</p>
213+
* <p>{record.title}</p>
214+
* <p>{record.published_at}</p>
215+
* <p>{record.nb_views}</p>
216+
* </div>
217+
* )
218+
* }
219+
* </List>
220+
* );
221+
*/
222+
render?: (props: ListControllerResult<RecordType, Error>) => ReactNode;
223+
196224
/**
197225
* The component used to display the list. Defaults to <Card>.
198226
*

0 commit comments

Comments
 (0)