-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathkeypress.js
More file actions
63 lines (49 loc) · 1.33 KB
/
Copy pathkeypress.js
File metadata and controls
63 lines (49 loc) · 1.33 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
/* @flow */
import React from 'react';
import ExampleBox from '../ExampleBox';
import SyntaxHighlighter from 'react-syntax-highlighter/prism';
import { light } from 'react-syntax-highlighter/styles/prism';
type State = {
keyValue: ?string,
};
export default class Keypress extends React.Component<{||}, State> {
state: State = { keyValue: null };
render() {
return (
<ExampleBox>
<h2>Input onKeyPress example:</h2>
<p>
Press <code>s</code> in the following form component to toggle the
menu.
</p>
<input onKeyPress={this.handleKeyPress} />
{this.state.keyValue === 's' && (
<ol>
<li>hello</li>
<li>world</li>
</ol>
)}
<h3>Code:</h3>
<SyntaxHighlighter language="javascript" style={light}>
{codeString}
</SyntaxHighlighter>
</ExampleBox>
);
}
handleKeyPress = ({ key }: SyntheticKeyboardEvent<HTMLInputElement>) => {
const keyValue = this.state.keyValue === key ? null : key;
this.setState({ keyValue });
};
}
const codeString = `
state: State = { keyValue: null };
render() {
...
<input onKeyPress={this.handleKeyPress} />
...
}
handleKeyPress = ({ key }) => {
const keyValue = this.state.keyValue === key ? null : key;
this.setState({ keyValue });
};
`;