-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathArrayField.tsx
205 lines (193 loc) · 5.16 KB
/
ArrayField.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
import * as React from 'react';
import {
cloneElement,
Children,
useEffect,
useState,
memo,
FC,
ReactElement,
} from 'react';
import get from 'lodash/get';
import { Identifier, ListContextProvider, useRecordContext } from 'ra-core';
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
import PropTypes from 'prop-types';
const initialState = {
data: {},
ids: [],
};
const getDataAndIds = (
record: object,
source: string,
fieldKey: string
): State => {
const list = get(record, source);
if (!list) {
return initialState;
}
return fieldKey
? {
data: list.reduce((prev, item) => {
prev[item[fieldKey]] = item;
return prev;
}, {}),
ids: list.map(item => item[fieldKey]),
}
: {
data: list.reduce((prev, item) => {
prev[JSON.stringify(item)] = item;
return prev;
}, {}),
ids: list.map(JSON.stringify),
};
};
/**
* Display a collection
*
* Ideal for embedded arrays of objects, e.g.
* {
* id: 123
* tags: [
* { name: 'foo' },
* { name: 'bar' }
* ]
* }
*
* The child must be an iterator component
* (like <Datagrid> or <SingleFieldList>).
*
* @example Display all the backlinks of the current post as a <Datagrid>
* // post = {
* // id: 123
* // backlinks: [
* // {
* // uuid: '34fdf393-f449-4b04-a423-38ad02ae159e',
* // date: '2012-08-10T00:00:00.000Z',
* // url: 'http://example.com/foo/bar.html',
* // },
* // {
* // uuid: 'd907743a-253d-4ec1-8329-404d4c5e6cf1',
* // date: '2012-08-14T00:00:00.000Z',
* // url: 'https://blog.johndoe.com/2012/08/12/foobar.html',
* // }
* // ]
* // }
* <ArrayField source="backlinks">
* <Datagrid>
* <DateField source="date" />
* <UrlField source="url" />
* </Datagrid>
* </ArrayField>
*
* @example Display all the tags of the current post as <Chip> components
* // post = {
* // id: 123
* // tags: [
* // { name: 'foo' },
* // { name: 'bar' }
* // ]
* // }
* <ArrayField source="tags">
* <SingleFieldList>
* <ChipField source="name" />
* </SingleFieldList>
* </ArrayField>
*
* If the array value contains a lot of items, you may experience slowdowns in the UI.
* In such cases, set the `fieldKey` prop to use one field as key, and reduce CPU and memory usage:
*
* @example
* <ArrayField source="backlinks" fieldKey="uuid">
* ...
* </ArrayField>
*
* If you need to render a collection in a custom way, it's often simpler
* to write your own component:
*
* @example
* const TagsField = ({ record }) => (
* <ul>
* {record.tags.map(item => (
* <li key={item.name}>{item.name}</li>
* ))}
* </ul>
* );
* TagsField.defaultProps = { addLabel: true };
*/
export const ArrayField: FC<ArrayFieldProps> = memo(props => {
const {
addLabel,
basePath,
children,
record: _record,
resource,
sortable,
source,
fieldKey,
...rest
} = props;
const record = useRecordContext(props);
const [ids, setIds] = useState(initialState.ids);
const [data, setData] = useState(initialState.data);
useEffect(() => {
const { ids, data } = getDataAndIds(record, source, fieldKey);
setIds(ids);
setData(data);
}, [record, source, fieldKey]);
return (
<ListContextProvider
value={{
ids,
data,
loading: false,
basePath,
selectedIds: [],
currentSort: { field: null, order: null },
displayedFilters: null,
filterValues: null,
hasCreate: null,
hideFilter: null,
loaded: true,
onSelect: null,
onToggleItem: null,
onUnselectItems: null,
page: null,
perPage: null,
resource,
setFilters: null,
setPage: null,
setPerPage: null,
setSort: null,
showFilter: null,
total: null,
}}
>
{cloneElement(Children.only(children), {
ids,
data,
loading: false,
basePath,
currentSort: { field: null, order: null },
resource,
...rest,
})}
</ListContextProvider>
);
});
ArrayField.defaultProps = {
addLabel: true,
};
ArrayField.propTypes = {
...fieldPropTypes,
fieldKey: PropTypes.string,
};
export interface ArrayFieldProps extends PublicFieldProps, InjectedFieldProps {
fieldKey?: string;
children: ReactElement;
}
interface State {
data: object;
ids: Identifier[];
}
ArrayField.displayName = 'ArrayField';
export default ArrayField;