react-native/RNTester/js/ScrollViewExample.js

199 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-01-30 01:10:49 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule ScrollViewExample
* @format
2015-01-30 01:10:49 +00:00
*/
'use strict';
var React = require('react');
var ReactNative = require('react-native');
2015-01-30 01:10:49 +00:00
var {
Platform,
2015-01-30 01:10:49 +00:00
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
2015-01-30 01:10:49 +00:00
View,
Image,
} = ReactNative;
2015-01-30 01:10:49 +00:00
exports.displayName = 'ScrollViewExample';
2015-01-30 01:10:49 +00:00
exports.title = '<ScrollView>';
exports.description =
'Component that enables scrolling through child components';
2015-01-30 01:10:49 +00:00
exports.examples = [
{
title: '<ScrollView>',
description:
'To make content scrollable, wrap it within a <ScrollView> component',
render: function() {
var _scrollView: ScrollView;
return (
<View>
<ScrollView
ref={scrollView => {
_scrollView = scrollView;
}}
automaticallyAdjustContentInsets={false}
onScroll={() => {
console.log('onScroll!');
}}
scrollEventThrottle={200}
style={styles.scrollView}>
{THUMB_URLS.map(createThumbRow)}
</ScrollView>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.scrollTo({y: 0});
}}>
<Text>Scroll to top</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.scrollToEnd({animated: true});
}}>
<Text>Scroll to bottom</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.flashScrollIndicators();
}}>
<Text>Flash scroll indicators</Text>
</TouchableOpacity>
</View>
);
},
},
{
title: '<ScrollView> (horizontal = true)',
description:
"You can display <ScrollView>'s child components horizontally rather than vertically",
render: function() {
function renderScrollView(
title: string,
addtionalStyles: typeof StyleSheet,
) {
var _scrollView: ScrollView;
return (
<View style={addtionalStyles}>
<Text style={styles.text}>{title}</Text>
<ScrollView
ref={scrollView => {
_scrollView = scrollView;
}}
automaticallyAdjustContentInsets={false}
horizontal={true}
style={[styles.scrollView, styles.horizontalScrollView]}>
{THUMB_URLS.map(createThumbRow)}
</ScrollView>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.scrollTo({x: 0});
}}>
<Text>Scroll to start</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.scrollToEnd({animated: true});
}}>
<Text>Scroll to end</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
_scrollView.flashScrollIndicators();
}}>
<Text>Flash scroll indicators</Text>
</TouchableOpacity>
</View>
);
}
return (
<View>
{renderScrollView('LTR layout', {direction: 'ltr'})}
{renderScrollView('RTL layout', {direction: 'rtl'})}
</View>
);
},
},
];
2015-01-30 01:10:49 +00:00
class Thumb extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
shouldComponentUpdate(nextProps, nextState) {
2015-01-30 01:10:49 +00:00
return false;
}
render() {
2015-01-30 01:10:49 +00:00
return (
<View style={styles.thumb}>
<Image style={styles.img} source={this.props.source} />
2015-01-30 01:10:49 +00:00
</View>
);
}
}
2015-01-30 01:10:49 +00:00
var THUMB_URLS = [
require('./Thumbnails/like.png'),
require('./Thumbnails/dislike.png'),
require('./Thumbnails/call.png'),
require('./Thumbnails/fist.png'),
require('./Thumbnails/bandaged.png'),
require('./Thumbnails/flowers.png'),
require('./Thumbnails/heart.png'),
require('./Thumbnails/liking.png'),
require('./Thumbnails/party.png'),
require('./Thumbnails/poke.png'),
require('./Thumbnails/superlike.png'),
require('./Thumbnails/victory.png'),
];
THUMB_URLS = THUMB_URLS.concat(THUMB_URLS); // double length of THUMB_URLS
var createThumbRow = (uri, i) => <Thumb key={i} source={uri} />;
2015-01-30 01:10:49 +00:00
var styles = StyleSheet.create({
scrollView: {
backgroundColor: '#eeeeee',
2015-01-30 01:10:49 +00:00
height: 300,
},
horizontalScrollView: {
height: 106,
2015-01-30 01:10:49 +00:00
},
text: {
fontSize: 16,
fontWeight: 'bold',
margin: 5,
2015-01-30 01:10:49 +00:00
},
button: {
margin: 5,
2015-01-30 01:10:49 +00:00
padding: 5,
alignItems: 'center',
backgroundColor: '#cccccc',
2015-01-30 01:10:49 +00:00
borderRadius: 3,
},
thumb: {
margin: 5,
padding: 5,
backgroundColor: '#cccccc',
borderRadius: 3,
minWidth: 96,
2015-01-30 01:10:49 +00:00
},
img: {
width: 64,
height: 64,
},
2015-01-30 01:10:49 +00:00
});