diff --git a/packages/native/src/createNavigationAwareScrollable.js b/packages/native/src/createNavigationAwareScrollable.js index 60df24c2..4fdc8283 100644 --- a/packages/native/src/createNavigationAwareScrollable.js +++ b/packages/native/src/createNavigationAwareScrollable.js @@ -3,91 +3,112 @@ import hoistStatics from 'hoist-non-react-statics'; import { withNavigation } from '@react-navigation/core'; export default function createNavigationAwareScrollable(Component: any) { - class ComponentWithNavigationScrolling extends React.PureComponent { + const ComponentWithNavigationScrolling = withNavigation( + class extends React.PureComponent { + static displayName = `withNavigationScrollable(${Component.displayName || + Component.name})`; + + _subscription: any; + + componentDidMount() { + this._subscription = this.props.navigation.addListener( + 'refocus', + () => { + const scrollableNode = this.getNode(); + if (this.props.navigation.isFocused() && scrollableNode !== null) { + if (scrollableNode.scrollToTop != null) { + scrollableNode.scrollToTop(); + } else if (scrollableNode.scrollTo != null) { + scrollableNode.scrollTo({ y: 0 }); + } + } + } + ); + } + + getNode() { + if (this._scrollRef === null) { + return null; + } + + if (this._scrollRef.getScrollResponder) { + return this._scrollRef.getScrollResponder(); + } else if (this._scrollRef.getNode) { + return this._scrollRef.getNode(); + } else { + return this._scrollRef; + } + } + + componentWillUnmount() { + if (this._subscription != null) { + this._subscription.remove(); + } + } + + render() { + return ( + { + this._scrollRef = view; + }} + {...this.props} + /> + ); + } + } + ); + + class NavigationAwareScrollable extends React.PureComponent { static displayName = `NavigationAwareScrollable(${Component.displayName || Component.name})`; - _subscription: any; - - componentDidMount() { - this._subscription = this.props.navigation.addListener('refocus', () => { - const scrollableNode = this.getNode(); - if (this.props.navigation.isFocused() && scrollableNode !== null) { - if (scrollableNode.scrollToTop != null) { - scrollableNode.scrollToTop(); - } else if (scrollableNode.scrollTo != null) { - scrollableNode.scrollTo({ y: 0 }); - } - } - }); - } + _captureRef = view => { + this._innerRef = view; + this.props.onRef && this.props.onRef(view); + }; setNativeProps = (...args) => { - return this.getNode().setNativeProps(...args); + return this._innerRef.getNode().setNativeProps(...args); }; getScrollResponder = (...args) => { - return this.getNode().getScrollResponder(...args); + return this._innerRef.getNode().getScrollResponder(...args); }; getScrollableNode = (...args) => { - return this.getNode().getScrollableNode(...args); + return this._innerRef.getNode().getScrollableNode(...args); }; getInnerViewNode = (...args) => { - return this.getNode().getInnerViewNode(...args); + return this._innerRef.getNode().getInnerViewNode(...args); }; scrollTo = (...args) => { - return this.getNode().scrollTo(...args); + return this._innerRef.getNode().scrollTo(...args); }; scrollToEnd = (...args) => { - return this.getNode().scrollToEnd(...args); + return this._innerRef.getNode().scrollToEnd(...args); }; scrollWithoutAnimationTo = (...args) => { - return this.getNode().scrollWithoutAnimationTo(...args); + return this._innerRef.getNode().scrollWithoutAnimationTo(...args); }; flashScrollIndicators = (...args) => { - return this.getNode().flashScrollIndicators(...args); + return this._innerRef.getNode().flashScrollIndicators(...args); }; - getNode() { - if (this._scrollRef === null) { - return null; - } - - if (this._scrollRef.getScrollResponder) { - return this._scrollRef.getScrollResponder(); - } else if (this._scrollRef.getNode) { - return this._scrollRef.getNode(); - } else { - return this._scrollRef; - } - } - - componentWillUnmount() { - if (this._subscription != null) { - this._subscription.remove(); - } - } - render() { return ( - { - this._scrollRef = view; - }} + ); } } - return hoistStatics( - withNavigation(ComponentWithNavigationScrolling), - Component - ); + return hoistStatics(NavigationAwareScrollable, Component); }