UNPKG

react-native-modal-popover

Version:
97 lines 3.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PopoverTouchable = void 0; const PropTypes = __importStar(require("prop-types")); const React = __importStar(require("react")); const react_native_1 = require("react-native"); class PopoverTouchable extends React.PureComponent { constructor(props) { super(props); this.onOrientationChange = () => { if (this.state.showPopover) { requestAnimationFrame(this.onPress); } }; this.touchable = null; this.setRef = (ref) => { this.touchable = ref; }; this.onPress = () => { const handle = (0, react_native_1.findNodeHandle)(this.touchable); if (handle) { react_native_1.NativeModules.UIManager.measure(handle, this.onTouchableMeasured); } }; this.onTouchableMeasured = (x0, y0, width, height, x, y) => { this.setState({ showPopover: true, popoverAnchor: { x, y, width, height }, }, () => { if (this.props.onPopoverDisplayed) { this.props.onPopoverDisplayed(); } }); }; this.onClosePopover = () => this.setState({ showPopover: false }); this.state = { showPopover: false, popoverAnchor: { x: 0, y: 0, width: 0, height: 0 }, }; console.warn('PopoverTouchable is deprecated, please use PopoverController instead'); } componentDidMount() { this.dimensionsSub = react_native_1.Dimensions.addEventListener('change', this.onOrientationChange); } componentWillUnmount() { var _a; (_a = this.dimensionsSub) === null || _a === void 0 ? void 0 : _a.remove(); } render() { const children = React.Children.toArray(this.props.children); if (children.length !== 2 || children[1] instanceof Number || children[1] instanceof String || children[1].type.displayName !== 'Popover') { throw new Error('Popover touchable must have two children and the second one must be Popover'); } return (<React.Fragment> {React.cloneElement(children[0], { ref: this.setRef, onPress: this.onPress, })} {React.cloneElement(children[1], { visible: this.state.showPopover, onClose: this.onClosePopover, fromRect: this.state.popoverAnchor, })} </React.Fragment>); } } exports.PopoverTouchable = PopoverTouchable; PopoverTouchable.propTypes = { onPopoverDisplayed: PropTypes.func, }; //# sourceMappingURL=PopoverTouchable.js.map