generated from HarveyD/react-component-library
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseNext.stories.tsx
134 lines (116 loc) · 3.33 KB
/
useNext.stories.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { useState } from "react";
import useNext from "./useNext";
import { EventHandlers, TopologyData, NxTopologyConfig } from "./NextTypes";
export default {
title: "useNext",
};
const sampleTopology: TopologyData = {
nodes: [
{ name: "Router1", id: 1, type: "router" },
{ name: "Router2", id: 2, type: "router" },
],
links: [{ source: 1, target: 2 }],
};
const sampleConfig: NxTopologyConfig = {
autoLayout: true,
adaptive: true,
identityKey: "id",
};
export const Basic = () => {
const { NextUI } = useNext({
topologyData: sampleTopology,
topologyConfig: sampleConfig,
style: { height: "90vh", width: "65vw" },
});
return <>{NextUI}</>;
};
export const WithEventHandlers = () => {
let i = 10;
const sampleEvtHandlers: EventHandlers = {
clickLink: (sender, event) => {
// Display an alert to the user
alert(`You clicked a link with id ${event.id()}`);
},
pressA: (sender, event) => {
// Insert Data
sender.addNode({ id: i++ });
},
};
const { NextUI } = useNext({
topologyData: sampleTopology,
topologyConfig: sampleConfig,
eventHandlers: sampleEvtHandlers,
style: { height: "90vh", width: "65vw" },
});
return <>{NextUI}</>;
};
export const WithCustomFn = () => {
const afterLoad = (nxApp) => {
window.nx.define("testTooltipPolicy", nx.graphic.Topology.TooltipPolicy, {
properties: {
topology: {},
tooltipManager: {},
},
methods: {
init(args) {
this.sets(args);
this._tm = this.tooltipManager();
},
clickNode(node) {
// Overwrite click behavior: Do nothing.
// This prevents the popup from displaying in the Next container
},
clickLink(link) {
// Overwrite click behavior: Do nothing.
// This prevents the popup from displaying in the Next container
},
},
});
nxApp.tooltipManager().tooltipPolicyClass("testTooltipPolicy");
};
const { NextUI } = useNext({
topologyData: sampleTopology,
topologyConfig: sampleConfig,
callback: afterLoad,
style: { height: "90vh", width: "65vw" },
});
return <>{NextUI}</>;
};
export const withNxAppUsed = () => {
const [nodeColor, setNodeColor] = useState("blue");
const [topology, setTopology] = useState(sampleTopology);
const { NextUI, nxApp } = useNext({
topologyData: topology,
topologyConfig: sampleConfig,
});
let i = 1;
const clickHandlerAddNode = () => {
nxApp.insertData({ nodes: [{ id: 50 * i++ }] });
};
const clickHandlerAddLink = () => {
nxApp.insertData({ links: [{ source: 1, target: 2 }] });
};
const clickHandlerNewTopology = () => {
setTopology({
nodes: [{ id: 1 }, { id: 2 }, { id: 8 }],
links: [{ source: 1, target: 2 }],
});
};
const clickHandlerChangeColor = () => {
setNodeColor((nodeColor) => (nodeColor === "red" ? "blue" : "red"));
nxApp?.eachNode((node) => {
if (node.id() === 1) {
node.color(nodeColor);
}
});
};
return (
<>
{NextUI}
<button onClick={clickHandlerAddNode}>Add Node</button>
<button onClick={clickHandlerAddLink}>Add Link</button>
<button onClick={clickHandlerNewTopology}>New Topology</button>
<button onClick={clickHandlerChangeColor}>Change node color</button>
</>
);
};