-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
ListView.tsx
337 lines (311 loc) · 9.49 KB
/
ListView.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
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { ReactElement, ReactNode, ElementType } from 'react';
import { SxProps } from '@mui/system';
import Card from '@mui/material/Card';
import clsx from 'clsx';
import { useListContext, RaRecord } from 'ra-core';
import { Title } from '../layout/Title';
import { ListToolbar } from './ListToolbar';
import { Pagination as DefaultPagination } from './pagination';
import { ListActions as DefaultActions } from './ListActions';
import { Empty } from './Empty';
const defaultActions = <DefaultActions />;
const defaultPagination = <DefaultPagination />;
const defaultEmpty = <Empty />;
const DefaultComponent = Card;
export const ListView = <RecordType extends RaRecord = any>(
props: ListViewProps
) => {
const {
actions = defaultActions,
aside,
filters,
emptyWhileLoading,
pagination = defaultPagination,
children,
className,
component: Content = DefaultComponent,
title,
empty = defaultEmpty,
...rest
} = props;
const { defaultTitle, data, error, isPending, filterValues, resource } =
useListContext<RecordType>();
if (!children || (!data && isPending && emptyWhileLoading)) {
return null;
}
const renderList = () => (
<div className={ListClasses.main}>
{(filters || actions) && (
<ListToolbar
className={ListClasses.actions}
filters={filters}
actions={actions}
/>
)}
<Content className={ListClasses.content}>{children}</Content>
{!error && pagination !== false && pagination}
</div>
);
const renderEmpty = () =>
empty !== false && <div className={ListClasses.noResults}>{empty}</div>;
const shouldRenderEmptyPage =
!isPending &&
data?.length === 0 &&
!Object.keys(filterValues).length &&
empty !== false;
return (
<Root className={clsx('list-page', className)} {...rest}>
<Title
title={title}
defaultTitle={defaultTitle}
preferenceKey={`${resource}.list.title`}
/>
{shouldRenderEmptyPage ? renderEmpty() : renderList()}
{aside}
</Root>
);
};
export interface ListViewProps {
/**
* The actions to display in the toolbar. defaults to Filter + Create + Export.
*
* @see https://marmelab.com/react-admin/List.html#actions
* @example
* import {
* CreateButton,
* DatagridConfigurable,
* ExportButton,
* FilterButton,
* List,
* SelectColumnsButton,
* TopToolbar,
* } from 'react-admin';
* import IconEvent from '@mui/icons-material/Event';
*
* const ListActions = () => (
* <TopToolbar>
* <SelectColumnsButton />
* <FilterButton/>
* <CreateButton/>
* <ExportButton/>
* </TopToolbar>
* );
*
* export const PostList = () => (
* <List actions={<ListActions/>}>
* <DatagridConfigurable>
* ...
* </DatagridConfigurable>
* </List>
* );
*/
actions?: ReactElement | false;
/**
* The content to render as a sidebar.
* @see https://marmelab.com/react-admin/List.html#aside
* @example
* import { List, useListContext } from 'react-admin';
* import { Typography } from '@mui/material';
*
* const Aside = () => {
* const { data, isPending } = useListContext();
* if (isPending) return null;
* return (
* <div style={{ width: 200, margin: '4em 1em' }}>
* <Typography variant="h6">Posts stats</Typography>
* <Typography variant="body2">
* Total views: {data.reduce((sum, post) => sum + post.views, 0)}
* </Typography>
* </div>
* );
* };
*
* const PostList = () => (
* <List aside={<Aside />}>
* ...
* </List>
* );
*/
aside?: ReactElement;
/**
* A class name to apply to the root div element
*/
className?: string;
/**
* The components rendering the list of records. Usually a <Datagrid> or <SimpleList>.
*
* @see https://marmelab.com/react-admin/List.html#children
* @example
* import { List, Datagrid, TextField, DateField, NumberField, BooleanField, ReferenceManyCount } from 'react-admin';
*
* export const BookList = () => (
* <List>
* <Datagrid rowClick="edit">
* <TextField source="id" />
* <TextField source="title" />
* <DateField source="published_at" />
* <ReferenceManyCount label="Nb comments" reference="comments" target="post_id" link />
* <BooleanField source="commentable" label="Com." />
* <NumberField source="nb_views" label="Views" />
* </Datagrid>
* </List>
* );
*/
children: ReactNode;
/**
* The component used to display the list. Defaults to <Card>.
*
* @see https://marmelab.com/react-admin/List.html#component
* @example
* import { List } from 'react-admin';
*
* const PostList = () => (
* <List component="div">
* ...
* </List>
* );
*/
component?: ElementType;
/**
* The component to display when the list is empty.
*
* @see https://marmelab.com/react-admin/List.html#empty
* @example
* import { CreateButton, List } from 'react-admin';
* import { Box, Button, Typography } from '@mui/material';
*
* const Empty = () => (
* <Box textAlign="center" m={1}>
* <Typography variant="h4" paragraph>
* No products available
* </Typography>
* <Typography variant="body1">
* Create one or import products from a file
* </Typography>
* <CreateButton />
* <Button onClick={...}>Import</Button>
* </Box>
* );
*
* const ProductList = () => (
* <List empty={<Empty />}>
* ...
* </List>
* );
*/
empty?: ReactElement | false;
/**
* Set to true to return null while the list is loading.
*
* @see https://marmelab.com/react-admin/List.html#emptywhileloading
* @example
* import { List } from 'react-admin';
* import { SimpleBookList } from './BookList';
*
* const BookList = () => (
* <List emptyWhileLoading>
* <SimpleBookList />
* </List>
* );
*/
emptyWhileLoading?: boolean;
/**
* The filter inputs to display in the toolbar.
*
* @see https://marmelab.com/react-admin/List.html#filters
* @example
* import { List, TextInput } from 'react-admin';
*
* const postFilters = [
* <TextInput label="Search" source="q" alwaysOn />,
* <TextInput label="Title" source="title" defaultValue="Hello, World!" />,
* ];
*
* export const PostList = () => (
* <List filters={postFilters}>
* ...
* </List>
* );
*/
filters?: ReactElement | ReactElement[];
/**
* The pagination component to display. defaults to <Pagination />
*
* @see https://marmelab.com/react-admin/List.html#pagination
* @example
* import { Pagination, List } from 'react-admin';
*
* const PostPagination = props => <Pagination rowsPerPageOptions={[10, 25, 50, 100]} {...props} />;
*
* export const PostList = () => (
* <List pagination={<PostPagination />}>
* ...
* </List>
* );
*/
pagination?: ReactElement | false;
/**
* The page title (main title) to display above the data. Defaults to the humanized resource name.
*
* @see https://marmelab.com/react-admin/List.html#title
* @example
* import { List } from 'react-admin';
*
* export const PostList = () => (
* <List title="List of posts">
* ...
* </List>
* );
*/
title?: string | ReactElement;
/**
* The CSS styles to apply to the component.
*
* @see https://marmelab.com/react-admin/List.html#sx-css-api
* @example
* const PostList = () => (
* <List
* sx={{
* backgroundColor: 'yellow',
* '& .RaList-content': {
* backgroundColor: 'red',
* },
* }}
* >
* ...
* </List>
* );
*/
sx?: SxProps;
}
const PREFIX = 'RaList';
export const ListClasses = {
main: `${PREFIX}-main`,
content: `${PREFIX}-content`,
actions: `${PREFIX}-actions`,
noResults: `${PREFIX}-noResults`,
};
const Root = styled('div', {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
display: 'flex',
[`& .${ListClasses.main}`]: {
flex: '1 1 auto',
display: 'flex',
flexDirection: 'column',
},
[`& .${ListClasses.content}`]: {
position: 'relative',
[theme.breakpoints.down('sm')]: {
boxShadow: 'none',
},
overflow: 'inherit',
},
[`& .${ListClasses.actions}`]: {},
[`& .${ListClasses.noResults}`]: {
flex: 1,
},
}));