-
Notifications
You must be signed in to change notification settings - Fork 17
/
reversed-flat-list.js
67 lines (59 loc) · 1.57 KB
/
reversed-flat-list.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
import React from 'react';
import { FlatList, View, ScrollView, StyleSheet } from 'react-native';
/**
* A FlatList that is rendered in reverse.
* Useful for chats and whatnots
*/
export default class ReversedList extends React.Component {
constructor(props) {
super(props);
this._previousData = props.data;
this.state = {
data: [...props.data].reverse()
};
}
// when provided data array changes,
// update the internal reversed copy
componentWillReceiveProps({ data }) {
if (data !== this._previousData) {
this._previousData = data;
this.setState({ data: [...data].reverse() });
}
}
scrollToBottom() {
this.scrollToIndex({ index: 0 });
}
scrollToIndex(...args) {
if (this._listViewRef) {
this._listViewRef.scrollToIndex(...args);
}
}
// then backing scrollview is flipped to reverse the list
renderScrollComponent = ({ style, refreshing, ...props }) => (
<ScrollView style={[style, styles.flip]} {...props} />
);
// each row is flipped back to normal position
renderItem = props => (
<View style={styles.flip}>
{this.props.renderItem(props)}
</View>
);
render() {
const { renderItem, data, ...props } = this.props;
return (
<FlatList
{...props}
data={this.state.data}
renderItem={this.renderItem}
renderScrollComponent={this.renderScrollComponent}
ref={ref => (this._listViewRef = ref)}
/>
);
}
}
// flipping is done by scale transform
const styles = StyleSheet.create({
flip: {
transform: [{ scaleY: -1 }]
}
});