Skip to content

Commit 76f6bf4

Browse files
Fix group labels, revert breaking change (#8299)
1 parent 8faac0c commit 76f6bf4

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

packages/react-integration/demo-app-ts/src/components/demos/TopologyDemo/PipelineLayout.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
GRAPH_LAYOUT_END_EVENT,
1616
getSpacerNodes,
1717
getEdgesFromNodes,
18-
DEFAULT_EDGE_TYPE
18+
DEFAULT_EDGE_TYPE,
19+
DEFAULT_SPACER_NODE_TYPE,
20+
DEFAULT_FINALLY_NODE_TYPE
1921
} from '@patternfly/react-topology';
2022
import '@patternfly/react-styles/css/components/Topology/topology-components.css';
2123
import pipelineComponentFactory, { GROUPED_EDGE_TYPE } from './components/pipelineComponentFactory';
@@ -49,9 +51,14 @@ const TopologyPipelineLayout: React.FC = () => {
4951
React.useEffect(() => {
5052
const spacerNodes = getSpacerNodes(pipelineNodes);
5153
const nodes = [...pipelineNodes, ...spacerNodes];
54+
const edgeType = showGroups ? GROUPED_EDGE_TYPE : DEFAULT_EDGE_TYPE;
5255
const edges = getEdgesFromNodes(
5356
nodes.filter(n => !n.group),
54-
showGroups ? GROUPED_EDGE_TYPE : DEFAULT_EDGE_TYPE
57+
DEFAULT_SPACER_NODE_TYPE,
58+
edgeType,
59+
edgeType,
60+
[DEFAULT_FINALLY_NODE_TYPE],
61+
edgeType
5562
);
5663

5764
controller.fromModel(

packages/react-integration/demo-app-ts/src/components/demos/TopologyDemo/useDemoPipelineNodes.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export const useDemoPipelineNodes = (
156156
type: 'task-group',
157157
children: parallelTasks.map(t => t.id),
158158
group: true,
159-
style: { padding: [15, 15] }
159+
label: 'Parallel tasks'
160160
});
161161
}
162162
}
@@ -184,8 +184,7 @@ export const useDemoPipelineNodes = (
184184
id: 'finally-group',
185185
type: 'finally-group',
186186
children: finallyNodes.map(n => n.id),
187-
group: true,
188-
style: { padding: [15, 15] }
187+
group: true
189188
};
190189

191190
if (showGroups) {
@@ -197,7 +196,8 @@ export const useDemoPipelineNodes = (
197196
id: `group-${task.data.columnGroup}`,
198197
type: 'task-group',
199198
children: [],
200-
group: true
199+
group: true,
200+
label: `Group ${task.data.columnGroup}`
201201
};
202202
acc.push(taskGroup);
203203
}

packages/react-topology/src/pipelines/components/groups/DefaultTaskGroup.tsx

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import CollapseIcon from '@patternfly/react-icons/dist/esm/icons/compress-alt-ic
66
import NodeLabel from '../../../components/nodes/labels/NodeLabel';
77
import { Layer } from '../../../components/layers';
88
import { GROUPS_LAYER } from '../../../const';
9-
import { useCombineRefs, useHover } from '../../../utils';
10-
import { BadgeLocation, isGraph, Node } from '../../../types';
9+
import { maxPadding, useCombineRefs, useHover } from '../../../utils';
10+
import { BadgeLocation, isGraph, LabelPosition, Node, NodeStyle } from '../../../types';
1111
import {
1212
useDragNode,
1313
WithContextMenuProps,
@@ -35,6 +35,7 @@ type DefaultTaskGroupProps = {
3535
badgeBorderColor?: string;
3636
badgeClassName?: string;
3737
badgeLocation?: BadgeLocation;
38+
labelOffset?: number; // Space between the label and the group
3839
labelIconClass?: string; // Icon to show in label
3940
labelIcon?: string;
4041
labelIconPadding?: number;
@@ -63,6 +64,7 @@ const DefaultTaskGroup: React.FunctionComponent<DefaultTaskGroupProps> = ({
6364
badgeBorderColor,
6465
badgeClassName,
6566
badgeLocation,
67+
labelOffset = 17,
6668
labelIconClass,
6769
labelIcon,
6870
labelIconPadding,
@@ -73,6 +75,7 @@ const DefaultTaskGroup: React.FunctionComponent<DefaultTaskGroupProps> = ({
7375
const dragLabelRef = useDragNode()[1];
7476
const refs = useCombineRefs<SVGPathElement>(hoverRef, dragNodeRef);
7577
const isHover = hover !== undefined ? hover : hovered;
78+
const labelPosition = element.getLabelPosition();
7679

7780
let parent = element.getParent();
7881
let altGroup = false;
@@ -82,16 +85,40 @@ const DefaultTaskGroup: React.FunctionComponent<DefaultTaskGroupProps> = ({
8285
}
8386

8487
const children = element.getNodes().filter(c => c.isVisible());
88+
89+
// cast to number and coerce
90+
const padding = maxPadding(element.getStyle<NodeStyle>().padding ?? 17);
91+
92+
const { minX, minY, maxX, maxY } = children.reduce(
93+
(acc, child) => {
94+
const bounds = child.getBounds();
95+
return {
96+
minX: Math.min(acc.minX, bounds.x - padding),
97+
minY: Math.min(acc.minY, bounds.y - padding),
98+
maxX: Math.max(acc.maxX, bounds.x + bounds.width + padding),
99+
maxY: Math.max(acc.maxY, bounds.y + bounds.height + padding)
100+
};
101+
},
102+
{ minX: Infinity, minY: Infinity, maxX: 0, maxY: 0 }
103+
);
104+
105+
const [labelX, labelY] = React.useMemo(() => {
106+
if (!showLabel || !(label || element.getLabel())) {
107+
return [0, 0];
108+
}
109+
switch (labelPosition) {
110+
case LabelPosition.right:
111+
return [maxX + labelOffset, minY + (maxY - minY) / 2];
112+
case LabelPosition.bottom:
113+
default:
114+
return [minX + (maxX - minX) / 2, maxY + labelOffset];
115+
}
116+
}, [element, label, labelOffset, labelPosition, maxX, maxY, minX, minY, showLabel]);
117+
85118
if (children.length === 0) {
86119
return null;
87120
}
88121

89-
const bounds = element.getBounds();
90-
const minX = bounds.x;
91-
const minY = bounds.y;
92-
const maxX = bounds.x + bounds.width;
93-
const maxY = bounds.y + bounds.height;
94-
95122
const groupClassName = css(
96123
styles.topologyGroup,
97124
className,
@@ -121,8 +148,9 @@ const DefaultTaskGroup: React.FunctionComponent<DefaultTaskGroupProps> = ({
121148
{showLabel && (label || element.getLabel()) && (
122149
<NodeLabel
123150
className={styles.topologyGroupLabel}
124-
x={(maxX - minX) / 2}
125-
y={maxY + 17}
151+
x={labelX}
152+
y={labelY}
153+
position={labelPosition}
126154
paddingX={8}
127155
paddingY={5}
128156
dragRef={dragNodeRef ? dragLabelRef : undefined}

packages/react-topology/src/pipelines/utils/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ export const getSpacerNodes = (
114114

115115
export const getEdgesFromNodes = (
116116
nodes: PipelineNodeModel[],
117-
edgeType = DEFAULT_EDGE_TYPE,
118117
spacerNodeType = DEFAULT_SPACER_NODE_TYPE,
119-
finallyNodeTypes: string[] = [DEFAULT_FINALLY_NODE_TYPE]
118+
edgeType = DEFAULT_EDGE_TYPE,
119+
spacerEdgeType = DEFAULT_EDGE_TYPE,
120+
finallyNodeTypes: string[] = [DEFAULT_FINALLY_NODE_TYPE],
121+
finallyEdgeType = DEFAULT_EDGE_TYPE
120122
): EdgeModel[] => {
121123
const edges: EdgeModel[] = [];
122124

@@ -135,7 +137,7 @@ export const getEdgesFromNodes = (
135137
if (node && !finallyNodes.includes(node)) {
136138
edges.push({
137139
id: `${sourceId}-${spacer.id}`,
138-
type: edgeType,
140+
type: spacerEdgeType,
139141
source: sourceId,
140142
target: spacer.id
141143
});
@@ -150,7 +152,7 @@ export const getEdgesFromNodes = (
150152
if (spacer) {
151153
edges.push({
152154
id: `${spacer.id}-${node.id}`,
153-
type: edgeType,
155+
type: spacerEdgeType,
154156
source: spacer.id,
155157
target: node.id
156158
});
@@ -172,15 +174,15 @@ export const getEdgesFromNodes = (
172174
finallyNodes.forEach(finallyNode => {
173175
edges.push({
174176
id: `${finallyId}-${finallyNode.id}`,
175-
type: edgeType,
177+
type: spacerEdgeType,
176178
source: finallyId,
177179
target: finallyNode.id
178180
});
179181
});
180182
lastTasks.forEach(lastTaskNode => {
181183
edges.push({
182184
id: `${lastTaskNode.id}-${finallyId}`,
183-
type: edgeType,
185+
type: spacerEdgeType,
184186
source: lastTaskNode.id,
185187
target: finallyId
186188
});
@@ -190,7 +192,7 @@ export const getEdgesFromNodes = (
190192
lastTasks.forEach(lastTaskNode => {
191193
edges.push({
192194
id: `finallyId-${lastTaskNode.id}-${finallyNodes[0].id}`,
193-
type: edgeType,
195+
type: finallyEdgeType,
194196
source: lastTaskNode.id,
195197
target: finallyNodes[0].id
196198
});

0 commit comments

Comments
 (0)