1- // @flow weak
1+ // @flow
22import React , { Component } from 'react' ;
3+ import PropTypes from 'prop-types' ;
34import { View , Animated } from 'react-native' ;
4- import _ from 'lodash' ;
55
66type Props = {
77 onBeginHidden : Function ,
@@ -11,6 +11,7 @@ type Props = {
1111 onTouchTop : Function ,
1212 onTouchBottom : Function ,
1313 children ?: React$Node ,
14+ onLayout ?: Function ,
1415} ;
1516
1617type DefaultProps = {
@@ -56,7 +57,7 @@ class TriggeringView extends Component<Props, State> {
5657 onTouchBottom : ( ) => { } ,
5758 } ;
5859
59- constructor ( props ) {
60+ constructor ( props : Props ) {
6061 super ( props ) ;
6162 this . initialPageY = 0 ;
6263 }
@@ -68,19 +69,22 @@ class TriggeringView extends Component<Props, State> {
6869 this . listenerId = this . context . scrollY . addListener ( this . onScroll ) ;
6970 }
7071
71- componentWillReceiveProps ( nextProps , nextContext ) {
72+ componentWillReceiveProps ( nextProps : Props , nextContext : Context ) {
7273 if ( ! this . context . scrollY ) {
7374 return ;
7475 }
7576 this . context . scrollY . removeListener ( this . listenerId ) ;
7677 nextContext . scrollY . addListener ( this . onScroll ) ;
7778 }
7879
79- onRef = ref => {
80+ onRef = ( ref : any ) => {
8081 this . ref = ref ;
8182 } ;
8283
83- onLayout = e => {
84+ onLayout = ( e : * ) => {
85+ if ( this . props . onLayout ) {
86+ this . props . onLayout ( e ) ;
87+ }
8488 if ( ! this . ref ) {
8589 return ;
8690 }
@@ -91,44 +95,57 @@ class TriggeringView extends Component<Props, State> {
9195 } ) ;
9296 } ;
9397
94- onScroll = event => {
98+ onScroll = ( event : * ) => {
9599 if ( ! this . context . scrollPageY ) {
96100 return ;
97101 }
98102 const pageY = this . initialPageY - event . value ;
99103 this . triggerEvents ( this . context . scrollPageY , pageY , pageY + this . height ) ;
100104 } ;
101105
102- triggerEvents ( value , top , bottom ) {
106+ triggerEvents ( value : number , top : number , bottom : number ) {
103107 if ( ! this . state . touched && value >= top ) {
104- this . setState ( ( ) => ( { touched : true } ) ) ;
108+ this . setState ( { touched : true } ) ;
105109 this . props . onBeginHidden ( ) ;
106110 this . props . onTouchTop ( true ) ;
107111 } else if ( this . state . touched && value < top ) {
108- this . setState ( ( ) => ( { touched : false } ) ) ;
112+ this . setState ( { touched : false } ) ;
109113 this . props . onDisplay ( ) ;
110114 this . props . onTouchTop ( false ) ;
111115 }
112116
113117 if ( ! this . state . hidden && value >= bottom ) {
114- this . setState ( ( ) => ( { hidden : true } ) ) ;
118+ this . setState ( { hidden : true } ) ;
115119 this . props . onHide ( ) ;
116120 this . props . onTouchBottom ( true ) ;
117121 } else if ( this . state . hidden && value < bottom ) {
118- this . setState ( ( ) => ( { hidden : false } ) ) ;
122+ this . setState ( { hidden : false } ) ;
119123 this . props . onBeginDisplayed ( ) ;
120124 this . props . onTouchBottom ( false ) ;
121125 }
122126 }
123127
124128 render ( ) {
125- const viewProps = _ . omit ( this . props , _ . keys ( TriggeringView . propTypes ) ) ;
129+ const {
130+ onBeginHidden,
131+ onHide,
132+ onBeginDisplayed,
133+ onDisplay,
134+ onTouchTop,
135+ onTouchBottom,
136+ ...viewProps
137+ } = this . props ;
126138 return (
127- < View ref = { this . onRef } onLayout = { this . onLayout } collapsable = { false } { ...viewProps } >
139+ < View ref = { this . onRef } collapsable = { false } { ...viewProps } onLayout = { this . onLayout } >
128140 { this . props . children }
129141 </ View >
130142 ) ;
131143 }
132144}
133145
146+ TriggeringView . contextTypes = {
147+ scrollY : PropTypes . instanceOf ( Animated . Value ) ,
148+ scrollPageY : PropTypes . number ,
149+ } ;
150+
134151export default TriggeringView ;
0 commit comments