2017-02-14 00:20:21 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2017-02-14 00:20:21 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-02-14 00:20:21 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-02-14 00:20:21 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2017-02-14 00:20:21 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2018-07-18 05:31:07 +00:00
|
|
|
const {Alert, Animated, Button, StyleSheet, Text, View} = ReactNative;
|
2017-02-14 00:20:21 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
const RNTesterPage = require('./RNTesterPage');
|
2017-02-14 00:20:21 +00:00
|
|
|
|
|
|
|
const infoLog = require('infoLog');
|
|
|
|
|
|
|
|
const {
|
2017-02-17 02:59:59 +00:00
|
|
|
HeaderComponent,
|
2017-02-14 00:20:21 +00:00
|
|
|
FooterComponent,
|
|
|
|
ItemComponent,
|
|
|
|
PlainInput,
|
|
|
|
SeparatorComponent,
|
2017-03-22 05:19:01 +00:00
|
|
|
Spindicator,
|
2017-02-14 00:20:21 +00:00
|
|
|
genItemData,
|
|
|
|
pressItem,
|
|
|
|
renderSmallSwitchOption,
|
2017-02-28 10:09:09 +00:00
|
|
|
renderStackedItem,
|
2017-02-14 00:20:21 +00:00
|
|
|
} = require('./ListExampleShared');
|
|
|
|
|
2017-02-28 10:09:06 +00:00
|
|
|
const VIEWABILITY_CONFIG = {
|
|
|
|
minimumViewTime: 3000,
|
|
|
|
viewAreaCoveragePercentThreshold: 100,
|
|
|
|
waitForInteraction: true,
|
|
|
|
};
|
|
|
|
|
2017-02-28 10:09:09 +00:00
|
|
|
const renderSectionHeader = ({section}) => (
|
2017-04-05 16:07:22 +00:00
|
|
|
<View style={styles.header}>
|
2017-02-14 00:20:21 +00:00
|
|
|
<Text style={styles.headerText}>SECTION HEADER: {section.key}</Text>
|
|
|
|
<SeparatorComponent />
|
2017-02-14 19:40:47 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
const renderSectionFooter = ({section}) => (
|
|
|
|
<View style={styles.header}>
|
|
|
|
<Text style={styles.headerText}>SECTION FOOTER: {section.key}</Text>
|
|
|
|
<SeparatorComponent />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
const CustomSeparatorComponent = ({highlighted, text}) => (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.customSeparator,
|
|
|
|
highlighted && {backgroundColor: 'rgb(217, 217, 217)'},
|
|
|
|
]}>
|
2017-02-17 02:59:59 +00:00
|
|
|
<Text style={styles.separatorText}>{text}</Text>
|
2017-02-14 19:40:47 +00:00
|
|
|
</View>
|
|
|
|
);
|
2017-02-14 00:20:21 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class SectionListExample extends React.PureComponent<{}, $FlowFixMeState> {
|
2017-02-14 00:20:21 +00:00
|
|
|
static title = '<SectionList>';
|
|
|
|
static description = 'Performant, scrollable list of data.';
|
|
|
|
|
|
|
|
state = {
|
|
|
|
data: genItemData(1000),
|
2017-03-22 05:19:03 +00:00
|
|
|
debug: false,
|
2017-02-14 00:20:21 +00:00
|
|
|
filterText: '',
|
|
|
|
logViewable: false,
|
|
|
|
virtualized: true,
|
2018-03-18 03:09:00 +00:00
|
|
|
inverted: false,
|
2017-02-14 00:20:21 +00:00
|
|
|
};
|
2017-03-22 05:19:01 +00:00
|
|
|
|
|
|
|
_scrollPos = new Animated.Value(0);
|
|
|
|
_scrollSinkY = Animated.event(
|
2018-05-11 20:32:37 +00:00
|
|
|
[{nativeEvent: {contentOffset: {y: this._scrollPos}}}],
|
2017-03-22 05:19:01 +00:00
|
|
|
{useNativeDriver: true},
|
|
|
|
);
|
|
|
|
|
2018-07-18 05:31:07 +00:00
|
|
|
_sectionListRef: Animated.SectionList;
|
2018-05-11 20:32:37 +00:00
|
|
|
_captureRef = ref => {
|
|
|
|
this._sectionListRef = ref;
|
|
|
|
};
|
2017-05-25 17:18:48 +00:00
|
|
|
|
|
|
|
_scrollToLocation(sectionIndex: number, itemIndex: number) {
|
2018-05-11 20:32:37 +00:00
|
|
|
this._sectionListRef.getNode().scrollToLocation({sectionIndex, itemIndex});
|
2017-05-25 17:18:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 00:20:21 +00:00
|
|
|
render() {
|
|
|
|
const filterRegex = new RegExp(String(this.state.filterText), 'i');
|
2018-05-11 20:32:37 +00:00
|
|
|
const filter = item =>
|
|
|
|
filterRegex.test(item.text) || filterRegex.test(item.title);
|
2017-02-14 00:20:21 +00:00
|
|
|
const filteredData = this.state.data.filter(filter);
|
2017-03-22 05:19:03 +00:00
|
|
|
const filteredSectionData = [];
|
|
|
|
let startIndex = 0;
|
|
|
|
const endIndex = filteredData.length - 1;
|
|
|
|
for (let ii = 10; ii <= endIndex + 10; ii += 10) {
|
|
|
|
filteredSectionData.push({
|
2018-05-11 20:32:37 +00:00
|
|
|
key: `${filteredData[startIndex].key} - ${
|
|
|
|
filteredData[Math.min(ii - 1, endIndex)].key
|
|
|
|
}`,
|
2017-03-22 05:19:03 +00:00
|
|
|
data: filteredData.slice(startIndex, ii),
|
|
|
|
});
|
|
|
|
startIndex = ii;
|
|
|
|
}
|
2017-02-14 00:20:21 +00:00
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<RNTesterPage noSpacer={true} noScroll={true}>
|
2017-02-14 00:20:21 +00:00
|
|
|
<View style={styles.searchRow}>
|
|
|
|
<PlainInput
|
|
|
|
onChangeText={filterText => {
|
|
|
|
this.setState(() => ({filterText}));
|
|
|
|
}}
|
|
|
|
placeholder="Search..."
|
|
|
|
value={this.state.filterText}
|
|
|
|
/>
|
|
|
|
<View style={styles.optionSection}>
|
|
|
|
{renderSmallSwitchOption(this, 'virtualized')}
|
|
|
|
{renderSmallSwitchOption(this, 'logViewable')}
|
2017-03-22 05:19:03 +00:00
|
|
|
{renderSmallSwitchOption(this, 'debug')}
|
2018-03-18 03:09:00 +00:00
|
|
|
{renderSmallSwitchOption(this, 'inverted')}
|
2017-03-22 05:19:01 +00:00
|
|
|
<Spindicator value={this._scrollPos} />
|
2017-02-14 00:20:21 +00:00
|
|
|
</View>
|
2017-05-25 17:18:48 +00:00
|
|
|
<View style={styles.scrollToRow}>
|
|
|
|
<Text>scroll to:</Text>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Button
|
|
|
|
title="Item A"
|
|
|
|
onPress={() => this._scrollToLocation(2, 1)}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title="Item B"
|
|
|
|
onPress={() => this._scrollToLocation(3, 6)}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title="Item C"
|
|
|
|
onPress={() => this._scrollToLocation(6, 3)}
|
|
|
|
/>
|
2017-05-25 17:18:48 +00:00
|
|
|
</View>
|
2017-02-14 00:20:21 +00:00
|
|
|
</View>
|
|
|
|
<SeparatorComponent />
|
2018-07-18 05:31:07 +00:00
|
|
|
<Animated.SectionList
|
2017-05-25 17:18:48 +00:00
|
|
|
ref={this._captureRef}
|
2017-02-17 02:59:59 +00:00
|
|
|
ListHeaderComponent={HeaderComponent}
|
|
|
|
ListFooterComponent={FooterComponent}
|
2018-05-11 20:32:37 +00:00
|
|
|
SectionSeparatorComponent={info => (
|
2017-04-25 21:44:00 +00:00
|
|
|
<CustomSeparatorComponent {...info} text="SECTION SEPARATOR" />
|
2018-05-11 20:32:37 +00:00
|
|
|
)}
|
|
|
|
ItemSeparatorComponent={info => (
|
2017-04-25 21:44:00 +00:00
|
|
|
<CustomSeparatorComponent {...info} text="ITEM SEPARATOR" />
|
2018-05-11 20:32:37 +00:00
|
|
|
)}
|
2017-03-22 05:19:03 +00:00
|
|
|
debug={this.state.debug}
|
2018-03-18 03:09:00 +00:00
|
|
|
inverted={this.state.inverted}
|
2017-02-14 00:20:21 +00:00
|
|
|
enableVirtualization={this.state.virtualized}
|
2017-05-25 17:18:48 +00:00
|
|
|
onRefresh={() => Alert.alert('onRefresh: nothing to refresh :P')}
|
2017-03-22 05:19:01 +00:00
|
|
|
onScroll={this._scrollSinkY}
|
2017-02-14 00:20:21 +00:00
|
|
|
onViewableItemsChanged={this._onViewableItemsChanged}
|
|
|
|
refreshing={false}
|
2017-02-28 10:09:09 +00:00
|
|
|
renderItem={this._renderItemComponent}
|
|
|
|
renderSectionHeader={renderSectionHeader}
|
2017-04-25 21:44:00 +00:00
|
|
|
renderSectionFooter={renderSectionFooter}
|
2017-04-05 16:07:22 +00:00
|
|
|
stickySectionHeadersEnabled
|
2017-02-14 00:20:21 +00:00
|
|
|
sections={[
|
2017-05-25 17:18:48 +00:00
|
|
|
{
|
|
|
|
key: 'empty section',
|
|
|
|
data: [],
|
|
|
|
},
|
2017-04-25 21:44:00 +00:00
|
|
|
{
|
|
|
|
renderItem: renderStackedItem,
|
|
|
|
key: 's1',
|
|
|
|
data: [
|
2018-05-11 20:32:37 +00:00
|
|
|
{
|
|
|
|
title: 'Item In Header Section',
|
|
|
|
text: 'Section s1',
|
|
|
|
key: 'header item',
|
|
|
|
},
|
2017-04-25 21:44:00 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 's2',
|
|
|
|
data: [
|
2018-05-11 20:32:37 +00:00
|
|
|
{
|
|
|
|
noImage: true,
|
|
|
|
title: '1st item',
|
|
|
|
text: 'Section s2',
|
|
|
|
key: 'noimage0',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
noImage: true,
|
|
|
|
title: '2nd item',
|
|
|
|
text: 'Section s2',
|
|
|
|
key: 'noimage1',
|
|
|
|
},
|
2017-04-25 21:44:00 +00:00
|
|
|
],
|
|
|
|
},
|
2017-03-22 05:19:03 +00:00
|
|
|
...filteredSectionData,
|
2017-02-14 00:20:21 +00:00
|
|
|
]}
|
2017-04-12 23:57:04 +00:00
|
|
|
style={styles.list}
|
2017-02-28 10:09:06 +00:00
|
|
|
viewabilityConfig={VIEWABILITY_CONFIG}
|
2017-02-14 00:20:21 +00:00
|
|
|
/>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterPage>
|
2017-02-14 00:20:21 +00:00
|
|
|
);
|
|
|
|
}
|
2017-04-12 23:57:04 +00:00
|
|
|
|
|
|
|
_renderItemComponent = ({item, separators}) => (
|
|
|
|
<ItemComponent
|
|
|
|
item={item}
|
|
|
|
onPress={this._pressItem}
|
|
|
|
onHideUnderlay={separators.unhighlight}
|
|
|
|
onShowUnderlay={separators.highlight}
|
|
|
|
/>
|
2017-03-03 21:14:04 +00:00
|
|
|
);
|
2017-04-12 23:57:04 +00:00
|
|
|
|
2017-03-03 21:14:04 +00:00
|
|
|
// This is called when items change viewability by scrolling into our out of
|
|
|
|
// the viewable area.
|
2017-02-14 00:20:21 +00:00
|
|
|
_onViewableItemsChanged = (info: {
|
|
|
|
changed: Array<{
|
2017-03-03 21:14:04 +00:00
|
|
|
key: string,
|
|
|
|
isViewable: boolean,
|
|
|
|
item: {columns: Array<*>},
|
|
|
|
index: ?number,
|
2018-05-11 20:32:37 +00:00
|
|
|
section?: any,
|
|
|
|
}>,
|
|
|
|
}) => {
|
2017-02-14 00:20:21 +00:00
|
|
|
// Impressions can be logged here
|
|
|
|
if (this.state.logViewable) {
|
2018-05-11 20:32:37 +00:00
|
|
|
infoLog(
|
|
|
|
'onViewableItemsChanged: ',
|
|
|
|
info.changed.map((v: Object) => ({
|
|
|
|
...v,
|
|
|
|
item: '...',
|
|
|
|
section: v.section.key,
|
|
|
|
})),
|
|
|
|
);
|
2017-02-14 00:20:21 +00:00
|
|
|
}
|
|
|
|
};
|
2017-04-12 23:57:04 +00:00
|
|
|
|
|
|
|
_pressItem = (key: string) => {
|
|
|
|
!isNaN(key) && pressItem(this, key);
|
2017-02-14 00:20:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2017-04-12 23:57:04 +00:00
|
|
|
customSeparator: {
|
|
|
|
backgroundColor: 'rgb(200, 199, 204)',
|
|
|
|
},
|
2017-04-05 16:07:22 +00:00
|
|
|
header: {
|
|
|
|
backgroundColor: '#e9eaed',
|
|
|
|
},
|
2017-02-14 00:20:21 +00:00
|
|
|
headerText: {
|
|
|
|
padding: 4,
|
2017-04-12 23:57:04 +00:00
|
|
|
fontWeight: '600',
|
|
|
|
},
|
|
|
|
list: {
|
|
|
|
backgroundColor: 'white',
|
2017-02-14 00:20:21 +00:00
|
|
|
},
|
|
|
|
optionSection: {
|
|
|
|
flexDirection: 'row',
|
2018-03-18 03:09:00 +00:00
|
|
|
flexWrap: 'wrap',
|
|
|
|
alignItems: 'center',
|
2017-02-14 00:20:21 +00:00
|
|
|
},
|
|
|
|
searchRow: {
|
|
|
|
paddingHorizontal: 10,
|
|
|
|
},
|
2017-05-25 17:18:48 +00:00
|
|
|
scrollToRow: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingHorizontal: 8,
|
|
|
|
},
|
2017-02-17 02:59:59 +00:00
|
|
|
separatorText: {
|
2017-02-14 19:40:47 +00:00
|
|
|
color: 'gray',
|
|
|
|
alignSelf: 'center',
|
2017-04-12 23:57:04 +00:00
|
|
|
fontSize: 7,
|
2017-02-14 19:40:47 +00:00
|
|
|
},
|
2017-02-14 00:20:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = SectionListExample;
|