-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Datagrid.tsx
343 lines (329 loc) · 10.3 KB
/
Datagrid.tsx
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import * as React from 'react';
import {
isValidElement,
Children,
cloneElement,
useCallback,
FC,
ReactElement,
} from 'react';
import PropTypes from 'prop-types';
import {
sanitizeListRestProps,
useListContext,
useVersion,
Identifier,
Record,
} from 'ra-core';
import {
Checkbox,
Table,
TableProps,
TableCell,
TableHead,
TableRow,
} from '@material-ui/core';
import classnames from 'classnames';
import DatagridHeaderCell from './DatagridHeaderCell';
import DatagridLoading from './DatagridLoading';
import DatagridBody, { PureDatagridBody } from './DatagridBody';
import useDatagridStyles from './useDatagridStyles';
import { ClassesOverride } from '../../types';
/**
* 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>
*
*
* @example Usage outside of a <List> or a <ReferenceManyField>.
*
* const currentSort = { field: 'published_at', order: 'DESC' };
*
* export const MyCustomList = (props) => {
* const { ids, data, total, loaded } = useGetList(
* 'posts',
* { page: 1, perPage: 10 },
* currentSort
* );
*
* return (
* <Datagrid
* basePath=""
* currentSort={currentSort}
* data={data}
* ids={ids}
* selectedIds={[]}
* loaded={loaded}
* total={total}
* setSort={() => {
* console.log('set sort');
* }}
* onSelect={() => {
* console.log('on select');
* }}
* onToggleItem={() => {
* console.log('on toggle item');
* }}
* >
* <TextField source="id" />
* <TextField source="title" />
* </Datagrid>
* );
* }
*/
const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
const classes = useDatagridStyles(props);
const {
optimized = false,
body = optimized ? <PureDatagridBody /> : <DatagridBody />,
children,
classes: classesOverride,
className,
expand,
hasBulkActions = false,
hover,
isRowSelectable,
resource,
rowClick,
rowStyle,
size = 'small',
...rest
} = props;
const {
basePath,
currentSort,
data,
ids,
loaded,
onSelect,
onToggleItem,
selectedIds,
setSort,
total,
} = useListContext(props);
const version = useVersion();
const updateSort = useCallback(
event => {
event.stopPropagation();
const newField = event.currentTarget.dataset.field;
const newOrder =
currentSort.field === newField
? currentSort.order === 'ASC'
? 'DESC'
: 'ASC'
: event.currentTarget.dataset.order;
setSort(newField, newOrder);
},
[currentSort.field, currentSort.order, setSort]
);
const handleSelectAll = useCallback(
event => {
if (event.target.checked) {
const all = ids.concat(
selectedIds.filter(id => !ids.includes(id))
);
onSelect(
isRowSelectable
? all.filter(id => isRowSelectable(data[id]))
: all
);
} else {
onSelect([]);
}
},
[data, ids, onSelect, isRowSelectable, 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;
}
const all = isRowSelectable
? ids.filter(id => isRowSelectable(data[id]))
: ids;
/**
* 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
ref={ref}
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={classnames(
classes.headerCell,
classes.expandHeader
)}
/>
)}
{hasBulkActions && (
<TableCell
padding="checkbox"
className={classes.headerCell}
>
<Checkbox
className="select-all"
color="primary"
checked={
selectedIds.length > 0 &&
all.length > 0 &&
all.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 as any).sortBy ||
(field.props as any).source)
}
key={(field.props as any).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,
isRowSelectable,
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,
// @ts-ignore
expand: PropTypes.oneOfType([PropTypes.element, PropTypes.elementType]),
hasBulkActions: PropTypes.bool,
hover: PropTypes.bool,
ids: PropTypes.arrayOf(PropTypes.any),
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),
setSort: PropTypes.func,
total: PropTypes.number,
version: PropTypes.number,
isRowSelectable: PropTypes.func,
};
type RowClickFunction = (
id: Identifier,
basePath: string,
record: Record
) => string;
export interface DatagridProps extends Omit<TableProps, 'size' | 'classes'> {
body?: ReactElement;
classes?: ClassesOverride<typeof useDatagridStyles>;
className?: string;
expand?:
| ReactElement
| FC<{
basePath: string;
id: Identifier;
record: Record;
resource: string;
}>;
hasBulkActions?: boolean;
hover?: boolean;
isRowSelectable?: (record: Record) => boolean;
optimized?: boolean;
rowClick?: string | RowClickFunction;
rowStyle?: (record: Record, index: number) => any;
size?: 'medium' | 'small';
}
export default Datagrid;