2016-01-04 15:59:10 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-02-25 11:05:32 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-01-04 15:59:10 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2016-01-04 15:59:10 +00:00
|
|
|
const {
|
|
|
|
ScrollView,
|
|
|
|
StyleSheet,
|
|
|
|
RefreshControl,
|
|
|
|
Text,
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2016-01-04 15:59:10 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
row: {
|
|
|
|
borderColor: 'grey',
|
|
|
|
borderWidth: 1,
|
|
|
|
padding: 20,
|
|
|
|
backgroundColor: '#3a5795',
|
|
|
|
margin: 5,
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
color: '#fff',
|
|
|
|
},
|
|
|
|
scrollview: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Row extends React.Component {
|
|
|
|
_onClick = () => {
|
2016-01-04 15:59:10 +00:00
|
|
|
this.props.onClick(this.props.data);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2016-01-04 15:59:10 +00:00
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<TouchableWithoutFeedback onPress={this._onClick}>
|
2016-01-04 15:59:10 +00:00
|
|
|
<View style={styles.row}>
|
|
|
|
<Text style={styles.text}>
|
|
|
|
{this.props.data.text + ' (' + this.props.data.clicks + ' clicks)'}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 15:59:10 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class RefreshControlExample extends React.Component {
|
|
|
|
static title = '<RefreshControl>';
|
|
|
|
static description = 'Adds pull-to-refresh support to a scrollview.';
|
2016-01-04 15:59:10 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
isRefreshing: false,
|
|
|
|
loaded: 0,
|
2018-05-11 20:32:37 +00:00
|
|
|
rowData: Array.from(new Array(20)).map((val, i) => ({
|
|
|
|
text: 'Initial row ' + i,
|
|
|
|
clicks: 0,
|
|
|
|
})),
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-04 15:59:10 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
_onClick = row => {
|
2016-01-04 15:59:10 +00:00
|
|
|
row.clicks++;
|
|
|
|
this.setState({
|
|
|
|
rowData: this.state.rowData,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-04 15:59:10 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const rows = this.state.rowData.map((row, ii) => {
|
2018-05-11 20:32:37 +00:00
|
|
|
return <Row key={ii} data={row} onClick={this._onClick} />;
|
2016-01-04 15:59:10 +00:00
|
|
|
});
|
|
|
|
return (
|
|
|
|
<ScrollView
|
|
|
|
style={styles.scrollview}
|
|
|
|
refreshControl={
|
|
|
|
<RefreshControl
|
|
|
|
refreshing={this.state.isRefreshing}
|
|
|
|
onRefresh={this._onRefresh}
|
|
|
|
tintColor="#ff0000"
|
|
|
|
title="Loading..."
|
2016-04-16 22:14:50 +00:00
|
|
|
titleColor="#00ff00"
|
2016-01-04 15:59:10 +00:00
|
|
|
colors={['#ff0000', '#00ff00', '#0000ff']}
|
|
|
|
progressBackgroundColor="#ffff00"
|
|
|
|
/>
|
|
|
|
}>
|
|
|
|
{rows}
|
|
|
|
</ScrollView>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-04 15:59:10 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_onRefresh = () => {
|
2016-01-04 15:59:10 +00:00
|
|
|
this.setState({isRefreshing: true});
|
|
|
|
setTimeout(() => {
|
|
|
|
// prepend 10 items
|
|
|
|
const rowData = Array.from(new Array(10))
|
2018-05-11 20:32:37 +00:00
|
|
|
.map((val, i) => ({
|
|
|
|
text: 'Loaded row ' + (+this.state.loaded + i),
|
|
|
|
clicks: 0,
|
|
|
|
}))
|
|
|
|
.concat(this.state.rowData);
|
2016-01-04 15:59:10 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
loaded: this.state.loaded + 10,
|
|
|
|
isRefreshing: false,
|
|
|
|
rowData: rowData,
|
|
|
|
});
|
|
|
|
}, 5000);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
2016-01-04 15:59:10 +00:00
|
|
|
|
|
|
|
module.exports = RefreshControlExample;
|