-
Notifications
You must be signed in to change notification settings - Fork 1
/
gridControl.js
128 lines (117 loc) · 4.6 KB
/
gridControl.js
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
import React, { Component } from 'react';
import CellControl from './cellControl';
export default class gridControl extends Component {
constructor(props) {
super(props);
this.state = {
board: this.props.value || [
['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
['', '', '', '']
]
};
this.onChange = this.onChange.bind(this);
};
componentDidMount() {
this.cell00.focus();
}
onChange(e) {
// Move to next cell on right, or down a row and to the left-most cell.
var x = e.x;
var y = e.y;
// Update board state.
var state = this.state;
state['board'][y] = state['board'][y] || [];
state['board'][y][x] = e.value;
this.setState(state, function() {
// Notify parent event handler.
if (this.props.onChange) {
this.props.onChange(this.state);
}
});
// Focus on next control.
if (x < 3) {
// Focus right.
x++;
}
else {
// Move down a row and wrap to left.
x = 0;
if (y < 3) {
y++;
}
else {
// Wrap back to top-left.
y = 0;
}
}
this['cell' + x + y].focus();
};
render() {
return (
<div className="grid-control">
<table>
<tbody>
<tr>
<td>
<CellControl x="0" y="0" value={this.state.board[0][0]} onChange={this.onChange} ref={(input) => { this.cell00 = input }}></CellControl>
</td>
<td>
<CellControl x="1" y="0" value={this.state.board[0][1]} onChange={this.onChange} ref={(input) => { this.cell10 = input }}></CellControl>
</td>
<td>
<CellControl x="2" y="0" value={this.state.board[0][2]} onChange={this.onChange} ref={(input) => { this.cell20 = input }}></CellControl>
</td>
<td>
<CellControl x="3" y="0" value={this.state.board[0][3]} onChange={this.onChange} ref={(input) => { this.cell30 = input }}></CellControl>
</td>
</tr>
<tr>
<td>
<CellControl x="0" y="1" value={this.state.board[1][0]} onChange={this.onChange} ref={(input) => { this.cell01 = input }}></CellControl>
</td>
<td>
<CellControl x="1" y="1" value={this.state.board[1][1]} onChange={this.onChange} ref={(input) => { this.cell11 = input }}></CellControl>
</td>
<td>
<CellControl x="2" y="1" value={this.state.board[1][2]} onChange={this.onChange} ref={(input) => { this.cell21 = input }}></CellControl>
</td>
<td>
<CellControl x="3" y="1" value={this.state.board[1][3]} onChange={this.onChange} ref={(input) => { this.cell31 = input }}></CellControl>
</td>
</tr>
<tr>
<td>
<CellControl x="0" y="2" value={this.state.board[2][0]} onChange={this.onChange} ref={(input) => { this.cell02 = input }}></CellControl>
</td>
<td>
<CellControl x="1" y="2" value={this.state.board[2][1]} onChange={this.onChange} ref={(input) => { this.cell12 = input }}></CellControl>
</td>
<td>
<CellControl x="2" y="2" value={this.state.board[2][2]} onChange={this.onChange} ref={(input) => { this.cell22 = input }}></CellControl>
</td>
<td>
<CellControl x="3" y="2" value={this.state.board[2][3]} onChange={this.onChange} ref={(input) => { this.cell32 = input }}></CellControl>
</td>
</tr>
<tr>
<td>
<CellControl x="0" y="3" value={this.state.board[3][0]} onChange={this.onChange} ref={(input) => { this.cell03 = input }}></CellControl>
</td>
<td>
<CellControl x="1" y="3" value={this.state.board[3][1]} onChange={this.onChange} ref={(input) => { this.cell13 = input }}></CellControl>
</td>
<td>
<CellControl x="2" y="3" value={this.state.board[3][2]} onChange={this.onChange} ref={(input) => { this.cell23 = input }}></CellControl>
</td>
<td>
<CellControl x="3" y="3" value={this.state.board[3][3]} onChange={this.onChange} ref={(input) => { this.cell33 = input }}></CellControl>
</td>
</tr>
</tbody>
</table>
</div>
);
}
}