1 | import { useCallback, useMemo, useRef } from 'react';
|
2 | import useEventCallback from './useEventCallback';
|
3 | import useMounted from './useMounted';
|
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 | export default function useFocusManager(opts) {
|
30 | const isMounted = useMounted();
|
31 | const lastFocused = useRef();
|
32 | const handle = useRef();
|
33 | const willHandle = useEventCallback(opts.willHandle);
|
34 | const didHandle = useEventCallback(opts.didHandle);
|
35 | const onChange = useEventCallback(opts.onChange);
|
36 | const isDisabled = useEventCallback(opts.isDisabled);
|
37 | const handleChange = useCallback((focused, event) => {
|
38 | if (focused !== lastFocused.current) {
|
39 | didHandle == null ? void 0 : didHandle(focused, event);
|
40 |
|
41 |
|
42 | if (isMounted() || !focused) {
|
43 | lastFocused.current = focused;
|
44 | onChange == null ? void 0 : onChange(focused, event);
|
45 | }
|
46 | }
|
47 | }, [isMounted, didHandle, onChange, lastFocused]);
|
48 | const handleFocusChange = useCallback((focused, event) => {
|
49 | if (isDisabled()) return;
|
50 | if (event && event.persist) event.persist();
|
51 | if ((willHandle == null ? void 0 : willHandle(focused, event)) === false) {
|
52 | return;
|
53 | }
|
54 | clearTimeout(handle.current);
|
55 | if (focused) {
|
56 | handleChange(focused, event);
|
57 | } else {
|
58 | handle.current = window.setTimeout(() => handleChange(focused, event));
|
59 | }
|
60 | }, [willHandle, handleChange]);
|
61 | return useMemo(() => ({
|
62 | onBlur: event => {
|
63 | handleFocusChange(false, event);
|
64 | },
|
65 | onFocus: event => {
|
66 | handleFocusChange(true, event);
|
67 | }
|
68 | }), [handleFocusChange]);
|
69 | } |
\ | No newline at end of file |