-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Datagrid.js
276 lines (265 loc) · 8.27 KB
/
Datagrid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, {
isValidElement,
Children,
cloneElement,
useCallback,
} from 'react';
import PropTypes from 'prop-types';
import { sanitizeListRestProps } from 'ra-core';
import { makeStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Checkbox from '@material-ui/core/Checkbox';
import classnames from 'classnames';
import DatagridHeaderCell from './DatagridHeaderCell';
import DatagridLoading from './DatagridLoading';
import DatagridBody, { PureDatagridBody } from './DatagridBody';
const useStyles = makeStyles(theme => ({
table: {
tableLayout: 'auto',
},
thead: {},
tbody: {},
headerRow: {},
headerCell: {},
checkbox: {},
row: {},
clickableRow: {
cursor: 'pointer',
},
rowEven: {},
rowOdd: {},
rowCell: {},
expandHeader: {
padding: 0,
width: theme.spacing(6),
},
expandIconCell: {
width: theme.spacing(6),
},
expandIcon: {
padding: theme.spacing(1),
transform: 'rotate(-90deg)',
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
},
expanded: {
transform: 'rotate(0deg)',
},
}));
/**
* The Datagrid component renders a list of records as a table.
* It is usually used as a child of the <List> and <ReferenceManyField> components.
*
* Props:
* - rowStyle
*
* @example Display all posts as a datagrid
* const postRowStyle = (record, index) => ({
* backgroundColor: record.nb_views >= 500 ? '#efe' : 'white',
* });
* export const PostList = (props) => (
* <List {...props}>
* <Datagrid rowStyle={postRowStyle}>
* <TextField source="id" />
* <TextField source="title" />
* <TextField source="body" />
* <EditButton />
* </Datagrid>
* </List>
* );
*
* @example Display all the comments of the current post as a datagrid
* <ReferenceManyField reference="comments" target="post_id">
* <Datagrid>
* <TextField source="id" />
* <TextField source="body" />
* <DateField source="created_at" />
* <EditButton />
* </Datagrid>
* </ReferenceManyField>
*/
function Datagrid({ classes: classesOverride, ...props }) {
const classes = useStyles({ classes: classesOverride });
const {
basePath,
optimized = false,
body = optimized ? <PureDatagridBody /> : <DatagridBody />,
children,
className,
currentSort,
data,
expand,
hasBulkActions,
hover,
ids,
loading,
loaded,
onSelect,
onToggleItem,
resource,
rowClick,
rowStyle,
selectedIds,
setSort,
size = 'small',
total,
version,
...rest
} = props;
const updateSort = useCallback(
event => {
event.stopPropagation();
setSort(event.currentTarget.dataset.sort);
},
[setSort]
);
const handleSelectAll = useCallback(
event => {
if (event.target.checked) {
onSelect(
ids.concat(selectedIds.filter(id => !ids.includes(id)))
);
} else {
onSelect([]);
}
},
[ids, onSelect, selectedIds]
);
/**
* if loaded is false, the list displays for the first time, and the dataProvider hasn't answered yet
* if loaded is true, the data for the list has at least been returned once by the dataProvider
* if loaded is undefined, the Datagrid parent doesn't track loading state (e.g. ReferenceArrayField)
*/
if (loaded === false) {
return (
<DatagridLoading
classes={classes}
className={className}
expand={expand}
hasBulkActions={hasBulkActions}
nbChildren={React.Children.count(children)}
size={size}
/>
);
}
/**
* Once loaded, the data for the list may be empty. Instead of
* displaying the table header with zero data rows,
* the datagrid displays nothing in this case.
*/
if (loaded && (ids.length === 0 || total === 0)) {
return null;
}
/**
* After the initial load, if the data for the list isn't empty,
* and even if the data is refreshing (e.g. after a filter change),
* the datagrid displays the current data.
*/
return (
<Table
className={classnames(classes.table, className)}
size={size}
{...sanitizeListRestProps(rest)}
>
<TableHead className={classes.thead}>
<TableRow
className={classnames(classes.row, classes.headerRow)}
>
{expand && (
<TableCell
padding="none"
className={classes.expandHeader}
/>
)}
{hasBulkActions && (
<TableCell padding="checkbox">
<Checkbox
className="select-all"
color="primary"
checked={
selectedIds.length > 0 &&
ids.length > 0 &&
ids.every(id => selectedIds.includes(id))
}
onChange={handleSelectAll}
/>
</TableCell>
)}
{Children.map(children, (field, index) =>
isValidElement(field) ? (
<DatagridHeaderCell
className={classes.headerCell}
currentSort={currentSort}
field={field}
isSorting={
currentSort.field ===
(field.props.sortBy || field.props.source)
}
key={field.props.source || index}
resource={resource}
updateSort={updateSort}
/>
) : null
)}
</TableRow>
</TableHead>
{cloneElement(
body,
{
basePath,
className: classes.tbody,
classes,
expand,
rowClick,
data,
hasBulkActions,
hover,
ids,
onToggleItem,
resource,
rowStyle,
selectedIds,
version,
},
children
)}
</Table>
);
}
Datagrid.propTypes = {
basePath: PropTypes.string,
body: PropTypes.element,
children: PropTypes.node.isRequired,
classes: PropTypes.object,
className: PropTypes.string,
currentSort: PropTypes.shape({
field: PropTypes.string,
order: PropTypes.string,
}),
data: PropTypes.object.isRequired,
expand: PropTypes.oneOfType([PropTypes.element, PropTypes.elementType]),
hasBulkActions: PropTypes.bool.isRequired,
hover: PropTypes.bool,
ids: PropTypes.arrayOf(PropTypes.any).isRequired,
loading: PropTypes.bool,
onSelect: PropTypes.func,
onToggleItem: PropTypes.func,
resource: PropTypes.string,
rowClick: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
rowStyle: PropTypes.func,
selectedIds: PropTypes.arrayOf(PropTypes.any).isRequired,
setSort: PropTypes.func,
total: PropTypes.number,
version: PropTypes.number,
};
Datagrid.defaultProps = {
data: {},
hasBulkActions: false,
ids: [],
selectedIds: [],
};
export default Datagrid;