Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated RefreshControl component #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 95 additions & 53 deletions Libraries/Components/RefreshControl/RefreshControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @format
* @flow
* @generate-docs
*/

'use strict';
Expand Down Expand Up @@ -36,41 +37,63 @@ if (Platform.OS === 'android') {

type IOSProps = $ReadOnly<{|
/**
* The color of the refresh indicator.
The color of the refresh indicator.

@platform ios
*/
tintColor?: ?ColorValue,
/**
* Title color.
The color of the refresh indicator title.

@platform ios
*/
titleColor?: ?ColorValue,
/**
* The title displayed under the refresh indicator.
The title displayed under the refresh indicator.

@platform ios
*/
title?: ?string,
|}>;

type AndroidProps = $ReadOnly<{|
/**
* Whether the pull to refresh functionality is enabled.
Whether the pull to refresh functionality is enabled.

@platform android

@default true
*/
enabled?: ?boolean,
/**
* The colors (at least one) that will be used to draw the refresh indicator.
The colors (at least one) that will be used to draw the refresh indicator.

@platform android
*/
colors?: ?$ReadOnlyArray<ColorValue>,
/**
* The background color of the refresh indicator.
The background color of the refresh indicator.

@platform android
*/
progressBackgroundColor?: ?ColorValue,
/**
* Size of the refresh indicator, see RefreshControl.SIZE.
Size of the refresh indicator.

@platform android

@default RefreshLayoutConsts.SIZE.DEFAULT
*/
size?: ?(
| typeof RefreshLayoutConsts.SIZE.DEFAULT
| typeof RefreshLayoutConsts.SIZE.LARGE
),
/**
* Progress view top offset
Progress view top offset

@platform android

@default 0
*/
progressViewOffset?: ?number,
|}>;
Expand All @@ -81,60 +104,79 @@ export type RefreshControlProps = $ReadOnly<{|
...AndroidProps,

/**
* Called when the view starts refreshing.
Called when the view starts refreshing.
*/
onRefresh?: ?() => void | Promise<void>,

/**
* Whether the view should be indicating an active refresh.
Whether the view should be indicating an active refresh.
*/
refreshing: boolean,
|}>;

/**
* This component is used inside a ScrollView or ListView to add pull to refresh
* functionality. When the ScrollView is at `scrollY: 0`, swiping down
* triggers an `onRefresh` event.
*
* ### Usage example
*
* ``` js
* class RefreshableList extends Component {
* constructor(props) {
* super(props);
* this.state = {
* refreshing: false,
* };
* }
*
* _onRefresh() {
* this.setState({refreshing: true});
* fetchData().then(() => {
* this.setState({refreshing: false});
* });
* }
*
* render() {
* return (
* <ListView
* refreshControl={
* <RefreshControl
* refreshing={this.state.refreshing}
* onRefresh={this._onRefresh.bind(this)}
* />
* }
* ...
* >
* ...
* </ListView>
* );
* }
* ...
* }
* ```
*
* __Note:__ `refreshing` is a controlled prop, this is why it needs to be set to true
* in the `onRefresh` function otherwise the refresh indicator will stop immediately.
This component is used inside a ScrollView or ListView to add pull to refresh
functionality. When the ScrollView is at `scrollY: 0`, swiping down
triggers an `onRefresh` event.

```SnackPlayer name=RefreshControl&supportedPlatforms=ios,android
import React from 'react';
import {
ScrollView,
RefreshControl,
StyleSheet,
Text,
SafeAreaView,
} from 'react-native';
import Constants from 'expo-constants';

const wait = (timeout) => {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
}

const App = () => {
const [refreshing, setRefreshing] = React.useState(false);

const onRefresh = React.useCallback(() => {
setRefreshing(true);

wait(2000).then(() => setRefreshing(false));
}, []);

return (
<SafeAreaView style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollView}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
>
<Text>Pull down to see RefreshControl indicator</Text>
</ScrollView>
</SafeAreaView>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
scrollView: {
flex: 1,
backgroundColor: 'pink',
alignItems: 'center',
justifyContent: 'center',
},
});

export default App;
```

> Note: `refreshing` is a controlled prop, this is why it needs to be set to
true in the `onRefresh` function otherwise the refresh indicator will stop immediately.
*/
class RefreshControl extends React.Component<RefreshControlProps> {
static SIZE: any = RefreshLayoutConsts.SIZE;
Expand Down