Skip to content

Commit 191b664

Browse files
authored
Refactor/sandbox improvements (#12)
* Sandbox general style and grid improvements * Add refresh button to sandbox * Add quick links * Add random yet limited number on links when adding new nodes in sandbox * Add check script to package.json
1 parent 204369a commit 191b664

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Daniel Caldas",
66
"license": "MIT",
77
"scripts": {
8+
"check": "npm run lint && npm run test",
89
"dev": "node_modules/.bin/webpack-dev-server -d --content-base sandbox --inline --hot --port 3002",
910
"dist": "node_modules/.bin/npm-run-all --parallel dist:*",
1011
"dist:rd3g": "rm -rf dist/ && webpack --config webpack.config.dist.js -p --display-modules",

sandbox/Sandbox.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,21 @@ export default class Sandbox extends React.Component {
7171
if (this.state.data.nodes && this.state.data.nodes.length) {
7272
const maxIndex = this.state.data.nodes.length - 1;
7373
const minIndex = 0;
74-
const i = Math.floor(Math.random() * (maxIndex - minIndex + 1) + minIndex);
75-
const id = this.state.data.nodes[i].id;
74+
let i = Math.floor(Math.random() * (maxIndex - minIndex + 1) + minIndex);
75+
let nLinks = Math.floor(Math.random() * (5 - minIndex + 1) + minIndex);
7676
const newNode = `Node ${this.state.data.nodes.length}`;
7777

7878
this.state.data.nodes.push({id: newNode});
79-
this.state.data.links.push({
80-
source: newNode,
81-
target: id
82-
});
79+
80+
while (this.state.data.nodes[i] && this.state.data.nodes[i].id && nLinks) {
81+
this.state.data.links.push({
82+
source: newNode,
83+
target: this.state.data.nodes[i].id
84+
});
85+
86+
i++;
87+
nLinks--;
88+
}
8389

8490
this.setState({
8591
data: this.state.data
@@ -136,6 +142,8 @@ export default class Sandbox extends React.Component {
136142
return {config, schemaPropsValues};
137143
}
138144

145+
refresh = () => location.reload();
146+
139147
refreshGraph = (data) => {
140148
const {config, schemaPropsValues} = this._buildGraphConfig(data);
141149

@@ -195,16 +203,27 @@ export default class Sandbox extends React.Component {
195203
return (
196204
<div className='container'>
197205
<div className='container__graph'>
198-
<button onClick={this.restartGraphSimulation} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>▶️</button>
199-
<button onClick={this.pauseGraphSimulation} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>⏸️</button>
200-
<button onClick={this.resetNodesPositions} className='btn btn-default' style={btnStyle} disabled={this.state.config.staticGraph}>Unstick nodes</button>
201-
<button onClick={this.onClickAddNode} className='btn btn-default' style={btnStyle}>+</button>
202-
<button onClick={this.onClickRemoveNode} className='btn btn-default' style={btnStyle}>-</button>
203-
<br/><b>Nodes: </b> {this.state.data.nodes.length}, <b>Links: </b> {this.state.data.links.length}
204-
<Graph ref='graph' {...graphProps}/>
206+
<button onClick={this.restartGraphSimulation} className='btn btn-default btn-margin-left' style={btnStyle} disabled={this.state.config.staticGraph}>▶️</button>
207+
<button onClick={this.pauseGraphSimulation} className='btn btn-default btn-margin-left' style={btnStyle} disabled={this.state.config.staticGraph}>⏸️</button>
208+
<button onClick={this.refresh} className='btn btn-default btn-margin-left' style={btnStyle}>🔄</button>
209+
<button onClick={this.resetNodesPositions} className='btn btn-default btn-margin-left' style={btnStyle} disabled={this.state.config.staticGraph}>Unstick nodes</button>
210+
<button onClick={this.onClickAddNode} className='btn btn-default btn-margin-left' style={btnStyle}>+</button>
211+
<button onClick={this.onClickRemoveNode} className='btn btn-default btn-margin-left' style={btnStyle}>-</button>
212+
<span className='container__graph-info'>
213+
<b>Nodes: </b> {this.state.data.nodes.length} | <b>Links: </b> {this.state.data.links.length}
214+
</span>
215+
<div className='container__graph-area'>
216+
<Graph ref='graph' {...graphProps}/>
217+
</div>
205218
</div>
206219
<div className='container__form'>
207-
<h4><a href="https://github.com/danielcaldas/react-d3-graph" target="_blank"><img width="40" height="40" src="https://a248.e.akamai.net/assets.github.com/images/icons/emoji/octocat.png"></img> react-d3-graph</a> configurations </h4>
220+
<h4>
221+
<a href="https://github.com/danielcaldas/react-d3-graph" target="_blank">react-d3-graph</a>
222+
</h4>
223+
<h4>
224+
<a href="https://danielcaldas.github.io/react-d3-graph/docs/index.html" target="_blank">docs</a>
225+
</h4>
226+
<h3>Configurations</h3>
208227
<Form className='form-wrapper'
209228
schema={this.state.schema}
210229
uiSchema={this.uiSchema}
@@ -220,7 +239,7 @@ export default class Sandbox extends React.Component {
220239
<JSONContainer data={this.state.generatedConfig} staticData={false} />
221240
</div>
222241
<div className='container__graph-data'>
223-
<h4>Initial Graph data</h4>
242+
<h4>Initial Graph Data</h4>
224243
<JSONContainer data={this.state.data} staticData={true}/>
225244
</div>
226245
</div>

sandbox/styles.css

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,54 @@
99
padding: 1em;
1010
}.container {
1111
display: grid;
12-
grid-template-columns: repeat(3, 2fr);
12+
width: 100%;
13+
grid-template-columns: 450px 1fr;
1314
grid-auto-rows: minmax(100px, auto);
1415
}
1516

1617
.container__graph {
1718
grid-column: 1 / 5;
1819
grid-row: 1 / 2;
20+
border: 1px solid black;
21+
}
22+
23+
.container__graph-info {
24+
margin-left: 12px;
25+
}
26+
27+
.btn-margin-left {
28+
margin-left: 4px;
29+
}
1930

20-
border: 1px dashed black;
31+
.container__graph-area {
32+
max-width: 800px;
33+
max-height: 400px;
34+
border: 1px dotted gray;
35+
margin-left: 80px;
36+
margin-top: 4px;
2137
}
2238

2339
.container__graph-data {
2440
grid-column: 1 / 2;
2541
grid-row: 2 / 3;
42+
margin-bottom: 4px;
2643
}
2744

2845
.container__graph-config {
29-
grid-column: 2 / 5;
46+
grid-column: 2 / 3;
3047
grid-row: 2 / 3;
48+
margin-bottom: 4px;
3149
}
3250

3351
.container__form {
3452
grid-column: 5/ 6;
3553
grid-row: 1 / 4;
54+
min-width: 400px;
3655
}
3756

3857
.form-wrapper {
3958
overflow-y: scroll;
40-
max-height: 600px;
59+
max-height: 70vh;
4160
}
4261

4362
.json-data-container {

0 commit comments

Comments
 (0)