Skip to content

Commit ec47b8c

Browse files
committed
UI enhancements, Add keys to react component lists
1 parent 9ea48fb commit ec47b8c

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/Actions.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ class Actions extends Component {
1818
return (
1919
<form className="actions">
2020
<div className="main-menu">
21-
<button className="btn btn-primary" onClick={resetMaze}>
21+
<button className={`btn ${rows.length ? 'btn-danger' : 'btn-primary'}`}
22+
onClick={resetMaze}>
2223
{rows.length ? 'Clear Maze' : 'Generate Maze'}
2324
</button>
2425
</div>
2526

2627
{ rows.length ? null :
2728
<div>
2829
<div className="row">
29-
<div className="col-12 col-sm-6 col-lg-3">
30+
<div className="col-12 col-sm-6 col-lg-4 col-xl-3">
3031
<div className="form-group">
3132
<label htmlFor="widthInput">Width:</label>
3233

@@ -44,7 +45,7 @@ class Actions extends Component {
4445
</div>
4546
</div>
4647

47-
<div className="col-12 col-sm-6 col-lg-3">
48+
<div className="col-12 col-sm-6 col-lg-4 col-xl-3">
4849
<div className="form-group">
4950
<label htmlFor="heightInput">Height:</label>
5051

@@ -64,7 +65,7 @@ class Actions extends Component {
6465
</div>
6566

6667
<div className="row">
67-
<div className="col-12 col-sm-6 col-lg-3">
68+
<div className="col-12 col-sm-6 col-lg-4 col-xl-3">
6869
<div className="form-group">
6970
<label htmlFor="mergeChanceInput">
7071
Merge chance (min 0 max 1):
@@ -84,7 +85,7 @@ class Actions extends Component {
8485
</small>
8586
</div>
8687
</div>
87-
<div className="col-12 col-sm-6 col-lg-3">
88+
<div className="col-12 col-sm-6 col-lg-4 col-xl-3">
8889
<div className="form-group">
8990
<label htmlFor="mergeChanceInput">
9091
Maze generation speed (ms):

src/Row.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Row extends Component {
6262
}
6363
}
6464

65-
newCell = <Cell setID={setID} walls={walls}/>;
65+
newCell = <Cell setID={setID} walls={walls} key={this.currentCell}/>;
6666

6767
this.setState({
6868
cells: [
@@ -84,13 +84,9 @@ class Row extends Component {
8484
getCellsAndHighlightCurrent() {
8585
return this.state.cells.map((cell, i) => {
8686
if (i !== this.currentCell) {
87-
return <Cell setID={cell.props.setID}
88-
active={false}
89-
walls={cell.props.walls}/>
87+
return <Cell {...cell.props} key={i} active={false}/>
9088
}
91-
return <Cell setID={cell.props.setID}
92-
active={true}
93-
walls={cell.props.walls}/>
89+
return <Cell {...cell.props} key={i} active={true}/>
9490
});
9591
}
9692

@@ -105,9 +101,7 @@ class Row extends Component {
105101
return cell;
106102
}
107103

108-
return <Cell setID={currentCellSetId}
109-
active={cell.props.active}
110-
walls={cell.props.walls}/>
104+
return <Cell {...cell.props} key={i} setID={currentCellSetId}/>
111105
});
112106
}
113107

@@ -117,10 +111,7 @@ class Row extends Component {
117111
clearInterval(this.timerID);
118112
// remove the active style
119113
const cells = this.state.cells.map((cell, i) => {
120-
121-
return <Cell setID={cell.props.setID}
122-
active={false}
123-
walls={cell.props.walls}/>
114+
return <Cell {...cell.props} key={i} active={false}/>
124115
});
125116
this.setState({ cells });
126117
// tell the Maze that the row is done, so we can pass
@@ -203,7 +194,8 @@ class Row extends Component {
203194
bottom: false,
204195
}
205196

206-
return <Cell setID={cell.props.setID}
197+
return <Cell {...cell.props}
198+
key={i}
207199
active={i === this.props.width - 1 ? true : false}
208200
walls={walls}/>
209201
});

src/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Maze extends Component {
3030
this.setState({
3131
rows: [
3232
<Row index={0}
33+
key={0}
3334
width={this.state.width}
3435
chanceToJoin={this.state.chanceToJoin}
3536
speed={this.state.speed}
@@ -53,6 +54,7 @@ class Maze extends Component {
5354
rows: [
5455
...this.state.rows,
5556
<Row index={index + 1}
57+
key={index + 1}
5658
width={this.state.width}
5759
chanceToJoin={this.state.chanceToJoin}
5860
speed={this.state.speed}
@@ -70,7 +72,8 @@ class Maze extends Component {
7072
// to spread them here again
7173
...this.state.rows,
7274
// the new row
73-
<Row index={index + 1}
75+
<Row index={index + 1}
76+
key={index + 1}
7477
width={this.state.width}
7578
chanceToJoin={this.state.chanceToJoin}
7679
speed={this.state.speed}
@@ -109,9 +112,14 @@ class Maze extends Component {
109112
}
110113

111114
isFormValid() {
112-
const { width, height, chanceToJoin } = this.state;
115+
const { width, height, chanceToJoin, speed } = this.state;
113116

114-
return width && height && chanceToJoin >= 0 && chanceToJoin <= 1;
117+
return (
118+
width &&
119+
height &&
120+
chanceToJoin !== '' && chanceToJoin >= 0 && chanceToJoin <= 1 &&
121+
speed !== '' && speed >= 0
122+
);
115123
}
116124

117125
resetMaze(e) {

0 commit comments

Comments
 (0)