/
ListView.ts
49 lines (42 loc) · 1.17 KB
/
ListView.ts
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
import * as React from 'react';
import {
ListView as RListView,
ListViewProperties,
ListViewDataSource,
createElement,
} from 'react-native';
export interface Props<T> extends Partial<ListViewProperties> {
items: Array<T>;
}
export interface State {
dataSource: ListViewDataSource;
}
export class ListView extends React.Component<Props<any>, State> {
public static displayName = 'CycleListView';
private _listView: any;
constructor(props: Props<any>) {
super();
const dataSource = new RListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
});
this.state = {dataSource: dataSource.cloneWithRows(props.items)};
}
public componentWillReceiveProps({items}: any) {
if (items !== this.props.items) {
this.setState({dataSource: this.state.dataSource.cloneWithRows(items)});
}
}
public getScrollResponder() {
return this._listView.getScrollResponder();
}
public render() {
const {items, ...listViewProps} = this.props;
return createElement(RListView, {
ref: (listView: any) => {
this._listView = listView;
},
dataSource: this.state.dataSource,
...listViewProps,
}) as any;
}
}