@patternfly/react-data-view 5.5.0 → 5.5.1

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.
@@ -40,19 +40,31 @@ const react_table_1 = require("@patternfly/react-table");
40
40
  const InternalContext_1 = require("../InternalContext");
41
41
  const DataViewTableHead_1 = require("../DataViewTableHead");
42
42
  const DataViewTable_1 = require("../DataViewTable");
43
- const getDescendants = (node) => (!node.children || !node.children.length) ? [node] : node.children.flatMap(getDescendants);
44
- const isNodeChecked = (node, isSelected) => {
45
- let allSelected = true;
46
- let someSelected = false;
47
- for (const descendant of getDescendants(node)) {
48
- const selected = !!(isSelected === null || isSelected === void 0 ? void 0 : isSelected(descendant));
49
- someSelected || (someSelected = selected);
50
- allSelected && (allSelected = selected);
51
- if (!allSelected && someSelected) {
52
- return null;
43
+ const getNodesAffectedBySelection = (allRows, node, isChecking, isSelected) => {
44
+ const getDescendants = (node) => node.children ? node.children.flatMap(getDescendants).concat(node) : [node];
45
+ const findParent = (child, rows) => {
46
+ var _a;
47
+ return (_a = rows.find(row => { var _a; return (_a = row.children) === null || _a === void 0 ? void 0 : _a.some(c => c === child); })) !== null && _a !== void 0 ? _a : rows.flatMap(row => { var _a; return (_a = row.children) !== null && _a !== void 0 ? _a : []; }).map(c => findParent(child, [c])).find(p => p);
48
+ };
49
+ const getAncestors = (node) => {
50
+ const ancestors = [];
51
+ let parent = findParent(node, allRows);
52
+ while (parent) {
53
+ ancestors.push(parent);
54
+ parent = findParent(parent, allRows);
53
55
  }
54
- }
55
- return allSelected;
56
+ return ancestors;
57
+ };
58
+ const affectedNodes = new Set([node, ...getDescendants(node)]);
59
+ getAncestors(node).forEach(ancestor => {
60
+ var _a, _b;
61
+ const allChildrenSelected = (_a = ancestor.children) === null || _a === void 0 ? void 0 : _a.every(child => (isSelected === null || isSelected === void 0 ? void 0 : isSelected(child)) || affectedNodes.has(child));
62
+ const anyChildAffected = (_b = ancestor.children) === null || _b === void 0 ? void 0 : _b.some(child => affectedNodes.has(child) || child.id === node.id);
63
+ if (isChecking ? !(isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && allChildrenSelected : (isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && anyChildAffected) {
64
+ affectedNodes.add(ancestor);
65
+ }
66
+ });
67
+ return Array.from(affectedNodes);
56
68
  };
57
69
  const DataViewTableTree = (_a) => {
58
70
  var { columns, rows, headStates, bodyStates, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "headStates", "bodyStates", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
@@ -70,7 +82,7 @@ const DataViewTableTree = (_a) => {
70
82
  }
71
83
  const isExpanded = expandedNodeIds.includes(node.id);
72
84
  const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
73
- const isChecked = isSelected && isNodeChecked(node, isSelected);
85
+ const isChecked = isSelected === null || isSelected === void 0 ? void 0 : isSelected(node);
74
86
  let icon = leafIcon;
75
87
  if (node.children) {
76
88
  icon = isExpanded ? expandedIcon : collapsedIcon;
@@ -84,7 +96,7 @@ const DataViewTableTree = (_a) => {
84
96
  const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
85
97
  return isDetailsExpanded ? otherDetailsExpandedNodeIds : [...otherDetailsExpandedNodeIds, node.id];
86
98
  }),
87
- onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getDescendants(node)),
99
+ onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getNodesAffectedBySelection(rows, node, isChecking, isSelected)),
88
100
  rowIndex,
89
101
  props: {
90
102
  isExpanded,
@@ -14,19 +14,31 @@ import { Table, Tbody, Td, TreeRowWrapper, } from '@patternfly/react-table';
14
14
  import { useInternalContext } from '../InternalContext';
15
15
  import { DataViewTableHead } from '../DataViewTableHead';
16
16
  import { isDataViewTdObject } from '../DataViewTable';
17
- const getDescendants = (node) => (!node.children || !node.children.length) ? [node] : node.children.flatMap(getDescendants);
18
- const isNodeChecked = (node, isSelected) => {
19
- let allSelected = true;
20
- let someSelected = false;
21
- for (const descendant of getDescendants(node)) {
22
- const selected = !!(isSelected === null || isSelected === void 0 ? void 0 : isSelected(descendant));
23
- someSelected || (someSelected = selected);
24
- allSelected && (allSelected = selected);
25
- if (!allSelected && someSelected) {
26
- return null;
17
+ const getNodesAffectedBySelection = (allRows, node, isChecking, isSelected) => {
18
+ const getDescendants = (node) => node.children ? node.children.flatMap(getDescendants).concat(node) : [node];
19
+ const findParent = (child, rows) => {
20
+ var _a;
21
+ return (_a = rows.find(row => { var _a; return (_a = row.children) === null || _a === void 0 ? void 0 : _a.some(c => c === child); })) !== null && _a !== void 0 ? _a : rows.flatMap(row => { var _a; return (_a = row.children) !== null && _a !== void 0 ? _a : []; }).map(c => findParent(child, [c])).find(p => p);
22
+ };
23
+ const getAncestors = (node) => {
24
+ const ancestors = [];
25
+ let parent = findParent(node, allRows);
26
+ while (parent) {
27
+ ancestors.push(parent);
28
+ parent = findParent(parent, allRows);
27
29
  }
28
- }
29
- return allSelected;
30
+ return ancestors;
31
+ };
32
+ const affectedNodes = new Set([node, ...getDescendants(node)]);
33
+ getAncestors(node).forEach(ancestor => {
34
+ var _a, _b;
35
+ const allChildrenSelected = (_a = ancestor.children) === null || _a === void 0 ? void 0 : _a.every(child => (isSelected === null || isSelected === void 0 ? void 0 : isSelected(child)) || affectedNodes.has(child));
36
+ const anyChildAffected = (_b = ancestor.children) === null || _b === void 0 ? void 0 : _b.some(child => affectedNodes.has(child) || child.id === node.id);
37
+ if (isChecking ? !(isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && allChildrenSelected : (isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && anyChildAffected) {
38
+ affectedNodes.add(ancestor);
39
+ }
40
+ });
41
+ return Array.from(affectedNodes);
30
42
  };
31
43
  export const DataViewTableTree = (_a) => {
32
44
  var { columns, rows, headStates, bodyStates, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "headStates", "bodyStates", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
@@ -44,7 +56,7 @@ export const DataViewTableTree = (_a) => {
44
56
  }
45
57
  const isExpanded = expandedNodeIds.includes(node.id);
46
58
  const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
47
- const isChecked = isSelected && isNodeChecked(node, isSelected);
59
+ const isChecked = isSelected === null || isSelected === void 0 ? void 0 : isSelected(node);
48
60
  let icon = leafIcon;
49
61
  if (node.children) {
50
62
  icon = isExpanded ? expandedIcon : collapsedIcon;
@@ -58,7 +70,7 @@ export const DataViewTableTree = (_a) => {
58
70
  const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
59
71
  return isDetailsExpanded ? otherDetailsExpandedNodeIds : [...otherDetailsExpandedNodeIds, node.id];
60
72
  }),
61
- onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getDescendants(node)),
73
+ onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getNodesAffectedBySelection(rows, node, isChecking, isSelected)),
62
74
  rowIndex,
63
75
  props: {
64
76
  isExpanded,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-data-view",
3
- "version": "5.5.0",
3
+ "version": "5.5.1",
4
4
  "description": "Data view used for Red Hat projects.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -12,24 +12,45 @@ import { DataViewTableHead } from '../DataViewTableHead';
12
12
  import { DataViewTh, DataViewTrTree, isDataViewTdObject } from '../DataViewTable';
13
13
  import { DataViewState } from '../DataView/DataView';
14
14
 
15
- const getDescendants = (node: DataViewTrTree): DataViewTrTree[] => (!node.children || !node.children.length) ? [ node ] : node.children.flatMap(getDescendants);
16
-
17
- const isNodeChecked = (node: DataViewTrTree, isSelected: (node: DataViewTrTree) => boolean) => {
18
- let allSelected = true;
19
- let someSelected = false;
20
-
21
- for (const descendant of getDescendants(node)) {
22
- const selected = !!isSelected?.(descendant);
23
-
24
- someSelected ||= selected;
25
- allSelected &&= selected;
26
-
27
- if (!allSelected && someSelected) { return null }
28
- }
29
-
30
- return allSelected;
15
+ const getNodesAffectedBySelection = (
16
+ allRows: DataViewTrTree[],
17
+ node: DataViewTrTree,
18
+ isChecking: boolean,
19
+ isSelected?: (item: DataViewTrTree) => boolean
20
+ ): DataViewTrTree[] => {
21
+
22
+ const getDescendants = (node: DataViewTrTree): DataViewTrTree[] =>
23
+ node.children ? node.children.flatMap(getDescendants).concat(node) : [ node ];
24
+
25
+ const findParent = (child: DataViewTrTree, rows: DataViewTrTree[]): DataViewTrTree | undefined =>
26
+ rows.find(row => row.children?.some(c => c === child)) ??
27
+ rows.flatMap(row => row.children ?? []).map(c => findParent(child, [ c ])).find(p => p);
28
+
29
+ const getAncestors = (node: DataViewTrTree): DataViewTrTree[] => {
30
+ const ancestors: DataViewTrTree[] = [];
31
+ let parent = findParent(node, allRows);
32
+ while (parent) {
33
+ ancestors.push(parent);
34
+ parent = findParent(parent, allRows);
35
+ }
36
+ return ancestors;
37
+ };
38
+
39
+ const affectedNodes = new Set([ node, ...getDescendants(node) ]);
40
+
41
+ getAncestors(node).forEach(ancestor => {
42
+ const allChildrenSelected = ancestor.children?.every(child => isSelected?.(child) || affectedNodes.has(child));
43
+ const anyChildAffected = ancestor.children?.some(child => affectedNodes.has(child) || child.id === node.id);
44
+
45
+ if (isChecking ? !isSelected?.(ancestor) && allChildrenSelected : isSelected?.(ancestor) && anyChildAffected) {
46
+ affectedNodes.add(ancestor);
47
+ }
48
+ });
49
+
50
+ return Array.from(affectedNodes);
31
51
  };
32
52
 
53
+
33
54
  /** extends TableProps */
34
55
  export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
35
56
  /** Columns definition */
@@ -83,7 +104,7 @@ export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
83
104
  }
84
105
  const isExpanded = expandedNodeIds.includes(node.id);
85
106
  const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
86
- const isChecked = isSelected && isNodeChecked(node, isSelected);
107
+ const isChecked = isSelected?.(node);
87
108
  let icon = leafIcon;
88
109
  if (node.children) {
89
110
  icon = isExpanded ? expandedIcon : collapsedIcon;
@@ -100,7 +121,7 @@ export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
100
121
  const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
101
122
  return isDetailsExpanded ? otherDetailsExpandedNodeIds : [ ...otherDetailsExpandedNodeIds, node.id ];
102
123
  }),
103
- onCheckChange: (isSelectDisabled?.(node) || !onSelect) ? undefined : (_event, isChecking) => onSelect?.(isChecking, getDescendants(node)),
124
+ onCheckChange: (isSelectDisabled?.(node) || !onSelect) ? undefined : (_event, isChecking) => onSelect?.(isChecking, getNodesAffectedBySelection(rows, node, isChecking, isSelected)),
104
125
  rowIndex,
105
126
  props: {
106
127
  isExpanded,