-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfocusManager.js
53 lines (44 loc) · 1.3 KB
/
focusManager.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
import { unstable_batchedUpdates as batchedUpdates } from 'react-dom'
import createTimeoutManager from './timeoutManager'
import createMountManager from './mountManager'
export function callFocusEventHandler(inst, focused, e) {
let handler = inst.props[focused ? 'onFocus' : 'onBlur']
handler && handler(e)
}
export default function createFocusManager(
instance,
{
willHandle,
didHandle,
onChange,
isDisabled = () => !!instance.props.disabled,
} = {}
) {
let lastFocused
let timeouts = createTimeoutManager(instance)
let isMounted = createMountManager(instance)
function handleFocus(focused, event) {
if (event && event.persist) event.persist()
if (willHandle && willHandle(focused, event) === false) return
timeouts.set('focus', () => {
batchedUpdates(() => {
if (focused !== lastFocused) {
if (didHandle) didHandle.call(instance, focused, event)
// only fire a change when unmounted if its a blur
if (isMounted() || !focused) {
lastFocused = focused
onChange && onChange(focused, event)
}
}
})
})
}
return {
handleBlur(event) {
if (!isDisabled()) handleFocus(false, event)
},
handleFocus(event) {
if (!isDisabled()) handleFocus(true, event)
},
}
}