Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Fix Datagrid can't handle dynamic children #3635

Merged
merged 7 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,29 @@ const PostList = ({ classes, ...props }) => (
export default withStyles(styles)(PostList);
```

### Performance

when displaying large pages of data, you might experience some performance issues.
This is mostly due to the fact that we iterate over the `<Datagrid>` children and clone them.

In such cases, you can opt-in for an optimized version of the `<Datagrid>` by setting its `optimized` prop to `true`.
Be aware that you can't have dynamic children, such as those displayed or hidden by checking permissions, when using this mode.

```jsx
const PostList = props => (
<List {...props}>
<Datagrid optimized>
<TextField source="id" />
<TextField source="title" />
<TextField source="views" />
</Datagrid>
</List>
);

export default withStyles(styles)(PostList);
```


## The `<SimpleList>` component

For mobile devices, a `<Datagrid>` is often unusable - there is simply not enough space to display several columns. The convention in that case is to use a simple list, with only one column per row. The `<SimpleList>` component serves that purpose, leveraging [material-ui's `<List>` and `<ListItem>` components](https://material-ui.com/demos/lists/). You can use it as `<List>` or `<ReferenceManyField>` child:
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/list/Datagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import Checkbox from '@material-ui/core/Checkbox';
import classnames from 'classnames';

import DatagridHeaderCell from './DatagridHeaderCell';
import DatagridBody from './DatagridBody';
import DatagridLoading from './DatagridLoading';
import MemoDatagridBody, { DatagridBody } from './DatagridBody';

const useStyles = makeStyles(theme => ({
table: {
Expand Down Expand Up @@ -103,7 +103,8 @@ function Datagrid({ classes: classesOverride, ...props }) {
const classes = useStyles({ classes: classesOverride });
const {
basePath,
body,
optimized = false,
body = optimized ? <MemoDatagridBody /> : <DatagridBody />,
children,
className,
currentSort,
Expand Down Expand Up @@ -289,7 +290,6 @@ Datagrid.defaultProps = {
hasBulkActions: false,
ids: [],
selectedIds: [],
body: <DatagridBody />,
};

export default Datagrid;
83 changes: 39 additions & 44 deletions packages/ra-ui-materialui/src/list/DatagridBody.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { cloneElement, useMemo } from 'react';
import React, { cloneElement, memo } from 'react';
import PropTypes from 'prop-types';
import TableBody from '@material-ui/core/TableBody';
import classnames from 'classnames';

import DatagridRow from './DatagridRow';
import PureDatagridRow, { DatagridRow } from './DatagridRow';

const DatagridBody = ({
export const DatagridBody = ({
basePath,
children,
classes,
Expand All @@ -24,45 +24,36 @@ const DatagridBody = ({
styles,
version,
...rest
}) =>
useMemo(
() => (
<TableBody
className={classnames('datagrid-body', className)}
{...rest}
>
{ids.map((id, rowIndex) =>
cloneElement(
row,
{
basePath,
classes,
className: classnames(classes.row, {
[classes.rowEven]: rowIndex % 2 === 0,
[classes.rowOdd]: rowIndex % 2 !== 0,
[classes.clickableRow]: rowClick,
}),
expand,
hasBulkActions,
hover,
id,
key: id,
onToggleItem,
record: data[id],
resource,
rowClick,
selected: selectedIds.includes(id),
style: rowStyle
? rowStyle(data[id], rowIndex)
: null,
},
children
)
)}
</TableBody>
),
[version, data, selectedIds, JSON.stringify(ids), hasBulkActions] // eslint-disable-line
);
}) => (
<TableBody className={classnames('datagrid-body', className)} {...rest}>
{ids.map((id, rowIndex) =>
cloneElement(
row,
{
basePath,
classes,
className: classnames(classes.row, {
[classes.rowEven]: rowIndex % 2 === 0,
[classes.rowOdd]: rowIndex % 2 !== 0,
[classes.clickableRow]: rowClick,
}),
expand,
hasBulkActions,
hover,
id,
key: id,
onToggleItem,
record: data[id],
resource,
rowClick,
selected: selectedIds.includes(id),
style: rowStyle ? rowStyle(data[id], rowIndex) : null,
},
children
)
)}
</TableBody>
);

DatagridBody.propTypes = {
basePath: PropTypes.string,
Expand Down Expand Up @@ -91,7 +82,11 @@ DatagridBody.defaultProps = {
row: <DatagridRow />,
};

const MemoDatagridBody = memo(DatagridBody);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo doesn't work here as children will always change

// trick material-ui Table into thinking this is one of the child type it supports
DatagridBody.muiName = 'TableBody';
MemoDatagridBody.muiName = 'TableBody';
MemoDatagridBody.defaultProps = {
row: <PureDatagridRow />,
};

export default DatagridBody;
export default MemoDatagridBody;
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/list/DatagridRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import React, {
useCallback,
memo,
} from 'react';
import isEqual from 'lodash/isEqual';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { useDispatch } from 'react-redux';
import { push } from 'connected-react-router';
import { TableCell, TableRow, Checkbox } from '@material-ui/core';
import { linkToRecord } from 'ra-core';
import isEqual from 'lodash/isEqual';

import DatagridCell from './DatagridCell';
import ExpandRowButton from './ExpandRowButton';
Expand All @@ -28,7 +28,7 @@ const computeNbColumns = (expand, children, hasBulkActions) =>

const defaultClasses = {};

const DatagridRow = ({
export const DatagridRow = ({
basePath,
children,
classes = defaultClasses,
Expand Down