Skip to content

Commit d8d4ced

Browse files
authored
Merge pull request #6164 from andrico1234/feature/add-empty-datagrid-component
Feature: Add empty Datagrid component
2 parents f1e47f7 + ac7dc19 commit d8d4ced

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docs/List.md

+5
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ Here are all the props accepted by the component:
19811981
* [`isRowExpandable`](#isrowexpandable)
19821982
* [`isRowSelectable`](#isrowselectable)
19831983
* [`optimized`](#performance)
1984+
* [`empty`](#empty)
19841985

19851986
Additional props are passed down to [the material-ui `<Table>` element](https://material-ui.com/api/table/).
19861987

@@ -2261,6 +2262,10 @@ const PostList = props => (
22612262
export default withStyles(styles)(PostList);
22622263
```
22632264

2265+
### Empty
2266+
2267+
It's possible that a Datagrid will have no records to display. If the Datagrid's parent component handles the loading state, the Datagrid will return `null` and render nothing.
2268+
Passing through a component to the `empty` prop will cause the Datagrid to render the `empty` component instead of `null`.
22642269
### CSS API
22652270

22662271
The `Datagrid` component accepts the usual `className` prop but you can override many class names injected to the inner components by React-admin thanks to the `classes` property (as most Material UI components, see their [documentation about it](https://material-ui.com/customization/components/#overriding-styles-with-classes)). This property accepts the following keys:

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

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ describe('<Datagrid />', () => {
4747
expect(contextValue.onSelect).toHaveBeenCalledTimes(0);
4848
});
4949

50+
it('should display the correct empty component', () => {
51+
const Empty = () => <div>No records to show</div>;
52+
53+
const emptyData = {
54+
...contextValue,
55+
data: [],
56+
ids: [],
57+
};
58+
59+
const { queryByText } = renderWithRedux(
60+
<ListContextProvider value={emptyData}>
61+
<Datagrid empty={<Empty />} hasBulkActions>
62+
<TitleField />
63+
</Datagrid>
64+
</ListContextProvider>
65+
);
66+
67+
expect(queryByText('No records to show')).toBeTruthy();
68+
});
69+
5070
describe('selecting items with the shift key', () => {
5171
it('should call onSelect with the correct ids when the last selection is after the first', () => {
5272
const Test = ({ selectedIds = [] }) => (

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
117117
children,
118118
classes: classesOverride,
119119
className,
120+
empty,
120121
expand,
121122
hasBulkActions = false,
122123
hover,
@@ -242,9 +243,13 @@ const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
242243
/**
243244
* Once loaded, the data for the list may be empty. Instead of
244245
* displaying the table header with zero data rows,
245-
* the datagrid displays nothing in this case.
246+
* the datagrid displays nothing or a custom empty component.
246247
*/
247248
if (loaded && (ids.length === 0 || total === 0)) {
249+
if (empty) {
250+
return empty;
251+
}
252+
248253
return null;
249254
}
250255

@@ -353,6 +358,7 @@ Datagrid.propTypes = {
353358
order: PropTypes.string.isRequired,
354359
}),
355360
data: PropTypes.any,
361+
empty: PropTypes.element,
356362
// @ts-ignore
357363
expand: PropTypes.oneOfType([PropTypes.element, PropTypes.elementType]),
358364
hasBulkActions: PropTypes.bool,
@@ -387,6 +393,7 @@ export interface DatagridProps<RecordType extends Record = Record>
387393
}>;
388394
hasBulkActions?: boolean;
389395
hover?: boolean;
396+
empty?: ReactElement;
390397
isRowSelectable?: (record: Record) => boolean;
391398
isRowExpandable?: (record: Record) => boolean;
392399
optimized?: boolean;

0 commit comments

Comments
 (0)