-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
use-focus-with-id.tsx
58 lines (49 loc) · 975 Bytes
/
use-focus-with-id.tsx
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
import React from 'react';
import {
render,
Box,
Text,
useFocus,
useInput,
useFocusManager,
} from '../../src/index.js';
function 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>
);
}
type ItemProperties = {
readonly id: number;
readonly label: string;
};
function Item({label, id}: ItemProperties) {
const {isFocused} = useFocus({id});
return (
<Text>
{label} {isFocused && <Text color="green">(focused)</Text>}
</Text>
);
}
render(<Focus />);