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

Fix sorting a list using a <Datagrid> inside a <ReferenceManyField> #5094

Merged
merged 10 commits into from
Aug 10, 2020
4 changes: 2 additions & 2 deletions cypress/support/ListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default url => ({
recordRows: '.datagrid-body tr',
viewsColumn: '.datagrid-body tr td:nth-child(7)',
datagridHeaders: 'th',
sortBy: name => `th span[data-sort="${name}"]`,
sortBy: name => `th span[data-field="${name}"]`,
svg: (name, criteria = '') =>
`th span[data-sort="${name}"] svg${criteria}`,
`th span[data-field="${name}"] svg${criteria}`,
logout: '.logout',
bulkActionsToolbar: '[data-test=bulk-actions-toolbar]',
customBulkActionsButton:
Expand Down
15 changes: 10 additions & 5 deletions packages/ra-ui-materialui/src/list/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ const Datagrid: FC<DatagridProps> = props => {
const updateSort = useCallback(
event => {
event.stopPropagation();
setSort(
event.currentTarget.dataset.sort,
event.currentTarget.dataset.order
);
const newField = event.currentTarget.dataset.field;
const newOrder =
currentSort.field === newField
? currentSort.order === 'ASC'
? 'DESC'
: 'ASC'
: event.currentTarget.dataset.order;

setSort(newField, newOrder);
},
[setSort]
[currentSort.field, currentSort.order, setSort]
);

const handleSelectAll = useCallback(
Expand Down
14 changes: 8 additions & 6 deletions packages/ra-ui-materialui/src/list/DatagridHeaderCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const DatagridHeaderCell = props => {
} = props;
const classes = useStyles(props);
const translate = useTranslate();

return (
<TableCell
className={classnames(className, field.props.headerClassName)}
Expand All @@ -60,7 +61,8 @@ export const DatagridHeaderCell = props => {
(field.props.sortBy || field.props.source)
}
direction={currentSort.order === 'ASC' ? 'asc' : 'desc'}
data-sort={field.props.sortBy || field.props.source}
data-sort={field.props.sortBy || field.props.source} // @deprecated. Use data-field instead.
data-field={field.props.sortBy || field.props.source}
data-order={field.props.sortByOrder || 'ASC'}
onClick={updateSort}
classes={classes}
Expand Down Expand Up @@ -97,11 +99,11 @@ DatagridHeaderCell.propTypes = {
updateSort: PropTypes.func.isRequired,
};

export default memo(
DatagridHeaderCell,
(props, nextProps) =>
export default memo(DatagridHeaderCell, (props, nextProps) => {
return (
props.updateSort === nextProps.updateSort &&
props.currentSort.sort === nextProps.currentSort.sort &&
props.currentSort.field === nextProps.currentSort.field &&
props.currentSort.order === nextProps.currentSort.order &&
!(nextProps.isSorting && props.sortable !== nextProps.sortable)
);
);
});
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/list/DatagridHeaderCell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('<DatagridHeaderCell />', () => {
</tbody>
</table>
);
expect(getByTitle('ra.action.sort').dataset.sort).toBe('title');
expect(getByTitle('ra.action.sort').dataset.field).toBe('title');
});

it('should be enabled when field has a sortBy props', () => {
Expand All @@ -64,7 +64,7 @@ describe('<DatagridHeaderCell />', () => {
</tbody>
</table>
);
expect(getByTitle('ra.action.sort').dataset.sort).toBe('title');
expect(getByTitle('ra.action.sort').dataset.field).toBe('title');
});

it('should be change order when field has a sortByOrder props', () => {
Expand Down