Skip to content

Commit 99730df

Browse files
committed
feat: add labels customization in CytoViz NodeData component
Tables header and attributes labels can now be provided with the 'labels' prop in NodeData
1 parent a6174ec commit 99730df

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/charts/CytoViz/CytoViz.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export const CytoViz = (props) => {
2727
const { cytoscapeStylesheet, defaultSettings, elements, extraLayouts, labels, loading, getNodeDetails, bubblesets } =
2828
props;
2929

30+
let getNodeDetailsCallback = getNodeDetails;
31+
if (!getNodeDetailsCallback) {
32+
// eslint-disable-next-line react/display-name
33+
getNodeDetailsCallback = (node) => <NodeData data={node.data()} labels={labels.nodeData} />;
34+
getNodeDetailsCallback.displayName = 'NodeData';
35+
}
36+
3037
// Layout
3138
const [isDrawerOpen, setIsDrawerOpen] = React.useState(false);
3239
const [currentDrawerTab, setCurrentDrawerTab] = React.useState(0);
@@ -76,7 +83,7 @@ export const CytoViz = (props) => {
7683
// Init node selection behavior
7784
cytoscapeRef.on('select', 'node', function (e) {
7885
const selectedNode = e.target;
79-
setCurrentNodeDetails(getNodeDetails(selectedNode));
86+
setCurrentNodeDetails(getNodeDetailsCallback(selectedNode));
8087
});
8188
cytoscapeRef.on('unselect', 'node', function (e) {
8289
setCurrentNodeDetails(null);
@@ -86,7 +93,7 @@ export const CytoViz = (props) => {
8693
const selectedNode = e.target;
8794
setCurrentDrawerTab(0);
8895
setIsDrawerOpen(true);
89-
setCurrentNodeDetails(getNodeDetails(selectedNode));
96+
setCurrentNodeDetails(getNodeDetailsCallback(selectedNode));
9097
});
9198

9299
// Init bubblesets
@@ -287,16 +294,38 @@ CytoViz.propTypes = {
287294
spacingFactor: 'string',
288295
zoomLimits: 'string',
289296
}
297+
nodeData: {
298+
dictKey: 'string',
299+
dictValue: 'string',
300+
}
290301
}
291302
* </pre>
292303
*/
293304
labels: PropTypes.object,
294305
/**
295-
* Array of cytoscape elements to display
306+
* Boolean defining whether or not the data are loading.
307+
While loading is true, a spinner is displayed instead of the cytoscape component.
296308
*/
297309
loading: PropTypes.bool,
298310
};
299311

312+
const DEFAULT_LABELS = {
313+
elementDetails: 'Details',
314+
loading: 'Loading...',
315+
noSelectedNode: 'Select a node to view its data',
316+
settings: {
317+
compactMode: 'Compact layout',
318+
layout: 'Layout',
319+
title: 'Settings',
320+
spacingFactor: 'Spacing factor',
321+
zoomLimits: 'Min & max zoom',
322+
},
323+
nodeData: {
324+
dictKey: 'Key',
325+
dictValue: 'Value',
326+
},
327+
};
328+
300329
CytoViz.defaultProps = {
301330
cytoscapeStylesheet: [],
302331
defaultSettings: {
@@ -307,19 +336,7 @@ CytoViz.defaultProps = {
307336
spacingFactor: 1,
308337
},
309338
extraLayouts: {},
310-
getNodeDetails: (node) => <NodeData data={node.data()} />,
311339
groups: {},
312-
labels: {
313-
elementDetails: 'Details',
314-
loading: 'Loading...',
315-
noSelectedNode: 'Select a node to view its data',
316-
settings: {
317-
compactMode: 'Compact layout',
318-
layout: 'Layout',
319-
title: 'Settings',
320-
spacingFactor: 'Spacing factor',
321-
zoomLimits: 'Min & max zoom',
322-
},
323-
},
340+
labels: DEFAULT_LABELS,
324341
loading: false,
325342
};

src/charts/CytoViz/components/NodeData/NodeData.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ import React from 'react';
55
import PropTypes from 'prop-types';
66
import useStyles from './style';
77

8-
const _generateAttributeDetails = (classes, attributeName, attributeValue) => {
9-
const attributesToIgnore = ['label', 'Label', 'parent'];
8+
const _generateAttributeDetails = (classes, labels, attributeName, attributeValue) => {
9+
const attributeLabel = labels?.attributes?.[attributeName] || attributeName;
10+
const attributesToIgnore = ['label', 'Label', 'parent', 'source', 'target'];
1011
if (attributesToIgnore.indexOf(attributeName) !== -1) {
1112
return null;
1213
}
1314
if (typeof attributeValue === 'object' && 0 in attributeValue) {
1415
// List represented as an object
1516
return (
1617
<div key={attributeName} className={classes.attributeColumnContainer}>
17-
<div className={classes.attributeLabel}>{attributeName}:</div>
18+
<div className={classes.attributeLabel}>{attributeLabel}:</div>
1819
<div className={classes.tableContainer}>
1920
<table className={classes.table}>
2021
<thead>
2122
<tr>
22-
<th className={classes.th}>Iteration</th>
23-
<th className={classes.th}>Value</th>
23+
<th className={classes.th}>{labels.dictKey}</th>
24+
<th className={classes.th}>{labels.dictValue}</th>
2425
</tr>
2526
</thead>
2627
<tbody>
@@ -39,7 +40,7 @@ const _generateAttributeDetails = (classes, attributeName, attributeValue) => {
3940
// List represented as an object
4041
return (
4142
<div key={attributeName} className={classes.attributeRowContainer}>
42-
<span className={classes.attributeLabel}>{attributeName}:</span>
43+
<span className={classes.attributeLabel}>{attributeLabel}:</span>
4344
<span className={classes.attributeValue}>{JSON.stringify(attributeValue)}</span>
4445
</div>
4546
);
@@ -48,20 +49,30 @@ const _generateAttributeDetails = (classes, attributeName, attributeValue) => {
4849

4950
const NodeData = (props) => {
5051
const classes = useStyles();
51-
const { data } = props;
52+
const { data, labels } = props;
5253
if (!data) {
5354
return 'No data to display for this node.';
5455
}
5556

5657
return (
5758
<div className={classes.nodeDetailsContainer}>
58-
{Object.keys(data).map((key) => _generateAttributeDetails(classes, key, data[key]))}
59+
{Object.keys(data).map((key) => _generateAttributeDetails(classes, labels, key, data[key]))}
5960
</div>
6061
);
6162
};
6263

6364
NodeData.propTypes = {
6465
data: PropTypes.object,
66+
labels: PropTypes.object,
67+
};
68+
69+
NodeData.defaultProps = {
70+
data: PropTypes.object,
71+
labels: {
72+
dictKey: 'Key',
73+
dictValue: 'Value',
74+
attributes: {},
75+
},
6576
};
6677

6778
export default NodeData;

0 commit comments

Comments
 (0)