-
-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathuse-input.tsx
More file actions
41 lines (33 loc) · 754 Bytes
/
use-input.tsx
File metadata and controls
41 lines (33 loc) · 754 Bytes
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
import React from 'react';
import {render, useInput, useApp, Box, Text} from '../../src/index.js';
function Robot() {
const {exit} = useApp();
const [x, setX] = React.useState(1);
const [y, setY] = React.useState(1);
useInput((input, key) => {
if (input === 'q') {
exit();
}
if (key.leftArrow) {
setX(Math.max(1, x - 1));
}
if (key.rightArrow) {
setX(Math.min(20, x + 1));
}
if (key.upArrow) {
setY(Math.max(1, y - 1));
}
if (key.downArrow) {
setY(Math.min(10, y + 1));
}
});
return (
<Box flexDirection="column">
<Text>Use arrow keys to move the face. Press “q” to exit.</Text>
<Box height={12} paddingLeft={x} paddingTop={y}>
<Text>^_^</Text>
</Box>
</Box>
);
}
render(<Robot />);