refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
46 lines (37 loc) • 1.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mapPressed = void 0;
var _react = require("react");
var _tsfn = require("tsfn");
const mapPressed = props => {
const origOnPressInRef = (0, _react.useRef)();
const origOnPressOutRef = (0, _react.useRef)();
const [isPressed, setIsPressed] = (0, _react.useState)(false);
const onPressInRef = (0, _react.useRef)(_tsfn.NOOP);
const onPressOutRef = (0, _react.useRef)(_tsfn.NOOP);
origOnPressInRef.current = props.onPressIn;
origOnPressOutRef.current = props.onPressOut;
if (onPressInRef.current === _tsfn.NOOP) {
onPressInRef.current = () => {
setIsPressed(true);
if ((0, _tsfn.isFunction)(origOnPressInRef.current)) {
origOnPressInRef.current();
}
};
}
if (onPressOutRef.current === _tsfn.NOOP) {
onPressOutRef.current = () => {
setIsPressed(false);
if ((0, _tsfn.isFunction)(origOnPressOutRef.current)) {
origOnPressOutRef.current();
}
};
}
return { ...props,
isPressed: isPressed || Boolean(props.isPressed),
onPressIn: onPressInRef.current,
onPressOut: onPressOutRef.current
};
};
exports.mapPressed = mapPressed;