refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
74 lines (64 loc) • 2.1 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState, useRef } from 'react';
import { isFunction, NOOP } from 'tsfn';
export var mapKeyboardFocused = function mapKeyboardFocused(props) {
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isKeyboardFocused = _useState2[0],
setIsKeyboardFocused = _useState2[1];
var origOnFocusRef = useRef();
var origOnBlurRef = useRef();
var origOnPressInRef = useRef();
var origOnPressOutRef = useRef();
var isPressed = useRef(false);
var onPressInRef = useRef(NOOP);
var onPressOutRef = useRef(NOOP);
var onFocusRef = useRef(NOOP);
var onBlurRef = useRef(NOOP);
origOnFocusRef.current = props.onFocus;
origOnBlurRef.current = props.onBlur;
origOnPressInRef.current = props.onPressIn;
origOnPressOutRef.current = props.onPressOut;
if (onPressInRef.current === NOOP) {
onPressInRef.current = function () {
isPressed.current = true;
if (isFunction(origOnPressInRef.current)) {
origOnPressInRef.current();
}
};
}
if (onPressOutRef.current === NOOP) {
onPressOutRef.current = function () {
isPressed.current = false;
if (isFunction(origOnPressOutRef.current)) {
origOnPressOutRef.current();
}
};
}
if (onFocusRef.current === NOOP) {
onFocusRef.current = function () {
if (isPressed.current === false) {
setIsKeyboardFocused(true);
}
if (isFunction(origOnFocusRef.current)) {
origOnFocusRef.current();
}
};
}
if (onBlurRef.current === NOOP) {
onBlurRef.current = function () {
setIsKeyboardFocused(false);
if (isFunction(origOnBlurRef.current)) {
origOnBlurRef.current();
}
};
}
return Object.assign({}, props, {
isKeyboardFocused: isKeyboardFocused || Boolean(props.isKeyboardFocused),
onFocus: onFocusRef.current,
onBlur: onBlurRef.current,
onPressIn: onPressInRef.current,
onPressOut: onPressOutRef.current
});
};
//# sourceMappingURL=map-keyboard-focused.js.map