-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (95 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React from 'react';
import {
FlatList,
} from 'react-native';
import PropTypes from 'prop-types';
class SafelyNestedList extends React.Component {
static propTypes = {
ListComponent: PropTypes.shape({}).isRequired,
nestedScrollEnabled: PropTypes.bool,
onMomentumScrollEnd: PropTypes.func,
onScrollEndDrag: PropTypes.func,
}
static defaultProps = {
ListComponent: FlatList,
nestedScrollEnabled: false,
onMomentumScrollEnd: (e) => { },
onScrollEndDrag: (e) => { },
}
constructor(nextProps) {
super(nextProps);
this.state = {
nestedScrollEnabled: false,
};
this.__onMomentumScrollEnd = this.__onMomentumScrollEnd.bind(this);
this.__onScrollEndDrag = this.__onScrollEndDrag.bind(this);
this.__handleTransition = this.__handleTransition.bind(this);
}
componentWillUpdate(nextProps, nextState) {
const {
nestedScrollEnabled,
} = nextProps;
if (nestedScrollEnabled !== this.props.nestedScrollEnabled) {
this.setState(
{
nestedScrollEnabled,
},
);
}
}
__handleTransition(e, fn) {
const {
y,
} = e.nativeEvent.contentOffset;
const {
nestedScrollEnabled,
} = this.state;
if (nestedScrollEnabled && y === 0) {
this.setState(
{
nestedScrollEnabled: false,
},
() => fn(e),
);
}
}
__onMomentumScrollEnd(e) {
const {
onMomentumScrollEnd,
} = this.props;
return this.__handleTransition(
e,
onMomentumScrollEnd,
);
}
__onScrollEndDrag(e) {
const {
onScrollEndDrag,
} = this.props;
return this.__handleTransition(
e,
onScrollEndDrag,
);
}
render() {
const {
ListComponent: SomeListComponent,
onScrollEndDrag,
onMomentumScrollEnd,
...extraProps
} = this.props;
const {
nestedScrollEnabled,
...extraState
} = this.state;
return (
<SomeListComponent
{...extraProps}
onMomentumScrollEnd={this.__onMomentumScrollEnd}
onScrollEndDrag={this.__onScrollEndDrag}
nestedScrollEnabled={nestedScrollEnabled}
/>
);
}
}
export default SafelyNestedList;