@patternfly/react-core 4.179.1 → 4.180.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/esm/components/DualListSelector/DualListSelectorTree.d.ts +1 -1
  3. package/dist/esm/components/DualListSelector/DualListSelectorTree.d.ts.map +1 -1
  4. package/dist/esm/components/DualListSelector/DualListSelectorTree.js +2 -1
  5. package/dist/esm/components/DualListSelector/DualListSelectorTree.js.map +1 -1
  6. package/dist/esm/components/DualListSelector/DualListSelectorTreeItem.d.ts +3 -1
  7. package/dist/esm/components/DualListSelector/DualListSelectorTreeItem.d.ts.map +1 -1
  8. package/dist/esm/components/DualListSelector/DualListSelectorTreeItem.js +24 -2
  9. package/dist/esm/components/DualListSelector/DualListSelectorTreeItem.js.map +1 -1
  10. package/dist/js/components/DualListSelector/DualListSelectorTree.d.ts +1 -1
  11. package/dist/js/components/DualListSelector/DualListSelectorTree.d.ts.map +1 -1
  12. package/dist/js/components/DualListSelector/DualListSelectorTree.js +2 -1
  13. package/dist/js/components/DualListSelector/DualListSelectorTree.js.map +1 -1
  14. package/dist/js/components/DualListSelector/DualListSelectorTreeItem.d.ts +3 -1
  15. package/dist/js/components/DualListSelector/DualListSelectorTreeItem.d.ts.map +1 -1
  16. package/dist/js/components/DualListSelector/DualListSelectorTreeItem.js +24 -3
  17. package/dist/js/components/DualListSelector/DualListSelectorTreeItem.js.map +1 -1
  18. package/dist/umd/react-core.js +26 -3
  19. package/dist/umd/react-core.min.css +3587 -3587
  20. package/dist/umd/react-core.min.js +1 -1
  21. package/package.json +5 -5
  22. package/src/components/DualListSelector/DualListSelectorTree.tsx +4 -2
  23. package/src/components/DualListSelector/DualListSelectorTreeItem.tsx +32 -1
  24. package/src/components/DualListSelector/__tests__/__snapshots__/DualListSelector.test.tsx.snap +3 -0
  25. package/src/components/DualListSelector/examples/ComposableTree.tsx +345 -0
  26. package/src/components/DualListSelector/examples/DualListSelector.md +5 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-core",
3
- "version": "4.179.1",
3
+ "version": "4.180.0",
4
4
  "description": "This library provides a set of common React components for use with the PatternFly reference implementation.",
5
5
  "main": "dist/js/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -34,9 +34,9 @@
34
34
  "generate": "node scripts/copyStyles.js"
35
35
  },
36
36
  "dependencies": {
37
- "@patternfly/react-icons": "^4.30.1",
38
- "@patternfly/react-styles": "^4.29.1",
39
- "@patternfly/react-tokens": "^4.31.1",
37
+ "@patternfly/react-icons": "^4.31.0",
38
+ "@patternfly/react-styles": "^4.30.0",
39
+ "@patternfly/react-tokens": "^4.32.0",
40
40
  "focus-trap": "6.2.2",
41
41
  "react-dropzone": "9.0.0",
42
42
  "tippy.js": "5.1.2",
@@ -60,5 +60,5 @@
60
60
  "react": "^16.8.0 || ^17.0.0",
61
61
  "react-dom": "^16.8.0 || ^17.0.0"
62
62
  },
63
- "gitHead": "6f05128d4ab96a25ccae635f2c78b717e48f9a47"
63
+ "gitHead": "d356fa38b88466281002c38009d4cdb87aedbe5e"
64
64
  }
@@ -37,7 +37,7 @@ export interface DualListSelectorTreeItemData {
37
37
 
38
38
  export interface DualListSelectorTreeProps {
39
39
  /** Data of the tree view */
40
- data: DualListSelectorTreeItemData[];
40
+ data: DualListSelectorTreeItemData[] | (() => DualListSelectorTreeItemData[]);
41
41
  /** ID of the tree view */
42
42
  id?: string;
43
43
  /** @hide Flag indicating if the list is nested */
@@ -64,7 +64,8 @@ export const DualListSelectorTree: React.FunctionComponent<DualListSelectorTreeP
64
64
  isDisabled = false,
65
65
  ...props
66
66
  }: DualListSelectorTreeProps) => {
67
- const tree = data.map(item => (
67
+ const dataToRender = typeof data === 'function' ? data() : data;
68
+ const tree = dataToRender.map(item => (
68
69
  <DualListSelectorTreeItem
69
70
  key={item.id}
70
71
  text={item.text}
@@ -77,6 +78,7 @@ export const DualListSelectorTree: React.FunctionComponent<DualListSelectorTreeP
77
78
  badgeProps={item.badgeProps}
78
79
  itemData={item}
79
80
  isDisabled={isDisabled}
81
+ useMemo={true}
80
82
  {...(item.children && {
81
83
  children: (
82
84
  <DualListSelectorTree
@@ -36,9 +36,11 @@ export interface DualListSelectorTreeItemProps extends React.HTMLProps<HTMLLIEle
36
36
  itemData?: DualListSelectorTreeItemData;
37
37
  /** Flag indicating whether the component is disabled. */
38
38
  isDisabled?: boolean;
39
+ /** Flag indicating the DualListSelector tree should utilize memoization to help render large data sets. */
40
+ useMemo?: boolean;
39
41
  }
40
42
 
41
- export const DualListSelectorTreeItem: React.FunctionComponent<DualListSelectorTreeItemProps> = ({
43
+ const DualListSelectorTreeItemBase: React.FunctionComponent<DualListSelectorTreeItemProps> = ({
42
44
  onOptionCheck,
43
45
  children,
44
46
  className,
@@ -51,12 +53,18 @@ export const DualListSelectorTreeItem: React.FunctionComponent<DualListSelectorT
51
53
  badgeProps,
52
54
  itemData,
53
55
  isDisabled = false,
56
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
57
+ useMemo,
54
58
  ...props
55
59
  }: DualListSelectorTreeItemProps) => {
56
60
  const ref = React.useRef(null);
57
61
  const [isExpanded, setIsExpanded] = React.useState(defaultExpanded || false);
58
62
  const { setFocusedOption } = React.useContext(DualListSelectorListContext);
59
63
 
64
+ React.useEffect(() => {
65
+ setIsExpanded(defaultExpanded);
66
+ }, [defaultExpanded]);
67
+
60
68
  return (
61
69
  <li
62
70
  className={css(
@@ -150,4 +158,27 @@ export const DualListSelectorTreeItem: React.FunctionComponent<DualListSelectorT
150
158
  </li>
151
159
  );
152
160
  };
161
+
162
+ export const DualListSelectorTreeItem = React.memo(DualListSelectorTreeItemBase, (prevProps, nextProps) => {
163
+ if (!nextProps.useMemo) {
164
+ return false;
165
+ }
166
+
167
+ if (
168
+ prevProps.className !== nextProps.className ||
169
+ prevProps.text !== nextProps.text ||
170
+ prevProps.id !== nextProps.id ||
171
+ prevProps.defaultExpanded !== nextProps.defaultExpanded ||
172
+ prevProps.checkProps !== nextProps.checkProps ||
173
+ prevProps.hasBadge !== nextProps.hasBadge ||
174
+ prevProps.badgeProps !== nextProps.badgeProps ||
175
+ prevProps.isChecked !== nextProps.isChecked ||
176
+ prevProps.itemData !== nextProps.itemData
177
+ ) {
178
+ return false;
179
+ }
180
+
181
+ return true;
182
+ });
183
+
153
184
  DualListSelectorTreeItem.displayName = 'DualListSelectorTreeItem';
@@ -4487,6 +4487,7 @@ exports[`DualListSelector with tree 1`] = `
4487
4487
  key="O1"
4488
4488
  onOptionCheck={[Function]}
4489
4489
  text="Opt1"
4490
+ useMemo={true}
4490
4491
  >
4491
4492
  <li
4492
4493
  aria-expanded="true"
@@ -4598,6 +4599,7 @@ exports[`DualListSelector with tree 1`] = `
4598
4599
  key="O3"
4599
4600
  onOptionCheck={[Function]}
4600
4601
  text="Opt3"
4602
+ useMemo={true}
4601
4603
  >
4602
4604
  <li
4603
4605
  aria-selected={false}
@@ -4658,6 +4660,7 @@ exports[`DualListSelector with tree 1`] = `
4658
4660
  key="O2"
4659
4661
  onOptionCheck={[Function]}
4660
4662
  text="Opt2"
4663
+ useMemo={true}
4661
4664
  >
4662
4665
  <li
4663
4666
  aria-selected={false}
@@ -0,0 +1,345 @@
1
+ import React from 'react';
2
+ import {
3
+ DualListSelector,
4
+ DualListSelectorPane,
5
+ DualListSelectorList,
6
+ DualListSelectorControlsWrapper,
7
+ DualListSelectorControl,
8
+ DualListSelectorTree,
9
+ DualListSelectorTreeItemData,
10
+ SearchInput
11
+ } from '@patternfly/react-core';
12
+ import AngleDoubleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-left-icon';
13
+ import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
14
+ import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-right-icon';
15
+ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
16
+
17
+ interface FoodNode {
18
+ id: string;
19
+ text: string;
20
+ children?: FoodNode[];
21
+ }
22
+
23
+ interface ExampleProps {
24
+ data: FoodNode[];
25
+ }
26
+
27
+ export const ComposableDualListSelectorTree: React.FunctionComponent<ExampleProps> = ({ data }: ExampleProps) => {
28
+ const [checkedLeafIds, setCheckedLeafIds] = React.useState<string[]>([]);
29
+ const [chosenLeafIds, setChosenLeafIds] = React.useState<string[]>(['beans', 'beef', 'chicken', 'tofu']);
30
+ const [chosenFilter, setChosenFilter] = React.useState<string>('');
31
+ const [availableFilter, setAvailableFilter] = React.useState<string>('');
32
+ let hiddenChosen: string[] = [];
33
+ let hiddenAvailable: string[] = [];
34
+
35
+ // helper function to build memoized lists
36
+ const buildTextById = (node: FoodNode): { [key: string]: string } => {
37
+ let textById = {};
38
+ if (!node) {
39
+ return textById;
40
+ }
41
+ textById[node.id] = node.text;
42
+ if (node.children) {
43
+ node.children.forEach(child => {
44
+ textById = { ...textById, ...buildTextById(child) };
45
+ });
46
+ }
47
+ return textById;
48
+ };
49
+
50
+ // helper function to build memoized lists
51
+ const getDescendantLeafIds = (node: FoodNode): string[] => {
52
+ if (!node.children || !node.children.length) {
53
+ return [node.id];
54
+ } else {
55
+ let childrenIds = [];
56
+ node.children.forEach(child => {
57
+ childrenIds = [...childrenIds, ...getDescendantLeafIds(child)];
58
+ });
59
+ return childrenIds;
60
+ }
61
+ };
62
+
63
+ // helper function to build memoized lists
64
+ const getLeavesById = (node: FoodNode): { [key: string]: string[] } => {
65
+ let leavesById = {};
66
+ if (!node.children || !node.children.length) {
67
+ leavesById[node.id] = [node.id];
68
+ } else {
69
+ node.children.forEach(child => {
70
+ leavesById[node.id] = getDescendantLeafIds(node);
71
+ leavesById = { ...leavesById, ...getLeavesById(child) };
72
+ });
73
+ }
74
+ return leavesById;
75
+ };
76
+
77
+ // Builds a map of child leaf nodes by node id - memoized so that it only rebuilds the list if the data changes.
78
+ const { memoizedLeavesById, memoizedAllLeaves, memoizedNodeText } = React.useMemo(() => {
79
+ let leavesById = {};
80
+ let allLeaves = [];
81
+ let nodeTexts = {};
82
+ data.forEach(foodNode => {
83
+ nodeTexts = { ...nodeTexts, ...buildTextById(foodNode) };
84
+ leavesById = { ...leavesById, ...getLeavesById(foodNode) };
85
+ allLeaves = [...allLeaves, ...getDescendantLeafIds(foodNode)];
86
+ });
87
+ return {
88
+ memoizedLeavesById: leavesById,
89
+ memoizedAllLeaves: allLeaves,
90
+ memoizedNodeText: nodeTexts
91
+ };
92
+ }, [data]);
93
+
94
+ const moveChecked = (toChosen: boolean) => {
95
+ setChosenLeafIds(
96
+ prevChosenIds =>
97
+ toChosen
98
+ ? [...prevChosenIds, ...checkedLeafIds] // add checked ids to chosen list
99
+ : [...prevChosenIds.filter(x => !checkedLeafIds.includes(x))] // remove checked ids from chosen list
100
+ );
101
+
102
+ // uncheck checked ids that just moved
103
+ setCheckedLeafIds(prevChecked =>
104
+ toChosen
105
+ ? [...prevChecked.filter(x => chosenLeafIds.includes(x))]
106
+ : [...prevChecked.filter(x => !chosenLeafIds.includes(x))]
107
+ );
108
+ };
109
+
110
+ const moveAll = (toChosen: boolean) => {
111
+ if (toChosen) {
112
+ setChosenLeafIds(memoizedAllLeaves);
113
+ } else {
114
+ setChosenLeafIds([]);
115
+ }
116
+ };
117
+
118
+ const areAllDescendantsSelected = (node: FoodNode, isChosen: boolean) =>
119
+ memoizedLeavesById[node.id].every(
120
+ id => checkedLeafIds.includes(id) && (isChosen ? chosenLeafIds.includes(id) : !chosenLeafIds.includes(id))
121
+ );
122
+ const areSomeDescendantsSelected = (node: FoodNode, isChosen: boolean) =>
123
+ memoizedLeavesById[node.id].some(
124
+ id => checkedLeafIds.includes(id) && (isChosen ? chosenLeafIds.includes(id) : !chosenLeafIds.includes(id))
125
+ );
126
+
127
+ const isNodeChecked = (node: FoodNode, isChosen: boolean) => {
128
+ if (areAllDescendantsSelected(node, isChosen)) {
129
+ return true;
130
+ }
131
+ if (areSomeDescendantsSelected(node, isChosen)) {
132
+ return null;
133
+ }
134
+ return false;
135
+ };
136
+
137
+ const onOptionCheck = (
138
+ event: React.MouseEvent | React.ChangeEvent<HTMLInputElement> | React.KeyboardEvent,
139
+ isChecked: boolean,
140
+ node: DualListSelectorTreeItemData,
141
+ isChosen: boolean
142
+ ) => {
143
+ const nodeIdsToCheck = memoizedLeavesById[node.id].filter(id =>
144
+ isChosen
145
+ ? chosenLeafIds.includes(id) && !hiddenChosen.includes(id)
146
+ : !chosenLeafIds.includes(id) && !hiddenAvailable.includes(id)
147
+ );
148
+ if (isChosen) {
149
+ hiddenChosen = [];
150
+ } else {
151
+ hiddenAvailable = [];
152
+ }
153
+ setCheckedLeafIds(prevChecked => {
154
+ const otherCheckedNodeNames = prevChecked.filter(id => !nodeIdsToCheck.includes(id));
155
+ return !isChecked ? otherCheckedNodeNames : [...otherCheckedNodeNames, ...nodeIdsToCheck];
156
+ });
157
+ };
158
+
159
+ // builds a search input - used in each dual list selector pane
160
+ const buildSearchInput = (isChosen: boolean) => {
161
+ const onChange = value => (isChosen ? setChosenFilter(value) : setAvailableFilter(value));
162
+
163
+ return (
164
+ <SearchInput value={isChosen ? chosenFilter : availableFilter} onChange={onChange} onClear={() => onChange('')} />
165
+ );
166
+ };
167
+
168
+ // Builds the DualListSelectorTreeItems from the FoodNodes
169
+ const buildOptions = (
170
+ isChosen: boolean,
171
+ [node, ...remainingNodes]: FoodNode[],
172
+ hasParentMatch: boolean
173
+ ): DualListSelectorTreeItemData[] => {
174
+ if (!node) {
175
+ return [];
176
+ }
177
+
178
+ const isChecked = isNodeChecked(node, isChosen);
179
+
180
+ const filterValue = isChosen ? chosenFilter : availableFilter;
181
+ const descendentLeafIds = memoizedLeavesById[node.id];
182
+ const descendentsOnThisPane = isChosen
183
+ ? descendentLeafIds.filter(id => chosenLeafIds.includes(id))
184
+ : descendentLeafIds.filter(id => !chosenLeafIds.includes(id));
185
+
186
+ const hasMatchingChildren =
187
+ filterValue && descendentsOnThisPane.some(id => memoizedNodeText[id].includes(filterValue));
188
+ const isFilterMatch = filterValue && node.text.includes(filterValue) && descendentsOnThisPane.length > 0;
189
+
190
+ // A node is displayed if either of the following is true:
191
+ // - There is no filter value and this node or its descendents belong on this pane
192
+ // - There is a filter value and this node or one of this node's descendents or ancestors match on this pane
193
+ const isDisplayed =
194
+ (!filterValue && descendentsOnThisPane.length > 0) ||
195
+ hasMatchingChildren ||
196
+ (hasParentMatch && descendentsOnThisPane.length > 0) ||
197
+ isFilterMatch;
198
+
199
+ if (!isDisplayed) {
200
+ if (isChosen) {
201
+ hiddenChosen.push(node.id);
202
+ } else {
203
+ hiddenAvailable.push(node.id);
204
+ }
205
+ }
206
+
207
+ return [
208
+ ...(isDisplayed
209
+ ? [
210
+ {
211
+ id: node.id,
212
+ text: node.text,
213
+ isChecked,
214
+ checkProps: { 'aria-label': `Select ${node.text}` },
215
+ hasBadge: node.children && node.children.length > 0,
216
+ badgeProps: { isRead: true },
217
+ defaultExpanded: isChosen ? !!chosenFilter : !!availableFilter,
218
+ children: node.children
219
+ ? buildOptions(isChosen, node.children, isFilterMatch || hasParentMatch)
220
+ : undefined
221
+ }
222
+ ]
223
+ : []),
224
+ ...(!isDisplayed && node.children && node.children.length
225
+ ? buildOptions(isChosen, node.children, hasParentMatch)
226
+ : []),
227
+ ...(remainingNodes ? buildOptions(isChosen, remainingNodes, hasParentMatch) : [])
228
+ ];
229
+ };
230
+
231
+ const buildPane = (isChosen: boolean): React.ReactNode => {
232
+ const options: DualListSelectorTreeItemData[] = buildOptions(isChosen, data, false);
233
+ const numOptions = isChosen ? chosenLeafIds.length : memoizedAllLeaves.length - chosenLeafIds.length;
234
+ const numSelected = checkedLeafIds.filter(id =>
235
+ isChosen ? chosenLeafIds.includes(id) : !chosenLeafIds.includes(id)
236
+ ).length;
237
+ const status = `${numSelected} of ${numOptions} options selected`;
238
+ return (
239
+ <DualListSelectorPane
240
+ title={isChosen ? 'Chosen' : 'Available'}
241
+ status={status}
242
+ searchInput={buildSearchInput(isChosen)}
243
+ isChosen={isChosen}
244
+ >
245
+ <DualListSelectorList>
246
+ <DualListSelectorTree
247
+ data={options}
248
+ onOptionCheck={(e, isChecked, itemData) => onOptionCheck(e, isChecked, itemData, isChosen)}
249
+ />
250
+ </DualListSelectorList>
251
+ </DualListSelectorPane>
252
+ );
253
+ };
254
+
255
+ return (
256
+ <DualListSelector isTree>
257
+ {buildPane(false)}
258
+ <DualListSelectorControlsWrapper aria-label="Selector controls">
259
+ <DualListSelectorControl
260
+ isDisabled={!checkedLeafIds.filter(x => !chosenLeafIds.includes(x)).length}
261
+ onClick={() => moveChecked(true)}
262
+ aria-label="Add selected"
263
+ >
264
+ <AngleRightIcon />
265
+ </DualListSelectorControl>
266
+ <DualListSelectorControl
267
+ isDisabled={chosenLeafIds.length === memoizedAllLeaves.length}
268
+ onClick={() => moveAll(true)}
269
+ aria-label="Add all"
270
+ >
271
+ <AngleDoubleRightIcon />
272
+ </DualListSelectorControl>
273
+ <DualListSelectorControl
274
+ isDisabled={chosenLeafIds.length === 0}
275
+ onClick={() => moveAll(false)}
276
+ aria-label="Remove all"
277
+ >
278
+ <AngleDoubleLeftIcon />
279
+ </DualListSelectorControl>
280
+ <DualListSelectorControl
281
+ onClick={() => moveChecked(false)}
282
+ isDisabled={!checkedLeafIds.filter(x => !!chosenLeafIds.includes(x)).length}
283
+ aria-label="Remove selected"
284
+ >
285
+ <AngleLeftIcon />
286
+ </DualListSelectorControl>
287
+ </DualListSelectorControlsWrapper>
288
+ {buildPane(true)}
289
+ </DualListSelector>
290
+ );
291
+ };
292
+
293
+ export const ComposableDualListSelectorTreeExample: React.FunctionComponent = () => (
294
+ <ComposableDualListSelectorTree
295
+ data={[
296
+ {
297
+ id: 'fruits',
298
+ text: 'Fruits',
299
+ children: [
300
+ { id: 'apple', text: 'Apple' },
301
+ {
302
+ id: 'berries',
303
+ text: 'Berries',
304
+ children: [
305
+ { id: 'blueberry', text: 'Blueberry' },
306
+ { id: 'strawberry', text: 'Strawberry' }
307
+ ]
308
+ },
309
+ { id: 'banana', text: 'Banana' }
310
+ ]
311
+ },
312
+ { id: 'bread', text: 'Bread' },
313
+ {
314
+ id: 'vegetables',
315
+ text: 'Vegetables',
316
+ children: [
317
+ { id: 'broccoli', text: 'Broccoli' },
318
+ { id: 'cauliflower', text: 'Cauliflower' }
319
+ ]
320
+ },
321
+ {
322
+ id: 'proteins',
323
+ text: 'Proteins',
324
+ children: [
325
+ { id: 'beans', text: 'Beans' },
326
+ {
327
+ id: 'meats',
328
+ text: 'Meats',
329
+ children: [
330
+ {
331
+ id: 'beef',
332
+ text: 'Beef'
333
+ },
334
+ {
335
+ id: 'chicken',
336
+ text: 'Chicken'
337
+ }
338
+ ]
339
+ },
340
+ { id: 'tofu', text: 'Tofu' }
341
+ ]
342
+ }
343
+ ]}
344
+ />
345
+ );
@@ -8,7 +8,6 @@ propComponents: [
8
8
  'DualListSelectorControl',
9
9
  'DualListSelectorControlsWrapper',
10
10
  'DualListSelectorTree',
11
- 'DualListSelectorListItem',
12
11
  'DualListSelectorTreeItemData'
13
12
  ]
14
13
  beta: true
@@ -822,3 +821,8 @@ const ComposableDualListSelector = () => {
822
821
  );
823
822
  }
824
823
  ```
824
+
825
+ ### Composable dual list selector tree
826
+ ```ts file="ComposableTree.tsx"
827
+ ```
828
+