-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathListBuilder.jsx
509 lines (485 loc) · 17.9 KB
/
ListBuilder.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import React, { useCallback, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { ArrowRight, Reset, Subtract } from '@carbon/react/icons';
import warning from 'warning';
import { isNil } from 'lodash-es';
import HierarchyList from '../List/HierarchyList';
import { settings } from '../../constants/Settings';
import Button from '../Button/Button';
import deprecate from '../../internal/deprecate';
import List from '../List/List';
import { ButtonIconPropType } from '../../constants/SharedPropTypes';
import { EditingStyle } from '../../utils/DragAndDropUtils';
const { iotPrefix } = settings;
export const ListBuilderItemPropTypes = {
/** the id of this item */
id: PropTypes.string,
content: PropTypes.shape({
/** the value of this item */
value: PropTypes.string,
/**
* a function that returns an array of elements to trigger actions if the list builder has complex structure or logic
* otherwise, this will be populated by the ListBuilder as a simple add or remove button.
*/
rowActions: PropTypes.func,
}),
/** is this item disabled */
disabled: PropTypes.bool,
/** allows for groups or categories, see SelectUsersModal for a complex example */
isCategory: PropTypes.bool,
/** allows for groups or categories, see SelectUsersModal for a complex example */
children: PropTypes.arrayOf(PropTypes.object),
};
const propTypes = {
/** an array of the ids of the items in the "all list" that should be expanded */
allDefaultExpandedIds: PropTypes.arrayOf(PropTypes.string),
/** callback used to limit which items that should get drop targets rendered.
* receives the id of the item that is being dragged and returns a list of ids. */
getAllowedDropIds: PropTypes.func,
// TODO: remove deprecated testID in v3.
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
testId: PropTypes.string,
/** will increase the height of the list items to make to room for a secondary value */
isLargeRow: PropTypes.bool,
/** the list of all items available to select in the ListBuilder */
items: PropTypes.arrayOf(PropTypes.shape(ListBuilderItemPropTypes)),
/** current search value of the items search */
itemsSearchValue: PropTypes.string,
/** if the items contain children or groups, a proper count can be passed here. */
itemCount: PropTypes.number,
/** callback function returned before an item in the selected list is repositioned.
* Returning false cancels the move */
itemWillMove: PropTypes.func,
/** the ids of locked items that cannot be reordered in the selected list */
lockedIds: PropTypes.arrayOf(PropTypes.string),
/** RowIds for rows currently loading more rows */
loadingMoreIds: PropTypes.arrayOf(PropTypes.string),
/** an array of all selected items */
selectedItems: PropTypes.arrayOf(PropTypes.shape(ListBuilderItemPropTypes)),
/** an array of the ids of the selected items that should be expanded */
selectedDefaultExpandedIds: PropTypes.arrayOf(PropTypes.string),
/** (event, id) => void */
onAdd: PropTypes.func,
/** receives an updated array of the selected items after reordering */
onSelectedListReordered: PropTypes.func,
/** (event, id) => void */
onRemove: PropTypes.func,
/** Called when the items search value changes. Receives the latest value as a string.
* Required when useCheckboxes is true */
onItemsSearchChange: PropTypes.func,
/** Called when the reset button is clicked */
onReset: PropTypes.func,
/** if true checkboxes will be used for selection so selected items can stay in the left list */
useCheckboxes: PropTypes.bool,
/** call back function for when load more row is clicked (rowId) => {} */
handleLoadMore: PropTypes.func,
/** true if the items list should have search */
hasItemsSearch: PropTypes.bool,
/** true if the selected items list should have search */
hasSelectedItemsSearch: PropTypes.bool,
/** true if the reset button should be shown */
hasReset: PropTypes.bool,
i18n: PropTypes.shape({
/** (count) => `Items (${count} available)` */
allListTitle: PropTypes.func,
/** (count) => `${count} items selected` */
selectedListTitle: PropTypes.func,
/** remove icons description on selected items */
removeIconDescription: PropTypes.string,
/** add aria label on unselected items */
addLabel: PropTypes.string,
/** placeholder text for the search box for all items */
allListSearchPlaceholderText: PropTypes.string,
/** placeholder text for the search box for selected items */
selectedListSearchPlaceholderText: PropTypes.string,
/** expand icon description when using nested groups */
expandIconDescription: PropTypes.string,
/** collapse icon description when using nested groups */
collapseIconDescription: PropTypes.string,
/** reset button label */
resetLabel: PropTypes.string,
/** label for the "Load more" button */
loadMoreButtonLabel: PropTypes.string,
/** message shown if the all items list is empty */
allListEmptyText: PropTypes.string,
/** message shown if selected list is empty */
selectedListEmptyText: PropTypes.string,
/** icon description label for the clear search icon */
clearSearchIconDescription: PropTypes.string,
expand: deprecate(
PropTypes.string,
`The 'expand' prop has been deprecated. Please use 'expandIconDescription' instead.`
),
close: deprecate(
PropTypes.string,
`The 'close' prop has been deprecated. Please use 'collapseIconDescription' instead.`
),
}),
/** the icon used as button to remove items from the selected list */
removeIcon: ButtonIconPropType,
/** the editing style of the hirerchy list showing the selected items */
selectedEditingStyle: PropTypes.oneOf([EditingStyle.Single, EditingStyle.SingleNesting]),
/** if true the list for available columns will show a loader only */
showLoaderInAvailableList: PropTypes.bool,
/** if true the list for selected columns will show a loader only */
showLoaderInSelectedList: PropTypes.bool,
};
const defaultProps = {
allDefaultExpandedIds: [],
getAllowedDropIds: null,
handleLoadMore: undefined,
hasItemsSearch: true,
hasSelectedItemsSearch: true,
hasReset: false,
testId: 'list-builder',
isLargeRow: false,
items: [],
itemsSearchValue: null,
itemCount: null,
itemWillMove: () => true,
loadingMoreIds: [],
lockedIds: [],
selectedItems: [],
selectedDefaultExpandedIds: [],
onAdd: null,
onSelectedListReordered: () => {},
onRemove: null,
onReset: () => {},
onItemsSearchChange: null,
i18n: {
allListTitle: (count) => {
return `Items (${count} available)`;
},
selectedListTitle: (count) => {
return `${count} Selected`;
},
removeIconDescription: 'Remove item from list',
addLabel: 'Add item to list',
allListSearchPlaceholderText: 'Enter a value to search all items',
selectedListSearchPlaceholderText: 'Enter a value to search selected items',
expandIconDescription: 'Expand',
collapseIconDescription: 'Collapse',
resetLabel: 'Reset',
loadMoreButtonLabel: 'Load more...',
},
useCheckboxes: false,
removeIcon: Subtract,
selectedEditingStyle: undefined,
showLoaderInAvailableList: false,
showLoaderInSelectedList: false,
};
const getSelectedItemIds = (nestedSelectedItems) =>
nestedSelectedItems.flatMap((selectedItem) =>
selectedItem.isCategory ? getSelectedItemIds(selectedItem.children) : selectedItem.id
);
const getSelectedParentIds = (nestedSelectedItems, selectedChildIds, allListItems) => {
const parentsWithChildSelection = nestedSelectedItems.filter(
(item) => item.isCategory && item.children?.length
);
return parentsWithChildSelection.flatMap((currentParent) => {
const originalParent = allListItems.find(({ id }) => currentParent.id === id);
return originalParent.children.every(({ id }) => selectedChildIds.includes(id))
? [currentParent.id]
: [];
});
};
const ListBuilder = ({
allDefaultExpandedIds,
getAllowedDropIds,
handleLoadMore,
hasItemsSearch,
hasSelectedItemsSearch,
hasReset,
// TODO: remove deprecated testID in v3.
testID,
testId,
isLargeRow,
items,
itemsSearchValue,
itemCount,
itemWillMove,
loadingMoreIds,
lockedIds,
selectedItems,
selectedDefaultExpandedIds,
i18n,
onAdd,
onSelectedListReordered,
onRemove,
onItemsSearchChange,
onReset,
useCheckboxes,
removeIcon,
selectedEditingStyle,
showLoaderInAvailableList,
showLoaderInSelectedList,
}) => {
// When the checkbox design is used there are currently a few restrictions
// and additional requirements
if (__DEV__ && useCheckboxes) {
if (!onItemsSearchChange) {
warning(
false,
'The `onItemsSearchChange` prop must be provided when `useCheckboxes` is true.'
);
}
}
const mergedI18n = {
...defaultProps.i18n,
...i18n,
};
const {
allListTitle,
selectedListTitle,
resetLabel,
allListEmptyText,
selectedListEmptyText,
clearSearchIconDescription,
} = mergedI18n;
const handleAdd = useCallback(
(id) => (event) => {
event.persist();
onAdd(event, id);
},
[onAdd]
);
const handleRemove = useCallback(
(id) => (event) => {
event.persist();
onRemove(event, id);
},
[onRemove]
);
const allListItems = useMemo(
() =>
items?.map((item) => {
const rowActions = !useCheckboxes
? item.content.rowActions !== undefined
? item.content.rowActions
: // The "Select" button from the old design. New design uses checkboxes.
() => [
<Button
key={`${item.id}-list-item-button`}
// TODO: remove deprecated testID in v3.
testId={`${testID || testId}-add-button-${item.id}`}
role="button"
aria-label={mergedI18n.addLabel}
renderIcon={ArrowRight}
hasIconOnly
kind="ghost"
size="sm"
onClick={handleAdd(item.id)}
iconDescription={mergedI18n.addLabel}
/>,
]
: undefined;
return {
...item,
isSelectable: useCheckboxes
? item.hasOwnProperty('isSelectable')
? item.isSelectable
: true
: undefined,
content: {
...item.content,
rowActions,
},
};
}) ?? [],
[handleAdd, items, mergedI18n.addLabel, testID, testId, useCheckboxes]
);
const filteredListItems = useMemo(() => {
const lowerCaseSearchValue =
typeof itemsSearchValue === 'string' && itemsSearchValue.toLowerCase();
const isMatch = ({ id, content: { value } }) =>
id.toLowerCase().includes(lowerCaseSearchValue) ||
value.toLowerCase().includes(lowerCaseSearchValue);
return useCheckboxes && !isNil(itemsSearchValue) && itemsSearchValue !== ''
? allListItems.flatMap((item) => {
const directMatch = isMatch(item);
const matchingChildren = item.children?.length && item.children.filter(isMatch);
const matchingItem = directMatch || matchingChildren?.length ? { ...item } : [];
return matchingChildren?.length
? { ...matchingItem, children: matchingChildren }
: matchingItem;
})
: allListItems;
}, [allListItems, itemsSearchValue, useCheckboxes]);
const selectedListItems = useMemo(() => {
const appendRemoveAction = (item) => {
const rowActions =
item.content.rowActions !== undefined
? item.content.rowActions
: () => [
<Button
key={`${item.id}-list-item-button`}
// TODO: remove deprecated testID in v3.
testId={`${testID || testId}-remove-button-${item.id}`}
renderIcon={removeIcon}
hasIconOnly
kind="ghost"
size="sm"
onClick={handleRemove(item.id)}
iconDescription={mergedI18n.removeIconDescription}
/>,
];
const modifiedChildren = item.children
? item.children.map((child) => appendRemoveAction(child))
: undefined;
return {
...item,
content: {
...item.content,
rowActions,
},
...(modifiedChildren ? { children: modifiedChildren } : {}),
};
};
return selectedItems?.map((item) => appendRemoveAction(item)) ?? [];
}, [handleRemove, mergedI18n.removeIconDescription, selectedItems, testID, testId, removeIcon]);
const flattenedSelectedIds = useMemo(() => {
if (!useCheckboxes) return [];
const selectedChildIds = getSelectedItemIds(selectedItems, allListItems);
const selectedParentIds = getSelectedParentIds(selectedItems, selectedChildIds, allListItems);
return [...selectedChildIds, ...selectedParentIds];
}, [selectedItems, allListItems, useCheckboxes]);
const indeterminateIds = useMemo(() => {
if (!useCheckboxes) return [];
return allListItems
.filter(
(parent) =>
parent.children &&
parent.children.some((sibling) => flattenedSelectedIds.includes(sibling.id)) &&
parent.children.some((sibling) => !flattenedSelectedIds.includes(sibling.id))
)
.map((parent) => parent.id);
}, [allListItems, flattenedSelectedIds, useCheckboxes]);
const [checkboxListExpandedIds, setCheckboxListExpandedIds] = useState(allDefaultExpandedIds);
const renderCheckboxList = () => {
return (
<List
toggleExpansion={(id) => {
setCheckboxListExpandedIds((prev) => {
return prev.includes(id) ? prev.filter((expId) => id !== expId) : [...prev, id];
});
}}
expandedIds={checkboxListExpandedIds}
isLargeRow={isLargeRow}
isLoading={showLoaderInAvailableList}
indeterminateIds={indeterminateIds}
emptyState={allListEmptyText}
handleLoadMore={handleLoadMore}
loadingMoreIds={loadingMoreIds}
title={allListTitle(itemCount ?? allListItems.length)}
items={filteredListItems}
isVirtualList
selectedIds={flattenedSelectedIds}
isCheckboxMultiSelect
handleSelect={(id) => {
// Stay backwards compatible with the non-checkbox design where an event was
// passed before the selected id
return flattenedSelectedIds.find((selectedId) => selectedId === id)
? onRemove(null, id)
: onAdd(null, id);
}}
search={
hasItemsSearch
? {
onChange: (evt) => {
onItemsSearchChange(evt.target.value, evt);
},
}
: undefined
}
i18n={{
clearSearchIconDescription,
searchPlaceHolderText: mergedI18n.allListSearchPlaceholderText,
loadMore: mergedI18n.loadMoreButtonLabel,
}}
/>
);
};
return (
<div data-testid={testID || testId} className={`${iotPrefix}--list-builder__container`}>
<div
className={`${iotPrefix}--list-builder__all`}
// TODO: remove deprecated testID in v3.
data-testid={`${testID || testId}__all`}
>
{useCheckboxes ? (
renderCheckboxList()
) : (
<HierarchyList
defaultExpandedIds={allDefaultExpandedIds}
isLargeRow={isLargeRow}
isLoading={showLoaderInAvailableList}
emptyState={allListEmptyText}
title={allListTitle(itemCount ?? allListItems.length)}
items={allListItems}
hasSearch={hasItemsSearch}
hasPagination={false}
searchId={`${iotPrefix}--list-builder__all--search`}
i18n={{
clearSearchIconDescription,
searchPlaceHolderText: mergedI18n.allListSearchPlaceholderText,
expand: mergedI18n.expandIconDescription || mergedI18n.expand,
close: mergedI18n.collapseIconDescription || mergedI18n.close,
}}
/>
)}
</div>
<div
className={`${iotPrefix}--list-builder__selected`}
// TODO: remove deprecated testID in v3.
data-testid={`${testID || testId}__selected`}
>
<HierarchyList
isLargeRow={isLargeRow}
isLoading={showLoaderInSelectedList}
emptyState={selectedListEmptyText}
buttons={
hasReset
? [
<Button
className={`${iotPrefix}--list-builder__reset-button`}
testId={`${testId}__selected__reset-button`}
renderIcon={Reset}
kind="ghost"
size="sm"
iconDescription={resetLabel}
key="hierarchy-list-button-add"
onClick={onReset}
>
{resetLabel}
</Button>,
]
: []
}
editingStyle={selectedEditingStyle}
defaultExpandedIds={selectedDefaultExpandedIds}
getAllowedDropIds={getAllowedDropIds}
title={selectedListTitle(selectedListItems.length)}
items={selectedListItems}
itemWillMove={itemWillMove}
onListUpdated={onSelectedListReordered}
lockedIds={lockedIds}
hasSearch={hasSelectedItemsSearch}
hasPagination={false}
searchId={`${iotPrefix}--list-builder__selected--search`}
i18n={{
searchPlaceHolderText: mergedI18n.selectedListSearchPlaceholderText,
expand: mergedI18n.expandIconDescription || mergedI18n.expand,
close: mergedI18n.collapseIconDescription || mergedI18n.close,
}}
/>
</div>
</div>
);
};
ListBuilder.propTypes = propTypes;
ListBuilder.defaultProps = defaultProps;
export default ListBuilder;