1 | import { useState } from 'react';
|
2 | import useFocusManagerBase from '@restart/hooks/useFocusManager';
|
3 | import useEventCallback from '@restart/hooks/useEventCallback';
|
4 | import useMounted from '@restart/hooks/useMounted';
|
5 | import matches from 'dom-helpers/matches';
|
6 |
|
7 | const isInDisabledFieldset = node => {
|
8 | return !!node && matches(node, 'fieldset[disabled] *');
|
9 | };
|
10 |
|
11 | export default function useFocusManager(ref, props = {}, opts = {}) {
|
12 | const isMounted = useMounted();
|
13 | const [focused, setFocus] = useState(false);
|
14 | const isDisabled = useEventCallback(() => props.disabled === true || isInDisabledFieldset(ref.current));
|
15 | const events = useFocusManagerBase(Object.assign({}, opts, {
|
16 | isDisabled,
|
17 | onChange: focused => {
|
18 | if (isMounted()) setFocus(focused);
|
19 | },
|
20 |
|
21 | didHandle(focused, event) {
|
22 | let handler = props[focused ? 'onFocus' : 'onBlur'];
|
23 | if (handler) handler(event);
|
24 |
|
25 | if (opts.didHandle && !event.isWidgetDefaultPrevented) opts.didHandle(focused, event);
|
26 | }
|
27 |
|
28 | }));
|
29 | return [events, focused, setFocus];
|
30 | } |
\ | No newline at end of file |