Skip to content

Commit e5cd27e

Browse files
authored
Add a labelClass property to Node (#446)
* Add a labelClass property to Node The `labelClass` property can be used to pass a CSS class name to the node, and it will be applied to its label. * Update jest snapshots Co-authored-by: Diogo André de Assumpção <daa@fb.com>
1 parent 662f386 commit e5cd27e

File tree

6 files changed

+22
-0
lines changed

6 files changed

+22
-0
lines changed

sandbox/data/custom-node/custom-node.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
highlightStrokeColor: "SAME",
2626
highlightStrokeWidth: 1.5,
2727
labelProperty: "name",
28+
labelClass: "person-node-label",
2829
mouseCursor: "pointer",
2930
opacity: 1,
3031
renderLabel: false,

sandbox/data/custom-node/res/styles/custom-node.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@
6565
width: 80%;
6666
height: 30%;
6767
}
68+
69+
/* --- Node label style --- */
70+
71+
.person-node-label {
72+
fill: black;
73+
text-shadow: rgb(255, 255, 255) 0px -2px 0px, rgb(255, 255, 255) 0px 2px 0px, rgb(255, 255, 255) 2px 0px 0px,
74+
rgb(255, 255, 255) -2px 0px 0px;
75+
transform: rotate(-25deg) translateX(40px) translateY(15px);
76+
}

src/components/graph/graph.builder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
236236
renderLabel = node.renderLabel;
237237
}
238238

239+
var labelClass = config.node.labelClass;
240+
if (node.labelClass !== undefined && typeof node.labelClass === "string") {
241+
labelClass = node.labelClass;
242+
}
243+
239244
return {
240245
...node,
241246
className: CONST.NODE_CLASS_NAME,
@@ -253,6 +258,7 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
253258
opacity,
254259
overrideGlobalViewGenerator: !node.viewGenerator && node.svg,
255260
renderLabel,
261+
labelClass,
256262
size: isSizeNumericValue ? nodeSize * t : { height: nodeSize.height * t, width: nodeSize.width * t },
257263
stroke,
258264
strokeWidth: strokeWidth * t,

src/components/graph/graph.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
* - "top"
138138
* - "bottom"
139139
* - "center"
140+
* @param {string} [node.labelClass=""] - <a id="node-label-class href="#node-label-class">🔗</a> 🔍 CSS class to apply to the node label.
140141
*
141142
* <b>[note]</b> not specifying a label position will fallback to the original placement scheme of to the right of the node. This is different than the implementation for "right", which has the label shifted very slightly upward compared to the original.
142143
* @param {string|Function} [node.labelProperty="id"] - <a id="node-label-property" href="#node-label-property">🔗</a> this is the node property that will be used in runtime to</br>
@@ -312,6 +313,7 @@ export default {
312313
highlightStrokeWidth: "SAME",
313314
labelProperty: "id",
314315
labelPosition: null,
316+
labelClass: "",
315317
mouseCursor: "pointer",
316318
opacity: 1,
317319
renderLabel: true,

src/components/node/Node.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export default class Node extends React.Component {
9494
fontSize: this.props.fontSize,
9595
fontWeight: this.props.fontWeight,
9696
opacity: this.props.opacity,
97+
...(this.props.labelClass !== "" && { className: this.props.labelClass }),
9798
};
9899

99100
let size = this.props.size;

test/graph/__snapshots__/graph.builder.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Object {
1515
"highlighted": false,
1616
"id": "id",
1717
"label": "id",
18+
"labelClass": "",
1819
"labelPosition": null,
1920
"onClickNode": undefined,
2021
"onMouseOut": undefined,
@@ -50,6 +51,7 @@ Object {
5051
"highlighted": false,
5152
"id": "id",
5253
"label": "id",
54+
"labelClass": "",
5355
"labelPosition": null,
5456
"onClickNode": undefined,
5557
"onMouseOut": undefined,
@@ -85,6 +87,7 @@ Object {
8587
"highlighted": true,
8688
"id": "id",
8789
"label": "id",
90+
"labelClass": "",
8891
"labelPosition": null,
8992
"onClickNode": undefined,
9093
"onMouseOut": undefined,

0 commit comments

Comments
 (0)