1 | import * as React from 'react';
|
2 | const TEXT_NAVIGATION_RESET_TIMEOUT = 500;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export default function useTextNavigation(callback) {
|
14 | const textCriteriaRef = React.useRef({
|
15 | searchString: '',
|
16 | lastTime: null
|
17 | });
|
18 | return React.useCallback(event => {
|
19 | if (event.key.length === 1 && event.key !== ' ') {
|
20 | const textCriteria = textCriteriaRef.current;
|
21 | const lowerKey = event.key.toLowerCase();
|
22 | const currentTime = performance.now();
|
23 | if (textCriteria.searchString.length > 0 && textCriteria.lastTime && currentTime - textCriteria.lastTime > TEXT_NAVIGATION_RESET_TIMEOUT) {
|
24 | textCriteria.searchString = lowerKey;
|
25 | } else if (textCriteria.searchString.length !== 1 || lowerKey !== textCriteria.searchString) {
|
26 |
|
27 | textCriteria.searchString += lowerKey;
|
28 | }
|
29 | textCriteria.lastTime = currentTime;
|
30 | callback(textCriteria.searchString, event);
|
31 | }
|
32 | }, [callback]);
|
33 | } |
\ | No newline at end of file |