UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

88 lines (71 loc) 2.66 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.mapKeyboardFocused = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = require("react"); var _tsfn = require("tsfn"); var mapKeyboardFocused = function mapKeyboardFocused(props) { var _useState = (0, _react.useState)(false), _useState2 = (0, _slicedToArray2.default)(_useState, 2), isKeyboardFocused = _useState2[0], setIsKeyboardFocused = _useState2[1]; var origOnFocusRef = (0, _react.useRef)(); var origOnBlurRef = (0, _react.useRef)(); var origOnPressInRef = (0, _react.useRef)(); var origOnPressOutRef = (0, _react.useRef)(); var isPressed = (0, _react.useRef)(false); var onPressInRef = (0, _react.useRef)(_tsfn.NOOP); var onPressOutRef = (0, _react.useRef)(_tsfn.NOOP); var onFocusRef = (0, _react.useRef)(_tsfn.NOOP); var onBlurRef = (0, _react.useRef)(_tsfn.NOOP); origOnFocusRef.current = props.onFocus; origOnBlurRef.current = props.onBlur; origOnPressInRef.current = props.onPressIn; origOnPressOutRef.current = props.onPressOut; if (onPressInRef.current === _tsfn.NOOP) { onPressInRef.current = function () { isPressed.current = true; if ((0, _tsfn.isFunction)(origOnPressInRef.current)) { origOnPressInRef.current(); } }; } if (onPressOutRef.current === _tsfn.NOOP) { onPressOutRef.current = function () { isPressed.current = false; if ((0, _tsfn.isFunction)(origOnPressOutRef.current)) { origOnPressOutRef.current(); } }; } if (onFocusRef.current === _tsfn.NOOP) { onFocusRef.current = function () { if (isPressed.current === false) { setIsKeyboardFocused(true); } if ((0, _tsfn.isFunction)(origOnFocusRef.current)) { origOnFocusRef.current(); } }; } if (onBlurRef.current === _tsfn.NOOP) { onBlurRef.current = function () { setIsKeyboardFocused(false); if ((0, _tsfn.isFunction)(origOnBlurRef.current)) { origOnBlurRef.current(); } }; } return (0, _extends2.default)({}, props, { isKeyboardFocused: isKeyboardFocused || Boolean(props.isKeyboardFocused), onFocus: onFocusRef.current, onBlur: onBlurRef.current, onPressIn: onPressInRef.current, onPressOut: onPressOutRef.current }); }; exports.mapKeyboardFocused = mapKeyboardFocused; //# sourceMappingURL=map-keyboard-focused.js.map