-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathImmutableVirtualizedList.js
More file actions
190 lines (157 loc) · 6.09 KB
/
ImmutableVirtualizedList.js
File metadata and controls
190 lines (157 loc) · 6.09 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import { Text, VirtualizedList } from 'react-native';
import styles from '../styles';
import utils from '../utils';
import { EmptyVirtualizedList } from './EmptyVirtualizedList';
/**
* A VirtualizedList capable of displaying {@link https://facebook.github.io/immutable-js/ Immutable} data
* out of the box.
*/
// eslint-disable-next-line react/prefer-stateless-function
class ImmutableVirtualizedList extends PureComponent {
static propTypes = {
// Pass through any props that VirtualizedList would normally take.
...VirtualizedList.propTypes,
/**
* The immutable data to be rendered in a VirtualizedList.
*/
// eslint-disable-next-line consistent-return
immutableData: (props, propName, componentName) => {
// Note: It's not enough to simply validate PropTypes.instanceOf(Immutable.Iterable),
// because different imports of Immutable.js across files have different class prototypes.
if (!utils.isImmutableIterable(props[propName])) {
return new Error(`Invalid prop ${propName} supplied to ${componentName}: Must be instance of Immutable.Iterable.`);
}
},
/**
* A function that returns some {@link PropTypes.element}
* to be rendered as a section header. It will be passed a List
* or Map of the section's items, and the section key.
*/
renderSectionHeader: PropTypes.func,
/**
* A plain string, or a function that returns some {@link PropTypes.element}
* to be rendered in place of a `VirtualizedList` when there are no items in the list.
*
* Things like pull-refresh functionality will be lost unless explicitly supported by your custom component.
* Consider `renderEmptyInList` instead if you want this.
*
* It will be passed all the original props of the ImmutableVirtualizedList.
*/
renderEmpty: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* A plain string, or a function that returns some {@link PropTypes.element}
* to be rendered inside of an `EmptyVirtualizedList` when there are no items in the list.
*
* This allows pull-refresh functionality to be preserved.
*
* It will be passed all the original props of the ImmutableVirtualizedList.
*/
renderEmptyInList: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
};
static defaultProps = {
...VirtualizedList.defaultProps,
renderEmptyInList: 'No data.',
};
state = {
flattenedData: this.props.renderSectionHeader
? utils.flattenMap(this.props.immutableData)
: undefined,
stickyHeaderIndices: this.props.renderSectionHeader
? utils.getStickyHeaderIndices(this.props.immutableData)
: undefined,
};
componentWillReceiveProps(nextProps) {
const { immutableData } = this.props;
const { nextRenderSectionHeader, nextImmutableData } = nextProps;
if (nextRenderSectionHeader && immutableData !== nextImmutableData) {
this.setState({
flattenedData: utils.flattenMap(nextImmutableData),
stickyHeaderIndices: utils.getStickyHeaderIndices(nextImmutableData),
});
}
}
getVirtualizedList() {
return this.virtualizedListRef;
}
keyExtractor = (item, index) => {
if (this.props.renderSectionHeader) {
return this.state.flattenedData
.keySeq()
.skip(index)
.first();
}
return String(index);
};
scrollToEnd = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.scrollToEnd(...args);
scrollToIndex = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.scrollToIndex(...args);
scrollToItem = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.scrollToItem(...args);
scrollToOffset = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.scrollToOffset(...args);
recordInteraction = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.recordInteraction(...args);
renderItem = (info) => {
const { renderSectionHeader, renderItem } = this.props;
if (renderSectionHeader) {
const renderMethod = this.state.stickyHeaderIndices.includes(info.index)
? renderSectionHeader
: renderItem;
return renderMethod(info, this.keyExtractor(info.item, info.index));
}
return renderItem(info);
};
renderEmpty() {
const {
immutableData, renderEmpty, renderEmptyInList, contentContainerStyle,
} = this.props;
const shouldTryToRenderEmpty = renderEmpty || renderEmptyInList;
if (shouldTryToRenderEmpty && utils.isEmptyListView(immutableData)) {
if (renderEmpty) {
if (typeof renderEmpty === 'string') {
return <Text style={[styles.emptyText, contentContainerStyle]}>{renderEmpty}</Text>;
}
return renderEmpty(this.props);
}
if (renderEmptyInList) {
if (typeof renderEmptyInList === 'string') {
const { renderItem, ...passThroughProps } = this.props;
return <EmptyVirtualizedList {...passThroughProps} emptyText={renderEmptyInList} />;
}
return <EmptyVirtualizedList {...this.props} renderItem={() => renderEmptyInList(this.props)} />;
}
}
return null;
}
render() {
const {
immutableData,
renderEmpty,
renderEmptyInList,
renderSectionHeader,
renderItem,
...passThroughProps
} = this.props;
const { flattenedData, stickyHeaderIndices } = this.state;
return this.renderEmpty() || (
<VirtualizedList
ref={(component) => { this.virtualizedListRef = component; }}
data={renderSectionHeader ? flattenedData : immutableData}
getItem={(items, index) => (
renderSectionHeader
? items.skip(index).first()
: utils.getValueFromKey(index, items)
)}
getItemCount={(items) => (items.size || 0)}
keyExtractor={this.keyExtractor}
stickyHeaderIndices={renderSectionHeader ? stickyHeaderIndices : undefined}
renderItem={this.renderItem}
{...passThroughProps}
/>
);
}
}
export default ImmutableVirtualizedList;