-
Notifications
You must be signed in to change notification settings - Fork 3
/
LabelSelector.jsx
431 lines (408 loc) · 11.1 KB
/
LabelSelector.jsx
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import Autocomplete, { createFilterOptions } from "@mui/material/Autocomplete";
import {
Box,
Button,
IconButton,
Paper,
Stack,
TextField,
Tooltip
} from "@mui/material";
import { Delete, Edit } from "@mui/icons-material";
import React, { useState } from "react";
import Checkbox from "../Checkbox";
import DeleteLabelDialog from "./DeleteLabelDialog/DeleteLabelDialog";
import EditLabelDialog from "../EditLabelDialog/EditLabelDialog";
import LabelChip from "./LabelChip/LabelChip";
import NoWrapTypography from "../NoWrapTypography/NoWrapTypography";
import PropTypes from "prop-types";
// custom styling
const styles = {
color: {
borderRadius: 3,
flexShrink: 0,
height: 14,
marginRight: 8,
marginTop: 2,
width: 14
},
deleteIcon: {
"&:hover": { color: theme => theme.palette.error.dark, opacity: 1 },
height: 18,
opacity: 0.6,
width: 18
},
editIcon: {
"&:hover": { color: theme => theme.palette.primary.dark, opacity: 1 },
height: 18,
opacity: 0.6,
width: 18
}
};
// custom paper component for dropdown list that adds add new label button if user is an admin
const CustomPaper = ({
addEnabled,
setIsLabelDialogOpen,
setLabelDialogTitle,
...props
}) => {
// click handler for add new label button
const handleClick = () => {
// set dialog title
setLabelDialogTitle("Add New Label");
// open new label dialog
setIsLabelDialogOpen(true);
};
// prevent default on mouse down so that add new button can be clicked
const handleMouseDown = event => {
event.preventDefault();
};
// return paper componment with add new label button if user is admin
return (
<Paper {...props} onMouseDown={handleMouseDown}>
{props.children}
{addEnabled && (
<Box
sx={{
marginBottom: 1,
marginLeft: 2
}}
>
<Button
size={props.size}
color="primary"
variant="outlined"
onClick={handleClick}
>
+ Add New Label
</Button>
</Box>
)}
</Paper>
);
};
// label selector component
export default function LabelSelector({
addEnabled = false,
autocompleteLabel = "",
deleteEnabled = false,
error = false,
editEnabled = false,
helperText,
limitTags = -1,
nameMaxLength = 50,
multiple = true,
name,
onBlur = () => {},
onChange = () => {},
onDelete = () => {},
onEdit = () => {},
onNew = () => {},
options = [],
size = "small",
value = []
}) {
// default label object
const defaultLabel = {
_id: "",
color: "#005FA8",
description: "",
name: ""
};
// new label dialog open state
const [isLabelDialogOpen, setIsLabelDialogOpen] = useState(false);
// label dialog title state
const [labelDialogTitle, setLabelDialogTitle] = useState("Add New Label");
// delete dialog open state
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
// selected label state
const [selectedLabel, setSelectedLabel] = useState(defaultLabel);
// create custom filter options for autocomplete
const filter = createFilterOptions();
// remove label from selected labels when
// user clicks on the x icon in the label chip
const handleLabelRemove = id => {
// remove label from value
value = value.filter(label => label._id !== id);
// pass on the event with the new value
onChange(value);
};
// open dialog when user clicks on edit icon in list item
const onEditClick = (event, label) => {
// prevent click propagation to parent
event.stopPropagation();
// set label dialog title
setLabelDialogTitle(`Edit ${label.name}`);
// set selected label
setSelectedLabel(label);
// open new label dialog
setIsLabelDialogOpen(true);
};
// open confirm dialog when user clicks on delete icon in list item
const onDeleteClick = (event, label) => {
// prevent click propagation to parent
event.stopPropagation();
// set selected label
setSelectedLabel(label);
// open delete dialog
setIsDeleteDialogOpen(true);
};
return (
<>
<Autocomplete
size={size}
disableCloseOnSelect
limitTags={limitTags}
multiple={multiple}
onBlur={onBlur}
onChange={(_e, selectedValues) => onChange(selectedValues)}
options={options}
renderInput={params => (
<TextField
{...params}
error={error}
helperText={helperText}
name={name}
label={autocompleteLabel}
variant="outlined"
/>
)}
filterOptions={(options, params) => {
const filtered = filter(options, params);
return filtered;
}}
renderOption={(props, option, { selected }) => (
<Box key={option._id} component="li" {...props}>
<Stack
direction="row"
sx={{
alignItems: "center",
flexGrow: 1,
overflow: "hidden"
}}
>
<Checkbox
checked={selected}
size={size}
style={{
"&.Mui-checked": { color: option.color },
color: option.color
}}
/>
<Stack
direction="column"
sx={{
flexGrow: 1,
overflow: "hidden"
}}
>
<NoWrapTypography>{option.name}</NoWrapTypography>
<NoWrapTypography variant="caption">
{option.description}
</NoWrapTypography>
</Stack>
<>
{editEnabled && (
<Tooltip title="Edit">
<IconButton
data-testid={`edit-label-${option._id}`}
size={size}
onClick={event => onEditClick(event, option)}
>
<Edit sx={styles.editIcon} />
</IconButton>
</Tooltip>
)}
{deleteEnabled && (
<Tooltip title="Delete">
<IconButton
data-testid={`delete-label-${option._id}`}
size={size}
onClick={event => onDeleteClick(event, option)}
>
<Delete sx={styles.deleteIcon} />
</IconButton>
</Tooltip>
)}
</>
</Stack>
</Box>
)}
renderTags={options =>
options.map((option, index) => (
<LabelChip
key={index}
label={option.name}
color={option.color}
size={size}
style={{ marginLeft: 2 }}
onDelete={_e => {
handleLabelRemove(option._id);
}}
clickable={false}
/>
))
}
noOptionsText="No labels found"
getOptionLabel={option => option.name}
isOptionEqualToValue={(option, value) => {
return option._id === value._id;
}}
value={value || null}
slots={{
paper: props => {
return (
<CustomPaper
addEnabled={addEnabled}
setIsLabelDialogOpen={setIsLabelDialogOpen}
setLabelDialogTitle={setLabelDialogTitle}
{...props}
/>
);
}
}}
/>
<EditLabelDialog
isOpen={isLabelDialogOpen !== false}
onClose={() => {
setIsLabelDialogOpen(false);
setSelectedLabel(defaultLabel);
}}
options={options}
title={labelDialogTitle}
label={selectedLabel}
nameMaxLength={nameMaxLength}
onNew={onNew}
onEdit={onEdit}
/>
<DeleteLabelDialog
isOpen={isDeleteDialogOpen !== false}
onClose={() => {
setIsDeleteDialogOpen(false);
setSelectedLabel(defaultLabel);
}}
label={selectedLabel}
onDelete={onDelete}
/>
</>
);
}
// Label Selector Proptypes
LabelSelector.propTypes = {
/**
* If true, the "Add New Label" button will be displayed. Clicking this button will open a dialog to add a new label.
*/
addEnabled: PropTypes.bool,
/**
* Label for the autocomplete input field.
*/
autocompleteLabel: PropTypes.string,
/**
* If true, a delete icon will be displayed next to each label. Clicking this icon will open a dialog to confirm the deletion of the label.
*/
deleteEnabled: PropTypes.bool,
/**
* If true, an edit icon will be displayed next to each label. Clicking this icon will open a dialog to edit the label.
*/
editEnabled: PropTypes.bool,
/**
* If true, the label selector will display an error state.
*/
error: PropTypes.bool,
/**
* The helper text content.
*/
helperText: PropTypes.string,
/**
* The maximum number of tags that will be visible when not focused. Set to -1 to disable the limit.
*/
limitTags: PropTypes.number,
/**
* If true, the value must be an array and the menu will support multiple selections.
*/
multiple: PropTypes.bool,
/**
* The name of the input.
*/
name: PropTypes.string,
/**
* The maximum length of a label name.
*/
nameMaxLength: PropTypes.number,
/**
* Callback fired when the input is blurred.
*
* **Signature**
* ```
* function(event: object) => void
* ```
* _event_: The event source of the callback.
*/
onBlur: PropTypes.func,
/**
* Callback fired when the value is changed.
*
* **Signature**
* ```
* function(selectedLabels: array) => void
* ```
* _selectedLabels_: The labels that are currently selected.
*/
onChange: PropTypes.func,
/**
* Callback fired when a label is deleted.
*
* **Signature**
* ```
* function(deletedLabel: object) => void
* ```
* _deletedLabel_: The label that is deleted.
*/
onDelete: PropTypes.func,
/**
* Callback fired when a label is edited.
*
* **Signature**
* ```
* function(editedLabel: object) => void
* ```
* _editedLabel_: The label that is edited.
*/
onEdit: PropTypes.func,
/**
* Callback fired when a new label is added.
*
* **Signature**
* ```
* function(newLabel: object) => void
* ```
* _newLabel_: The label that is added.
*/
onNew: PropTypes.func,
/**
* Array of label objects to display in the listbox.
*/
options: PropTypes.arrayOf(
PropTypes.shape({
_id: PropTypes.string.isRequired,
color: PropTypes.string,
description: PropTypes.string,
name: PropTypes.string.isRequired
})
).isRequired,
/**
* The size of the component.
*/
size: PropTypes.oneOf(["small", "medium"]),
/**
* Array of label objects that are currently selected.
*/
value: PropTypes.arrayOf(
PropTypes.shape({
_id: PropTypes.string.isRequired,
color: PropTypes.string,
description: PropTypes.string,
name: PropTypes.string.isRequired
})
)
};