forked from necolas/react-native-web
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from ArekChr/arekchr/fix-viewable-items-invert…
…ed-list Fix: onViewableItemsChanged event handler for inverted FlatList
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/react-native-web-examples/pages/viewable-items/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { | ||
FlatList, | ||
StyleSheet, | ||
Text, | ||
Pressable, | ||
View, | ||
Button | ||
} from 'react-native'; | ||
import Example from '../../shared/example'; | ||
|
||
const ITEMS = [...Array(200)].map((_, i) => `Item ${i}`); | ||
|
||
const viewabilityConfig = { | ||
itemVisiblePercentThreshold: 95 | ||
}; | ||
|
||
function createItemRow({ item, index }) { | ||
return ( | ||
<Pressable key={index} style={[styles.item]}> | ||
<Text style={styles.text}>{item}</Text> | ||
</Pressable> | ||
); | ||
} | ||
|
||
function Divider() { | ||
return <View style={styles.divider} />; | ||
} | ||
|
||
function onViewableItemsChanged({ viewableItems, changed }) { | ||
console.log('Visible items are', viewableItems); | ||
console.log('Changed in this iteration', changed); | ||
} | ||
|
||
export default function ScrollViewPage() { | ||
const [isInverted, setIsInverted] = React.useState(true); | ||
|
||
return ( | ||
<Example title="ViewableItems Test"> | ||
<View style={styles.container}> | ||
<Button | ||
onPress={() => setIsInverted((val) => !val)} | ||
title={isInverted ? 'Inverted Enabled' : 'Inverted Disabled'} | ||
/> | ||
<FlatList | ||
ItemSeparatorComponent={Divider} | ||
data={ITEMS} | ||
inverted={isInverted} | ||
onViewableItemsChanged={onViewableItemsChanged} | ||
renderItem={createItemRow} | ||
style={styles.scrollView} | ||
viewabilityConfig={viewabilityConfig} | ||
/> | ||
</View> | ||
</Example> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
alignSelf: 'stretch' | ||
}, | ||
scrollView: { | ||
backgroundColor: '#eeeeee', | ||
maxHeight: 250 | ||
}, | ||
item: { | ||
margin: 5, | ||
padding: 5, | ||
backgroundColor: '#cccccc', | ||
borderRadius: 3, | ||
minWidth: 96 | ||
}, | ||
text: { | ||
fontSize: 16, | ||
fontWeight: 'bold', | ||
margin: 5 | ||
}, | ||
divider: { | ||
width: '1rem' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters