-
Notifications
You must be signed in to change notification settings - Fork 177
/
index-tests.tsx
82 lines (76 loc) · 2.29 KB
/
index-tests.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as React from 'react';
import Diagram, { useSchema } from '.';
import { Button } from 'beautiful-react-ui';
export const UncontrolledDiagram1 = () => {
// create diagrams schema
const [schema, { onChange }] = useSchema({
nodes: [
{ id: 'node-1', content: 'Node 1', coordinates: [250, 60] },
{ id: 'node-2', content: 'Node 2', coordinates: [100, 200] },
{ id: 'node-3', content: 'Node 3', coordinates: [250, 220] },
{ id: 'node-4', content: 'Node 4', coordinates: [400, 200] },
],
links: [
{ input: 'node-1', output: 'node-2' },
{ input: 'node-1', output: 'node-3' },
{ input: 'node-1', output: 'node-4' },
],
});
return (
<div style={{ height: '22.5rem' }}>
<Diagram schema={schema} onChange={onChange} />
</div>
);
};
export const UncontrolledDiagram2 = () => {
const [schema, { onChange, addNode, removeNode }] = useSchema({
nodes: [
{
id: 'node-1',
content: 'Node 1',
coordinates: [150, 60],
outputs: [{ id: 'port-1', alignment: 'right' }],
data: {
onDoubleClick: () => console.warn('hello'),
}
},
],
});
const addNewNode = () =>
addNode({
id: `node-${schema.nodes.length + 1}`,
content: `Node ${schema.nodes.length + 1}`,
coordinates: [
schema.nodes[schema.nodes.length - 1].coordinates[0] + 10,
schema.nodes[schema.nodes.length - 1].coordinates[1] + 20,
],
render: ({ content, data }) => (
<div
onDoubleClick={data?.onDoubleClick}
role='button'
style={{ padding: '15px', background: 'purple' }}
>
{content}
</div>
),
data: {
onDoubleClick: () => alert(`Schema length is: ${schema.nodes.length}`),
},
inputs: [{ id: `port-${schema.nodes.length + 1}` }],
});
const removeLast = () => {
const lastNode = schema.nodes[schema.nodes.length - 1];
removeNode(lastNode);
};
return (
<div style={{ height: '22.5rem' }}>
<Button color='primary' icon='plus' onClick={addNewNode}>
Add new node
</Button>
<Button color='secondary' icon='minus' onClick={removeLast}>
Remove last node
</Button>
<Diagram schema={schema} onChange={onChange} />
</div>
);
};