|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * The examples provided by Facebook are for non-commercial testing and |
| 10 | + * evaluation purposes only. |
| 11 | + * |
| 12 | + * Facebook reserves all rights not expressly granted. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 17 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + * |
| 21 | + * @flow |
| 22 | + */ |
| 23 | +'use strict'; |
| 24 | + |
| 25 | +const React = require('react'); |
| 26 | +const ReactNative = require('react-native'); |
| 27 | +const { |
| 28 | + StyleSheet, |
| 29 | + View, |
| 30 | +} = ReactNative; |
| 31 | + |
| 32 | +const FlatList = require('FlatList'); |
| 33 | +const UIExplorerPage = require('./UIExplorerPage'); |
| 34 | + |
| 35 | +const infoLog = require('infoLog'); |
| 36 | + |
| 37 | +const { |
| 38 | + FooterComponent, |
| 39 | + HeaderComponent, |
| 40 | + ItemComponent, |
| 41 | + PlainInput, |
| 42 | + SeparatorComponent, |
| 43 | + genItemData, |
| 44 | + getItemLayout, |
| 45 | + pressItem, |
| 46 | + renderSmallSwitchOption, |
| 47 | +} = require('./ListExampleShared'); |
| 48 | + |
| 49 | +class FlatListExample extends React.PureComponent { |
| 50 | + static title = '<FlatList>'; |
| 51 | + static description = 'Performant, scrollable list of data.'; |
| 52 | + |
| 53 | + state = { |
| 54 | + data: genItemData(1000), |
| 55 | + horizontal: false, |
| 56 | + filterText: '', |
| 57 | + fixedHeight: true, |
| 58 | + logViewable: false, |
| 59 | + virtualized: true, |
| 60 | + }; |
| 61 | + _onChangeFilterText = (filterText) => { |
| 62 | + this.setState({filterText}); |
| 63 | + }; |
| 64 | + _onChangeScrollToIndex = (text) => { |
| 65 | + this._listRef.scrollToIndex({viewPosition: 0.5, index: Number(text)}); |
| 66 | + }; |
| 67 | + render() { |
| 68 | + const filterRegex = new RegExp(String(this.state.filterText), 'i'); |
| 69 | + const filter = (item) => (filterRegex.test(item.text) || filterRegex.test(item.title)); |
| 70 | + const filteredData = this.state.data.filter(filter); |
| 71 | + return ( |
| 72 | + <UIExplorerPage |
| 73 | + noSpacer={true} |
| 74 | + noScroll={true}> |
| 75 | + <View style={styles.searchRow}> |
| 76 | + <PlainInput |
| 77 | + onChangeText={this._onChangeFilterText} |
| 78 | + placeholder="Search..." |
| 79 | + value={this.state.filterText} |
| 80 | + /> |
| 81 | + <PlainInput |
| 82 | + onChangeText={this._onChangeScrollToIndex} |
| 83 | + placeholder="scrollToIndex..." |
| 84 | + style={styles.searchTextInput} |
| 85 | + /> |
| 86 | + <View style={styles.options}> |
| 87 | + {renderSmallSwitchOption(this, 'virtualized')} |
| 88 | + {renderSmallSwitchOption(this, 'horizontal')} |
| 89 | + {renderSmallSwitchOption(this, 'fixedHeight')} |
| 90 | + {renderSmallSwitchOption(this, 'logViewable')} |
| 91 | + </View> |
| 92 | + </View> |
| 93 | + <FlatList |
| 94 | + HeaderComponent={HeaderComponent} |
| 95 | + FooterComponent={FooterComponent} |
| 96 | + ItemComponent={this._renderItemComponent} |
| 97 | + SeparatorComponent={SeparatorComponent} |
| 98 | + disableVirtualization={!this.state.virtualized} |
| 99 | + getItemLayout={this.state.fixedHeight ? this._getItemLayout : undefined} |
| 100 | + horizontal={this.state.horizontal} |
| 101 | + data={filteredData} |
| 102 | + key={(this.state.horizontal ? 'h' : 'v') + (this.state.fixedHeight ? 'f' : 'd')} |
| 103 | + legacyImplementation={false} |
| 104 | + onRefresh={() => alert('onRefresh: nothing to refresh :P')} |
| 105 | + refreshing={false} |
| 106 | + onViewableItemsChanged={this._onViewableItemsChanged} |
| 107 | + ref={this._captureRef} |
| 108 | + shouldItemUpdate={this._shouldItemUpdate} |
| 109 | + /> |
| 110 | + </UIExplorerPage> |
| 111 | + ); |
| 112 | + } |
| 113 | + _captureRef = (ref) => { this._listRef = ref; }; |
| 114 | + _getItemLayout = (data: any, index: number) => { |
| 115 | + return getItemLayout(data, index, this.state.horizontal); |
| 116 | + }; |
| 117 | + _renderItemComponent = ({item}) => { |
| 118 | + return ( |
| 119 | + <ItemComponent |
| 120 | + item={item} |
| 121 | + horizontal={this.state.horizontal} |
| 122 | + fixedHeight={this.state.fixedHeight} |
| 123 | + onPress={this._pressItem} |
| 124 | + /> |
| 125 | + ); |
| 126 | + }; |
| 127 | + _shouldItemUpdate(prev, next) { |
| 128 | + /** |
| 129 | + * Note that this does not check state.horizontal or state.fixedheight because we blow away the |
| 130 | + * whole list by changing the key in those cases. Make sure that you do the same in your code, |
| 131 | + * or incorporate all relevant data into the item data, or skip this optimization entirely. |
| 132 | + */ |
| 133 | + return prev.item !== next.item; |
| 134 | + } |
| 135 | + // This is called when items change viewability by scrolling into or out of the viewable area. |
| 136 | + _onViewableItemsChanged = (info: { |
| 137 | + changed: Array<{ |
| 138 | + key: string, isViewable: boolean, item: any, index: ?number, section?: any |
| 139 | + }> |
| 140 | + } |
| 141 | + ) => { |
| 142 | + // Impressions can be logged here |
| 143 | + if (this.state.logViewable) { |
| 144 | + infoLog('onViewableItemsChanged: ', info.changed.map((v) => ({...v, item: '...'}))); |
| 145 | + } |
| 146 | + }; |
| 147 | + _pressItem = (key: number) => { |
| 148 | + pressItem(this, key); |
| 149 | + }; |
| 150 | + _listRef: FlatList; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +const styles = StyleSheet.create({ |
| 155 | + options: { |
| 156 | + flexDirection: 'row', |
| 157 | + flexWrap: 'wrap', |
| 158 | + alignItems: 'center', |
| 159 | + }, |
| 160 | + searchRow: { |
| 161 | + backgroundColor: '#eeeeee', |
| 162 | + padding: 10, |
| 163 | + }, |
| 164 | +}); |
| 165 | + |
| 166 | +module.exports = FlatListExample; |
0 commit comments