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 1 commit
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
77 changes: 34 additions & 43 deletions packages/ra-ui-materialui/src/list/DatagridBody.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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';
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,7 @@ 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';

export default DatagridBody;
MemoDatagridBody.muiName = 'TableBody';
export default MemoDatagridBody;
9 changes: 1 addition & 8 deletions packages/ra-ui-materialui/src/list/DatagridRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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';
Expand Down Expand Up @@ -207,13 +206,7 @@ DatagridRow.defaultProps = {
selected: false,
};

const areEqual = (prevProps, nextProps) => {
const { children: _, ...prevPropsWithoutChildren } = prevProps;
const { children: __, ...nextPropsWithoutChildren } = nextProps;
return isEqual(prevPropsWithoutChildren, nextPropsWithoutChildren);
};

const PureDatagridRow = memo(DatagridRow, areEqual);
const PureDatagridRow = memo(DatagridRow);

PureDatagridRow.displayName = 'PureDatagridRow';

Expand Down