forked from vadimdemedes/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-focus-with-id.js
54 lines (46 loc) · 902 Bytes
/
use-focus-with-id.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
'use strict';
const React = require('react');
const {
render,
Box,
Text,
useFocus,
useInput,
useFocusManager
} = require('../..');
const Focus = () => {
const {focus} = useFocusManager();
useInput(input => {
if (input === '1') {
focus('1');
}
if (input === '2') {
focus('2');
}
if (input === '3') {
focus('3');
}
});
return (
<Box flexDirection="column" padding={1}>
<Box marginBottom={1}>
<Text>
Press Tab to focus next element, Shift+Tab to focus previous element,
Esc to reset focus.
</Text>
</Box>
<Item id="1" label="Press 1 to focus" />
<Item id="2" label="Press 2 to focus" />
<Item id="3" label="Press 3 to focus" />
</Box>
);
};
const Item = ({label, id}) => {
const {isFocused} = useFocus({id});
return (
<Text>
{label} {isFocused && <Text color="green">(focused)</Text>}
</Text>
);
};
render(<Focus />);