Skip to content

Commit 3d6cc31

Browse files
authored
Merge pull request #6115 from marmelab/fix-datagrid-less-props
Fix Datagrid requires too many props when used standalone
2 parents 652d5d8 + d44d7a7 commit 3d6cc31

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

docs/List.md

+22-27
Original file line numberDiff line numberDiff line change
@@ -2819,15 +2819,13 @@ export const UserList = ({ permissions, ...props }) => {
28192819

28202820
### Rendering `<Datagrid>` With A Custom Query
28212821

2822-
You can use the `<Datagrid>` component with [custom queries](./Actions.md#usequery-hook), provided you pass the result to a `<ListContextProvider>`:
2822+
You can use the `<Datagrid>` component with [custom queries](./Actions.md#usequery-hook):
28232823

28242824
{% raw %}
28252825
```jsx
28262826
import keyBy from 'lodash/keyBy'
28272827
import {
28282828
useQuery,
2829-
ResourceContextProvider,
2830-
ListContextProvider,
28312829
Datagrid,
28322830
TextField,
28332831
Pagination,
@@ -2836,13 +2834,14 @@ import {
28362834

28372835
const CustomList = () => {
28382836
const [page, setPage] = useState(1);
2839-
const perPage = 50;
2837+
const [perPage, setPerPage] = useState(25);
2838+
const [sort, setSort] = useState({ field: 'id', order: 'ASC' })
28402839
const { data, total, loading, error } = useQuery({
28412840
type: 'getList',
28422841
resource: 'posts',
28432842
payload: {
28442843
pagination: { page, perPage },
2845-
sort: { field: 'id', order: 'ASC' },
2844+
sort,
28462845
filter: {},
28472846
}
28482847
});
@@ -2854,33 +2853,29 @@ const CustomList = () => {
28542853
return <p>ERROR: {error}</p>
28552854
}
28562855
return (
2857-
<ResourceContextProvider value="posts">
2858-
<ListContextProvider
2859-
value={{
2860-
basePath: '/posts',
2861-
data: keyBy(data, 'id'),
2862-
ids: data.map(({ id }) => id),
2863-
currentSort: { field: 'id', order: 'ASC' },
2864-
selectedIds: [],
2865-
}}
2866-
>
2867-
<Datagrid rowClick="edit">
2868-
<TextField source="id" />
2869-
<TextField source="title" />
2870-
</Datagrid>
2871-
<Pagination
2872-
page={page}
2873-
perPage={perPage}
2874-
setPage={setPage}
2875-
total={total}
2876-
/>
2877-
</ListContextProvider>
2878-
</ResourceContextProvider>
2856+
<Datagrid
2857+
data={keyBy(data, 'id')}
2858+
ids={data.map(({ id }) => id)}
2859+
currentSort={sort}
2860+
setSort={(field, order) => setSort({ field, order })}
2861+
>
2862+
<TextField source="id" />
2863+
<TextField source="title" />
2864+
</Datagrid>
2865+
<Pagination
2866+
page={page}
2867+
setPage={setPage}
2868+
perPage={perPage}
2869+
setPerPage={setPerPage}
2870+
total={total}
2871+
/>
28792872
);
28802873
}
28812874
```
28822875
{% endraw %}
28832876

2877+
**Tip**: If you use the `<Datagrid rowClick>` prop in this case, you must also set the `basePath` prop to let the `<Datagrid>` compute the link to the record pages.
2878+
28842879
## Third-Party Components
28852880

28862881
You can find components for react-admin in third-party repositories.

examples/simple/src/customRouteLayout.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const CustomRouteLayout = () => {
2626
Found <span className="total">{total}</span> posts !
2727
</p>
2828
<Datagrid
29-
basePath=""
29+
basePath="/posts"
3030
currentSort={currentSort}
3131
data={data}
3232
ids={ids}
33-
selectedIds={[]}
3433
loaded={loaded}
3534
total={total}
35+
rowClick="edit"
3636
>
3737
<TextField source="id" sortable={false} />
3838
<TextField source="title" sortable={false} />

packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
147147
isRowExpandable,
148148
]);
149149

150-
const updateSort = useCallback(
150+
const updateSortCallback = useCallback(
151151
event => {
152152
event.stopPropagation();
153153
const newField = event.currentTarget.dataset.field;
@@ -163,6 +163,8 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
163163
[currentSort.field, currentSort.order, setSort]
164164
);
165165

166+
const updateSort = setSort ? updateSortCallback : null;
167+
166168
const handleSelectAll = useCallback(
167169
event => {
168170
if (event.target.checked) {
@@ -184,10 +186,10 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
184186
const lastSelected = useRef(null);
185187

186188
useEffect(() => {
187-
if (selectedIds.length === 0) {
189+
if (!selectedIds || selectedIds.length === 0) {
188190
lastSelected.current = null;
189191
}
190-
}, [selectedIds.length]);
192+
}, [JSON.stringify(selectedIds)]); // eslint-disable-line react-hooks/exhaustive-deps
191193

192194
const handleToggleItem = useCallback(
193195
(id, event) => {
@@ -276,7 +278,7 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
276278
)}
277279
/>
278280
)}
279-
{hasBulkActions && (
281+
{hasBulkActions && selectedIds && (
280282
<TableCell
281283
padding="checkbox"
282284
className={classes.headerCell}
@@ -346,9 +348,9 @@ Datagrid.propTypes = {
346348
children: PropTypes.node.isRequired,
347349
classes: PropTypes.object,
348350
className: PropTypes.string,
349-
currentSort: PropTypes.shape({
350-
field: PropTypes.string,
351-
order: PropTypes.string,
351+
currentSort: PropTypes.exact({
352+
field: PropTypes.string.isRequired,
353+
order: PropTypes.string.isRequired,
352354
}),
353355
data: PropTypes.any,
354356
// @ts-ignore
@@ -393,7 +395,7 @@ export interface DatagridProps<RecordType extends Record = Record>
393395
size?: 'medium' | 'small';
394396
// can be injected when using the component without context
395397
basePath?: string;
396-
currentsort?: SortPayload;
398+
currentSort?: SortPayload;
397399
data?: RecordMap<RecordType>;
398400
ids?: Identifier[];
399401
loaded?: boolean;

packages/ra-ui-materialui/src/list/datagrid/DatagridBody.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const DatagridBody: FC<DatagridBodyProps> = React.forwardRef(
4949
[classes.clickableRow]: rowClick,
5050
}),
5151
expand,
52-
hasBulkActions,
52+
hasBulkActions: hasBulkActions && selectedIds,
5353
hover,
5454
id,
5555
key: id,

packages/ra-ui-materialui/src/list/datagrid/DatagridHeaderCell.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export const DatagridHeaderCell = (
5252
variant="head"
5353
{...rest}
5454
>
55-
{field.props.sortable !== false &&
55+
{updateSort &&
56+
field.props.sortable !== false &&
5657
(field.props.sortBy || field.props.source) ? (
5758
<Tooltip
5859
title={translate('ra.action.sort')}
@@ -103,7 +104,7 @@ DatagridHeaderCell.propTypes = {
103104
}).isRequired,
104105
isSorting: PropTypes.bool,
105106
resource: PropTypes.string,
106-
updateSort: PropTypes.func.isRequired,
107+
updateSort: PropTypes.func,
107108
};
108109

109110
export interface DatagridHeaderCellProps
@@ -114,7 +115,7 @@ export interface DatagridHeaderCellProps
114115
isSorting?: boolean;
115116
resource: string;
116117
currentSort: SortPayload;
117-
updateSort: (event: any) => void;
118+
updateSort?: (event: any) => void;
118119
}
119120

120121
export default memo(

packages/ra-ui-materialui/src/list/datagrid/DatagridRow.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ const DatagridRow: FC<DatagridRowProps> = React.forwardRef((props, ref) => {
110110

111111
const effect =
112112
typeof rowClick === 'function'
113-
? await rowClick(id, basePath, record)
113+
? await rowClick(id, basePath || `/${resource}`, record)
114114
: rowClick;
115115
switch (effect) {
116116
case 'edit':
117-
history.push(linkToRecord(basePath, id));
117+
history.push(linkToRecord(basePath || `/${resource}`, id));
118118
return;
119119
case 'show':
120-
history.push(linkToRecord(basePath, id, 'show'));
120+
history.push(
121+
linkToRecord(basePath || `/${resource}`, id, 'show')
122+
);
121123
return;
122124
case 'expand':
123125
handleToggleExpand(event);
@@ -137,6 +139,7 @@ const DatagridRow: FC<DatagridRowProps> = React.forwardRef((props, ref) => {
137139
handleToggleSelection,
138140
id,
139141
record,
142+
resource,
140143
rowClick,
141144
]
142145
);

0 commit comments

Comments
 (0)