11// @flow weak
22import React , { Component } from 'react' ;
3- import PropTypes from 'prop-types' ;
43import { View , Animated } from 'react-native' ;
54import _ from 'lodash' ;
65
7- class TriggeringView extends Component < * , * , * > {
6+ type Props = {
7+ onBeginHidden : Function ,
8+ onHide : Function ,
9+ onBeginDisplayed : Function ,
10+ onDisplay : Function ,
11+ onTouchTop : Function ,
12+ onTouchBottom : Function ,
13+ children ?: React$Node ,
14+ } ;
15+
16+ type DefaultProps = {
17+ onBeginHidden : Function ,
18+ onHide : Function ,
19+ onBeginDisplayed : Function ,
20+ onDisplay : Function ,
21+ onTouchTop : Function ,
22+ onTouchBottom : Function ,
23+ } ;
24+
25+ type State = {
26+ touched : boolean ,
27+ hidden : boolean ,
28+ } ;
29+
30+ type Context = {
31+ scrollPageY ?: number ,
32+ scrollY : Animated . Value ,
33+ } ;
34+
35+ class TriggeringView extends Component < Props , State > {
836 initialPageY : number ;
9- listenerId : number ;
10- ref : * ;
37+ listenerId : string ;
38+ ref : ? any ; // @see https://github.com/facebook/react-native/issues/15955
1139 height : number ;
40+ context : Context ;
1241
1342 onScroll : Function ;
1443 onRef : Function ;
1544 onLayout : Function ;
45+ state : State = {
46+ touched : false ,
47+ hidden : false ,
48+ } ;
49+
50+ static defaultProps : DefaultProps = {
51+ onBeginHidden : ( ) => { } ,
52+ onHide : ( ) => { } ,
53+ onBeginDisplayed : ( ) => { } ,
54+ onDisplay : ( ) => { } ,
55+ onTouchTop : ( ) => { } ,
56+ onTouchBottom : ( ) => { } ,
57+ } ;
1658
1759 constructor ( props ) {
1860 super ( props ) ;
19- this . state = {
20- touched : false ,
21- hidden : false ,
22- } ;
2361 this . initialPageY = 0 ;
24- this . onScroll = this . _onScroll . bind ( this ) ;
25- this . onRef = this . _onRef . bind ( this ) ;
26- this . onLayout = this . _onLayout . bind ( this ) ;
2762 }
2863
2964 componentWillMount ( ) {
@@ -41,40 +76,46 @@ class TriggeringView extends Component<*, *, *> {
4176 nextContext . scrollY . addListener ( this . onScroll ) ;
4277 }
4378
44- _onRef ( ref ) {
79+ onRef = ref => {
4580 this . ref = ref ;
46- }
81+ } ;
4782
48- _onLayout ( e ) {
83+ onLayout = e => {
84+ if ( ! this . ref ) {
85+ return ;
86+ }
4987 const layout = e . nativeEvent . layout ;
5088 this . height = layout . height ;
5189 this . ref . measure ( ( x , y , width , height , pageX , pageY ) => {
5290 this . initialPageY = pageY ;
5391 } ) ;
54- }
92+ } ;
5593
56- _onScroll ( event ) {
94+ onScroll = event => {
95+ if ( ! this . context . scrollPageY ) {
96+ return ;
97+ }
5798 const pageY = this . initialPageY - event . value ;
5899 this . triggerEvents ( this . context . scrollPageY , pageY , pageY + this . height ) ;
59- }
100+ } ;
60101
61102 triggerEvents ( value , top , bottom ) {
62103 if ( ! this . state . touched && value >= top ) {
63- this . setState ( { touched : true } ) ;
104+ this . setState ( ( ) => ( { touched : true } ) ) ;
64105 this . props . onBeginHidden ( ) ;
65106 this . props . onTouchTop ( true ) ;
66107 } else if ( this . state . touched && value < top ) {
67- this . setState ( { touched : false } ) ;
108+ this . setState ( ( ) => ( { touched : false } ) ) ;
68109 this . props . onDisplay ( ) ;
69110 this . props . onTouchTop ( false ) ;
70111 }
71112
72113 if ( ! this . state . hidden && value >= bottom ) {
73- this . setState ( { hidden : true } ) ;
114+ this . setState ( ( ) => ( { hidden : true } ) ) ;
74115 this . props . onHide ( ) ;
75116 this . props . onTouchBottom ( true ) ;
76117 } else if ( this . state . hidden && value < bottom ) {
77- this . setState ( { hidden : false } ) ;
118+ this . setState ( ( ) => ( { hidden : false } ) ) ;
78119 this . props . onBeginDisplayed ( ) ;
79120 this . props . onTouchBottom ( false ) ;
80121 }
@@ -89,27 +130,5 @@ class TriggeringView extends Component<*, *, *> {
89130 ) ;
90131 }
91132}
92- TriggeringView . propTypes = {
93- onBeginHidden : PropTypes . func ,
94- onHide : PropTypes . func ,
95- onBeginDisplayed : PropTypes . func ,
96- onDisplay : PropTypes . func ,
97- onTouchTop : PropTypes . func ,
98- onTouchBottom : PropTypes . func ,
99- } ;
100-
101- TriggeringView . defaultProps = {
102- onBeginHidden : ( ) => { } ,
103- onHide : ( ) => { } ,
104- onBeginDisplayed : ( ) => { } ,
105- onDisplay : ( ) => { } ,
106- onTouchTop : ( ) => { } ,
107- onTouchBottom : ( ) => { } ,
108- } ;
109-
110- TriggeringView . contextTypes = {
111- scrollY : PropTypes . instanceOf ( Animated . Value ) ,
112- scrollPageY : PropTypes . number ,
113- } ;
114133
115134export default TriggeringView ;
0 commit comments