-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcode-explore.js
More file actions
65 lines (55 loc) · 1.37 KB
/
code-explore.js
File metadata and controls
65 lines (55 loc) · 1.37 KB
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
/* @flow */
import React from 'react';
import ExampleBox from '../ExampleBox';
type code = {| code: string, value: string |};
type State = {
codes: code[],
};
export default class CodeExplore extends React.Component<{||}, State> {
state: State = { codes: [] };
render() {
const { codes } = this.state;
const undefCodes: Array<code | void> = [undefined, undefined, undefined];
const elements = undefCodes
.concat(codes)
.reverse()
.slice(0, 3)
.map((code, i) => {
if (code === undefined) {
return <li key={i}>...</li>;
}
return (
<li key={i}>
{code.code} => {code.value}
</li>
);
});
return (
<ExampleBox>
<h2>
Input
<code>KeyboardEvent</code>
<code>.code</code>
and <code>.value</code>
explore:
</h2>
<input onKeyPress={this.handleKeyPress} />
<p>Last 3 values (code => value):</p>
<ol>{elements}</ol>
</ExampleBox>
);
}
handleKeyPress = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
const { codes } = this.state;
codes.push({
// $FlowFixMe
code: e.nativeEvent.code,
value: e.key,
});
if (codes.length > 3) {
codes.shift();
}
this.setState({ codes });
};
}