forked from bouguerra-khalil/react-table-drag-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
171 lines (163 loc) · 4.62 KB
/
demo.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React from "react";
import ReactDOM from "react-dom";
import equal from "deep-is";
import TableDragSelect from "./index.js";
import "./style.css"; // TableDragSelect stylesheet
import "./demo.css"; // Demo stylesheet
class App extends React.Component {
state = {
cells: [
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false]
]
};
render = () =>
<div>
<h1>
<a href="https://github.com/mcjohnalds/react-table-drag-select">
<code>react-table-drag-select</code>
</a>{" "}
demo
</h1>
<h2>Timetable</h2>
<div className="table-container">
<TableDragSelect
value={this.state.cells}
maxRows={3}
maxColumns={3}
onChange={this.handleChange}
onSelectionStart={event => console.log("start", event)}
onInput={event => console.log("event", event)}
>
<tr>
<td disabled />
<td disabled>Monday</td>
<td disabled>Tuesday</td>
<td disabled>Wednesday</td>
<td disabled>Thursday</td>
<td disabled>Friday</td>
<td disabled>Saturday</td>
</tr>
<tr>
<td disabled>10:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>11:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>12:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>13:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>14:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>15:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
<tr>
<td disabled>16:00</td>
<td />
<td />
<td />
<td />
<td />
<td>overtime</td>
</tr>
</TableDragSelect>
</div>
<button onClick={this.handleReset}>Reset</button>
<h2>
<code>{"onChange={cells => ...}"}</code> callback
</h2>
<pre ref="output">
cells = {stringifyBoolMatrix(this.state.cells)}
</pre>
<h2>Javascript</h2>
<h2>Optional styling</h2>
<p>
This isn't required, but changing the colors can really spruce things
up.
</p>
<h2>Resulting DOM</h2>
</div>;
handleChange = cells => {
if (!equal(this.state.cells, cells)) {
this.restartFlashAnimation();
this.setState({ cells });
}
};
handleReset = () => {
const cells = [
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false],
[false, false, false, false, false, false, false]
];
if (!equal(this.state.cells, cells)) {
this.restartFlashAnimation();
this.setState({ cells });
}
};
restartFlashAnimation = () => {
this.refs.output.classList.remove("flash");
setTimeout(() => this.refs.output.classList.add("flash"), 50);
};
}
// Takes a 2D array of booleans and returns a pretty-printed string
const stringifyBoolMatrix = matrix => {
const row2Str = row => {
return row.map(cell => (cell ? " true" : "false")).join(", ");
};
return "[\n [" + matrix.map(row2Str).join("],\n [") + "]\n]";
};
const div = document.createElement("div");
document.body.appendChild(div);
ReactDOM.render(<App />, div);