-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Pressable Sticky Header Example to RNTester
Summary: Add a test case showing off a pressable sticky header to RNTester. Note that this test case does not follow the styling of the other ScrollView Examples. I chose not to make it follow the styling because the existing examples need to be refactored later to not use custom buttons. Changelog: [General][Added] Add Pressable Sticky Header example to ScrollViewExamples in RNTester Reviewed By: lunaleaps Differential Revision: D29437827 fbshipit-source-id: 3ccee5df99bc6f00a04e1ecbd47fbe86b1eda4dc
- Loading branch information
1 parent
34903ba
commit 40a9391
Showing
2 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
packages/rn-tester/js/examples/ScrollView/ScrollViewPressableStickyHeaderExample.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,97 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
import { | ||
StyleSheet, | ||
View, | ||
Text, | ||
Pressable, | ||
ScrollView, | ||
Button, | ||
} from 'react-native'; | ||
import * as React from 'react'; | ||
|
||
function StickyHeader() { | ||
const [backgroundColor, setBackgroundColor] = React.useState('blue'); | ||
return ( | ||
<View | ||
key={0} | ||
style={{ | ||
backgroundColor: backgroundColor, | ||
margin: 10, | ||
width: 500, | ||
height: 100, | ||
}}> | ||
<Pressable | ||
style={{flex: 1}} | ||
onPress={() => { | ||
setBackgroundColor(backgroundColor === 'blue' ? 'yellow' : 'blue'); | ||
}} | ||
testID="pressable_header"> | ||
<Text>Press to change color</Text> | ||
</Pressable> | ||
</View> | ||
); | ||
} | ||
|
||
function renderComponent1(i) { | ||
return ( | ||
<View | ||
key={i} | ||
style={{backgroundColor: 'red', margin: 10, width: 500, height: 100}} | ||
/> | ||
); | ||
} | ||
|
||
export default function ScrollViewPressableStickyHeaderExample(): React.Node { | ||
const scrollRef = React.useRef(null); | ||
const components = []; | ||
for (var i = 1; i < 10; i++) { | ||
components.push(renderComponent1(i)); | ||
} | ||
return ( | ||
<View style={styles.container}> | ||
<ScrollView | ||
nestedScrollEnabled={true} | ||
ref={scrollRef} | ||
style={{flex: 1}} | ||
stickyHeaderIndices={[0]} | ||
showsVerticalScrollIndicator={false} | ||
testID="scroll_pressable_sticky_header"> | ||
<StickyHeader /> | ||
{components} | ||
</ScrollView> | ||
<View> | ||
<Button | ||
title="scroll to top" | ||
onPress={() => { | ||
scrollRef.current?.scrollTo({y: 0}); | ||
}} | ||
testID="scroll_to_top_button" | ||
/> | ||
<Button | ||
title="scroll to bottom" | ||
onPress={() => { | ||
scrollRef.current?.scrollToEnd(); | ||
}} | ||
testID="scroll_to_bottom_button" | ||
/> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
padding: 10, | ||
paddingTop: 30, | ||
flex: 1, | ||
}, | ||
}); |