-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.js
83 lines (71 loc) · 2.41 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
import React, {
forwardRef,
} from 'react';
import PropTypes from 'prop-types';
import {FlatList, StyleSheet} from 'react-native';
import _ from 'underscore';
import BaseInvertedFlatList from './BaseInvertedFlatList';
import styles from '../../styles/styles';
const propTypes = {
/** Passed via forwardRef so we can access the FlatList ref */
innerRef: PropTypes.shape({
current: PropTypes.instanceOf(FlatList),
}).isRequired,
/** Any additional styles to apply */
// eslint-disable-next-line react/forbid-prop-types
contentContainerStyle: PropTypes.any,
};
// This is adapted from https://codesandbox.io/s/react-native-dsyse
// It's a HACK alert since FlatList has inverted scrolling on web
class InvertedFlatList extends React.Component {
constructor(props) {
super(props);
this.invertedWheelEvent = this.invertedWheelEvent.bind(this);
this.list = undefined;
}
componentDidMount() {
if (!_.isFunction(this.props.innerRef)) {
// eslint-disable-next-line no-param-reassign
this.props.innerRef.current = this.list;
} else {
this.props.innerRef(this.list);
}
if (this.list) {
this.list
.getScrollableNode()
.addEventListener('wheel', this.invertedWheelEvent);
this.list.setNativeProps({
style: {
transform: 'translate3d(0,0,0) scaleY(-1)',
},
});
}
}
componentWillUnmount() {
this.list.getScrollableNode()
.removeEventListener('wheel', this.invertedWheelEvent);
}
invertedWheelEvent(e) {
this.list.getScrollableNode().scrollTop -= e.deltaY;
e.preventDefault();
}
render() {
return (
<BaseInvertedFlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
ref={el => this.list = el}
shouldMeasureItems
contentContainerStyle={StyleSheet.compose(this.props.contentContainerStyle, styles.justifyContentEnd)}
/>
);
}
}
InvertedFlatList.propTypes = propTypes;
InvertedFlatList.defaultProps = {
contentContainerStyle: {},
};
export default forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<InvertedFlatList {...props} innerRef={ref} />
));