Skip to content

Commit a170dae

Browse files
authored
Merge pull request #6218 from WiXSL/listactions-children
[Doc] Improve ListActions override
2 parents da44753 + 2e78254 commit a170dae

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

docs/List.md

+34-25
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ The title can be either a string or an element of your own.
9292

9393
You can replace the list of default actions by your own element using the `actions` prop:
9494

95+
```jsx
96+
import * as React from 'react';
97+
import { cloneElement } from 'react';
98+
import { List, ListActions, Button } from 'react-admin';
99+
import IconEvent from '@material-ui/icons/Event';
100+
101+
const ListActions = (props) => (
102+
<TopToolbar>
103+
{cloneElement(props.filters, { context: 'button' })}
104+
<CreateButton/>
105+
<ExportButton/>
106+
{/* Add your custom actions */}
107+
<Button
108+
onClick={() => { alert('Your custom action'); }}
109+
label="Show calendar"
110+
>
111+
<IconEvent/>
112+
</Button>
113+
</TopToolbar>
114+
);
115+
116+
export const PostList = (props) => (
117+
<List {...props} actions={<ListActions/>}>
118+
...
119+
</List>
120+
);
121+
```
122+
123+
This allows you to further control how the default actions behave. For example, you could disable the `<ExportButton>` when the list is empty:
124+
95125
{% raw %}
96126
```jsx
97127
import * as React from 'react';
@@ -103,46 +133,25 @@ import {
103133
CreateButton,
104134
ExportButton,
105135
Button,
106-
sanitizeListRestProps,
136+
sanitizeListRestProps
107137
} from 'react-admin';
108138
import IconEvent from '@material-ui/icons/Event';
109139

110140
const ListActions = (props) => {
111141
const {
112142
className,
113-
exporter,
114143
filters,
115144
maxResults,
116145
...rest
117146
} = props;
118147
const {
119-
currentSort,
120-
resource,
121-
displayedFilters,
122-
filterValues,
123-
hasCreate,
124-
basePath,
125-
selectedIds,
126-
showFilter,
127148
total,
128149
} = useListContext();
129150
return (
130151
<TopToolbar className={className} {...sanitizeListRestProps(rest)}>
131-
{filters && cloneElement(filters, {
132-
resource,
133-
showFilter,
134-
displayedFilters,
135-
filterValues,
136-
context: 'button',
137-
})}
138-
<CreateButton basePath={basePath} />
139-
<ExportButton
140-
disabled={total === 0}
141-
resource={resource}
142-
sort={currentSort}
143-
filterValues={filterValues}
144-
maxResults={maxResults}
145-
/>
152+
{cloneElement(filters, { context: 'button' })}
153+
<CreateButton />
154+
<ExportButton disabled={total === 0} maxResults={maxResults} />
146155
{/* Add your custom actions */}
147156
<Button
148157
onClick={() => { alert('Your custom action'); }}

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

+28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ import { ToolbarProps } from '@material-ui/core';
1515
import TopToolbar from '../layout/TopToolbar';
1616
import { CreateButton, ExportButton } from '../button';
1717

18+
/**
19+
* Action Toolbar for the List view
20+
*
21+
* Internal component. If you want to add or remove actions for a List view,
22+
* write your own ListActions Component. Then, in the <List> component,
23+
* use it in the `actions` prop to pass a custom component.
24+
*
25+
* @example
26+
* import { cloneElement } from 'react';
27+
* import Button from '@material-ui/core/Button';
28+
* import { TopToolbar, List, CreateButton, ExportButton } from 'react-admin';
29+
*
30+
* const PostListActions = ({ basePath, filters }) => (
31+
* <TopToolbar>
32+
* { cloneElement(filters, { context: 'button' }) }
33+
* <CreateButton/>
34+
* <ExportButton/>
35+
* // Add your custom actions here //
36+
* <Button onClick={customAction}>Custom Action</Button>
37+
* </TopToolbar>
38+
* );
39+
*
40+
* export const PostList = (props) => (
41+
* <List actions={<PostListActions />} {...props}>
42+
* ...
43+
* </List>
44+
* );
45+
*/
1846
const ListActions: FC<ListActionsProps> = props => {
1947
const { className, exporter, filters, ...rest } = props;
2048
const {

0 commit comments

Comments
 (0)