/
ReferenceDialog.tsx
247 lines (209 loc) · 7.03 KB
/
ReferenceDialog.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
import { CollectionSize, Entity, EntityCollection } from "../../models";
import {
Button,
Dialog,
DialogActions,
Divider,
Typography,
useMediaQuery,
useTheme
} from "@mui/material";
import createStyles from "@mui/styles/createStyles";
import makeStyles from "@mui/styles/makeStyles";
import React, { useEffect, useState } from "react";
import CollectionTable from "../../collection/components/CollectionTable";
import CollectionRowActions
from "../../collection/internal/CollectionRowActions";
import { useColumnIds } from "../../collection/internal/common";
import { useDataSource } from "../../hooks/data/useDataSource";
export const useStyles = makeStyles(theme => createStyles({
dialogBody: {
flexGrow: 1,
overflow: "auto",
minWidth: "85vw"
},
paper: {
height: "100%"
}
}));
/**
* @category Core components
*/
export interface ReferenceDialogProps {
/**
* Is the dialog currently open
*/
open: boolean;
/**
* Allow multiple selection of values
*/
multiselect: boolean;
/**
* Entity collection config
*/
collection: EntityCollection;
/**
* Absolute path of the collection
*/
path: string;
/**
* If you are opening the dialog for the first time, you can select some
* entity ids to be displayed first.
*/
selectedEntityIds?: string[];
/**
* If `multiselect` is set to `false`, you will get the select entity
* in this callback.
* @param entity
* @callback
*/
onSingleEntitySelected?(entity: Entity<any> | null): void;
/**
* If `multiselect` is set to `true`, you will get the selected entities
* in this callback.
* @param entities
* @callback
*/
onMultipleEntitiesSelected?(entities: Entity<any>[]): void;
/**
* Is the dialog currently open
* @callback
*/
onClose(): void;
}
/**
* This component renders an overlay dialog that allows to select entities
* in a given collection
* @category Core components
*/
export default function ReferenceDialog(
{
onSingleEntitySelected,
onMultipleEntitiesSelected,
onClose,
open,
multiselect,
collection,
path,
selectedEntityIds
}: ReferenceDialogProps) {
const classes = useStyles();
const dataSource = useDataSource();
const schema = collection.schema;
const textSearchEnabled = collection.textSearchEnabled;
const initialFilter = collection.initialFilter;
const displayedProperties = useColumnIds(collection, false);
const paginationEnabled = collection.pagination === undefined || Boolean(collection.pagination);
const pageSize = typeof collection.pagination === "number" ? collection.pagination : undefined;
const theme = useTheme();
const largeLayout = useMediaQuery(theme.breakpoints.up("md"));
const [selectedEntities, setSelectedEntities] = useState<Entity<any>[] | undefined>();
useEffect(() => {
if (selectedEntityIds) {
Promise.all(
selectedEntityIds.map((entityId) => dataSource.fetchEntity({
path,
entityId,
schema
})))
.then((entities) => {
setSelectedEntities(entities);
});
} else {
setSelectedEntities([]);
}
}, [selectedEntityIds]);
const onEntityClick = (entity: Entity<any>) => {
if (!multiselect && onSingleEntitySelected) {
onSingleEntitySelected(entity);
} else {
toggleEntitySelection(entity);
}
};
const onClear = () => {
if (!multiselect && onSingleEntitySelected) {
onSingleEntitySelected(null);
} else if (onMultipleEntitiesSelected) {
onMultipleEntitiesSelected([]);
}
};
const toggleEntitySelection = (entity: Entity<any>) => {
let newValue;
if (selectedEntities) {
if (selectedEntities.map((e) => e.id).indexOf(entity.id) > -1) {
newValue = selectedEntities.filter((item: Entity<any>) => item.id !== entity.id);
} else {
newValue = [...selectedEntities, entity];
}
setSelectedEntities(newValue);
if (onMultipleEntitiesSelected)
onMultipleEntitiesSelected(newValue);
}
};
const tableRowActionsBuilder = ({
entity,
size
}: { entity: Entity<any>, size: CollectionSize }) => {
const isSelected = selectedEntityIds && selectedEntityIds.indexOf(entity.id) > -1;
return <CollectionRowActions
entity={entity}
size={size}
isSelected={isSelected}
selectionEnabled={multiselect}
toggleEntitySelection={toggleEntitySelection}
/>;
};
const toolbarActionsBuilder = () => (
<Button onClick={onClear}
color="primary">
Clear
</Button>
);
const title = (
<Typography variant={"h6"}>
{`Select ${schema.name}`}
</Typography>);
return (
<Dialog
onClose={onClose}
classes={{
paper: classes.paper
}}
maxWidth={"xl"}
scroll={"paper"}
open={open}>
<div className={classes.dialogBody}>
{selectedEntities &&
<CollectionTable path={path}
inlineEditing={false}
schema={schema}
toolbarActionsBuilder={toolbarActionsBuilder}
onEntityClick={onEntityClick}
tableRowActionsBuilder={tableRowActionsBuilder}
paginationEnabled={paginationEnabled}
defaultSize={collection.defaultSize}
additionalColumns={collection.additionalColumns}
title={title}
pageSize={pageSize}
displayedProperties={displayedProperties}
textSearchEnabled={textSearchEnabled}
initialFilter={initialFilter}
initialSort={collection.initialSort}
entitiesDisplayedFirst={selectedEntities}
frozenIdColumn={largeLayout}
/>}
</div>
<Divider/>
<DialogActions>
<Button onClick={(event) => {
event.stopPropagation();
onClose();
}}
color="primary"
variant="contained">
Close
</Button>
</DialogActions>
</Dialog>
);
}