Files
react-native/packages/rn-tester/js/examples/ScrollView/ScrollViewPressableStickyHeaderExample.js
T
Kacie Bawiec 40a93914c6 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
2021-06-30 15:44:31 -07:00

98 lines
2.1 KiB
JavaScript

/**
* 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,
},
});