Skip to content

Commit a37ec82

Browse files
committed
Add Component.normalizeElements() utility function
Ref #2
1 parent bba0932 commit a37ec82

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ The flat list of [Cytoscape elements](http://js.cytoscape.org/#notation/elements
5252
5353
N.b. arrays or objects should not be used in an `element`'s `data` or `scratch` fields, unless using a custom `diff()` prop.
5454
55+
In order to make it easier to support passing in `elements` JSON in the `elements: { nodes: [], edges: [] }` format, there is a static function `CytoscapeComponent.normalizeElements()`. E.g.:
56+
57+
```jsx
58+
<CytoscapeComponent
59+
elements={CytoscapeComponent.normalizeElements({
60+
nodes: [
61+
{ data: { id: 'one', label: 'Node 1' }, position: { x: 0, y: 0 } },
62+
{ data: { id: 'two', label: 'Node 2' }, position: { x: 100, y: 0 } }
63+
],
64+
edges: [
65+
{
66+
data: { source: 'one', target: 'two', label: 'Edge from Node1 to Node2' }
67+
}
68+
]
69+
})}
70+
/>
71+
```
72+
73+
Note that `CytoscapeComponent.normalizeElements()` is useful only for plain-JSON data, such as an export from Cytoscape.js or the Cytoscape desktop software. If you use [custom prop types](#custom-prop-types), such as Immutable, then you should flatten the elements yourself before passing the `elements` prop.
74+
5575
### `stylesheet`
5676
5777
The Cytoscape stylesheet as non-stringified JSON. N.b. the prop key is `stylesheet` rather than `style`, the key used by Cytoscape itself, so as to not conflict with the HTML [`style`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/style) attribute. E.g.:

src/component.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ export default class CytoscapeComponent extends React.Component {
1818
return defaults;
1919
}
2020

21+
static normalizeElements(elements) {
22+
const isArray = elements.length != null;
23+
24+
if (isArray) {
25+
return elements;
26+
} else {
27+
let { nodes, edges } = elements;
28+
29+
if (nodes == null) {
30+
nodes = [];
31+
}
32+
33+
if (edges == null) {
34+
edges = [];
35+
}
36+
37+
return nodes.concat(edges);
38+
}
39+
}
40+
2141
constructor(props) {
2242
super(props);
2343
}

test/component.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,5 +416,36 @@
416416
expect(a.hasClass('bar')).to.be.false;
417417
});
418418
});
419+
420+
it('converts non-array elements', function() {
421+
const elements = {
422+
nodes: [{ data: { id: 'n0' } }, { data: { id: 'n1' } }],
423+
edges: [{ data: { id: 'e', source: 'n0', target: 'n1' } }]
424+
};
425+
426+
return updateCyProps({
427+
elements: ReactCytoscape.normalizeElements(elements)
428+
}).then(() => {
429+
expect(cy.getElementById('n0').length).to.equal(1);
430+
expect(cy.getElementById('n1').length).to.equal(1);
431+
expect(cy.getElementById('e').length).to.equal(1);
432+
});
433+
});
434+
435+
it('maintains array elements', function() {
436+
const elements = [
437+
{ data: { id: 'n0' } },
438+
{ data: { id: 'n1' } },
439+
{ data: { id: 'e', source: 'n0', target: 'n1' } }
440+
];
441+
442+
return updateCyProps({
443+
elements: ReactCytoscape.normalizeElements(elements)
444+
}).then(() => {
445+
expect(cy.getElementById('n0').length).to.equal(1);
446+
expect(cy.getElementById('n1').length).to.equal(1);
447+
expect(cy.getElementById('e').length).to.equal(1);
448+
});
449+
});
419450
});
420451
})();

0 commit comments

Comments
 (0)