-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (143 loc) · 4.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { Dimensions, FlatList, StyleSheet, View } from 'react-native';
import { LayoutProvider } from 'recyclerlistview';
import DataProvider from 'recyclerlistview/dist/reactnative/core/dependencies/DataProvider';
import RecyclerListView from 'recyclerlistview/dist/reactnative/core/RecyclerListView';
import { IAlphabetListItem } from './types/index'
// TODO: move to props
const indexComponentWidth = 30;
const { width: viewportWidth, height: viewportHeight } = Dimensions.get('window');
class AlphabetizedRecyclerList extends Component {
dataProvider
layoutProvider
// TODO: maybe add a recyclerListViewProps & a flatListProps as prop-types
static propTypes = {
items: PropTypes.arrayOf(IAlphabetListItem),
sectionItems: PropTypes.arrayOf(IAlphabetListItem),
onCellSelect: PropTypes.func,
cellComponent: PropTypes.elementType,
sectionComponent: PropTypes.elementType,
indexComponent: PropTypes.elementType,
cellProps: PropTypes.object,
indexHeight: PropTypes.number,
cellHeight: PropTypes.number,
sectionHeight: PropTypes.number,
}
constructor(props) {
super(props);
this.recyclerListView = React.createRef()
this.dataProvider = new DataProvider((r1, r2) => {
return r1.type !== r2.type || r1.id !== r2.id;
});
}
componentWillMount() {
this.updateProviders(this.props)
}
componentWillReceiveProps(newProps) {
this.updateProviders(newProps)
}
updateProviders = (props) => {
const { items = [], cellHeight, sectionHeight } = props;
if (items.length > 0) {
this.layoutProvider = new LayoutProvider(
index => items[index].type,
(type, dim) => {
switch (type) {
case 'section':
dim.width = viewportWidth;
dim.height = sectionHeight;
break;
case 'item':
dim.width = viewportWidth;
dim.height = cellHeight;
break;
default:
dim.width = viewportWidth;
dim.height = cellHeight;
break;
}
},
);
this.dataProvider = this.dataProvider.cloneWithRows(items);
}
}
rowRenderer = (type, el) => {
switch (type) {
case 'section':
const SectionComponent = this.props.sectionComponent;
return <SectionComponent data={el.item.data} />;
case 'item':
const CellComponent = this.props.cellComponent;
const props = {
item: el.item,
onSelect: this.props.onCellSelect,
};
return <CellComponent {...this.props.cellProps} {...props} />;
default:
// TODO: throw an error if anything else
return null;
}
}
renderIndexComponent = el => {
// TODO: set some default values
const {data, index} = el.item;
const IndexComponent = this.props.indexComponent;
return (
<IndexComponent onPress={this.scrollToIndex} data={data} index={index} />
);
}
scrollToIndex = index => {
this.recyclerListView.current.scrollToIndex(index, true);
}
render() {
const { sectionItems, indexHeight } = this.props;
const numberOfLetters = sectionItems.length;
if(!numberOfLetters) {
// TODO: maybe throw some error or a default message
return null
}
return (
<View
style={[
{ height: numberOfLetters * indexHeight },
styles.container
]}>
<RecyclerListView
ref={this.recyclerListView}
nestedScrollEnabled={true}
style={{ flex: 1 }}
layoutProvider={this.layoutProvider}
dataProvider={this.dataProvider}
rowRenderer={this.rowRenderer}
showsVerticalScrollIndicator={false}
/>
<View style={styles.indexContainer}>
<FlatList
renderItem={this.renderIndexComponent}
nestedScrollEnabled={true}
data={sectionItems}
keyExtractor={(it, index) => index.toString()}
showsVerticalScrollIndicator={false}
/>
</View>
</View>
)
}
}
export default AlphabetizedRecyclerList;
const styles = StyleSheet.create({
container: {
minHeight: 200,
maxHeight: viewportHeight * 0.9,
paddingStart: 10,
paddingEnd: 10,
paddingTop: 10,
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
},
indexContainer: {
width: indexComponentWidth,
},
});