Skip to content

Commit 88ebdb2

Browse files
janicduplessisFacebook Github Bot 7
authored andcommitted
RefreshControl refreshing prop and doc improvements
Summary:This makes the `refreshing` prop more 'controlled'. Before forgetting to set the refreshing prop in the onRefresh callback would make the js and native `refreshing` prop get out of sync and make the RefreshControl stop refreshing properly (see facebook#5839). I also added a simple usage example and a note about the refreshing prop in the doc. There was also a small bug in the doc generation code that made the array of color show as [[object Object]] instead of [color] so I fixed that too. ** Test plan** Tested using the UIExplorer example on iOS and Android. Not setting the `refreshing` prop to true in the `onRefresh` function should cause the RefreshControl to stop refreshing immediately and continue working properly after. Closes facebook#5839 Closes facebook#6434 Differential Revision: D3046279 Pulled By: nicklockwood fb-gh-sync-id: ebda04c659a10f0b9d468473c8d5c659256ca1b5 shipit-source-id: ebda04c659a10f0b9d468473c8d5c659256ca1b5
1 parent 972395a commit 88ebdb2

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

Libraries/Components/RefreshControl/RefreshControl.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
*/
1212
'use strict';
1313

14-
const React = require('React');
15-
const Platform = require('Platform');
1614
const ColorPropType = require('ColorPropType');
15+
const NativeMethodsMixin = require('NativeMethodsMixin');
16+
const Platform = require('Platform');
17+
const React = require('React');
1718
const View = require('View');
1819

1920
const requireNativeComponent = require('requireNativeComponent');
@@ -25,15 +26,57 @@ if (Platform.OS === 'android') {
2526
}
2627

2728
/**
28-
* This component is used inside a ScrollView to add pull to refresh
29+
* This component is used inside a ScrollView or ListView to add pull to refresh
2930
* functionality. When the ScrollView is at `scrollY: 0`, swiping down
3031
* triggers an `onRefresh` event.
32+
*
33+
* ### Usage example
34+
*
35+
* ``` js
36+
* class RefreshableList extends Component {
37+
* constructor(props) {
38+
* super(props);
39+
* this.state = {
40+
* refreshing: false,
41+
* };
42+
* }
43+
*
44+
* _onRefresh() {
45+
* this.setState({refreshing: true});
46+
* fetchData().then(() => {
47+
* this.setState({refreshing: false});
48+
* });
49+
* }
50+
*
51+
* render() {
52+
* return (
53+
* <ListView
54+
* refreshControl={
55+
* <RefreshControl
56+
* refreshing={this.state.refreshing}
57+
* onRefresh={this._onRefresh.bind(this)}
58+
* />
59+
* }
60+
* ...
61+
* >
62+
* ...
63+
* </ListView>
64+
* );
65+
* }
66+
* ...
67+
* }
68+
* ```
69+
*
70+
* __Note:__ `refreshing` is a controlled prop, this is why it needs to be set to true
71+
* in the `onRefresh` function otherwise the refresh indicator will stop immediatly.
3172
*/
3273
const RefreshControl = React.createClass({
3374
statics: {
3475
SIZE: RefreshLayoutConsts.SIZE,
3576
},
3677

78+
mixins: [NativeMethodsMixin],
79+
3780
propTypes: {
3881
...View.propTypes,
3982
/**
@@ -76,8 +119,21 @@ const RefreshControl = React.createClass({
76119
size: React.PropTypes.oneOf(RefreshLayoutConsts.SIZE.DEFAULT, RefreshLayoutConsts.SIZE.LARGE),
77120
},
78121

122+
_nativeRef: {},
123+
79124
render() {
80-
return <NativeRefreshControl {...this.props} />;
125+
return (
126+
<NativeRefreshControl
127+
{...this.props}
128+
ref={ref => this._nativeRef = ref}
129+
onRefresh={this._onRefresh}
130+
/>
131+
);
132+
},
133+
134+
_onRefresh() {
135+
this.props.onRefresh && this.props.onRefresh();
136+
this._nativeRef.setNativeProps({refreshing: this.props.refreshing});
81137
},
82138
});
83139

website/layout/AutodocsLayout.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ function renderType(type) {
4141
return '{' + Object.keys(type.value).map((key => key + ': ' + renderType(type.value[key]))).join(', ') + '}';
4242
}
4343

44-
if (type.name == 'union') {
44+
if (type.name === 'union') {
4545
return type.value.map(renderType).join(', ');
4646
}
4747

4848
if (type.name === 'arrayOf') {
49-
return '[' + renderType(type.value) + ']';
49+
return <span>[{renderType(type.value)}]</span>;
5050
}
5151

5252
if (type.name === 'instanceOf') {
@@ -56,10 +56,10 @@ function renderType(type) {
5656
if (type.name === 'custom') {
5757
if (styleReferencePattern.test(type.raw)) {
5858
var name = type.raw.substring(0, type.raw.indexOf('.'));
59-
return <a href={'docs/' + slugify(name) + '.html#style'}>{name}#style</a>
59+
return <a href={'docs/' + slugify(name) + '.html#style'}>{name}#style</a>;
6060
}
6161
if (type.raw === 'ColorPropType') {
62-
return <a href={'docs/colors.html'}>color</a>
62+
return <a href={'docs/colors.html'}>color</a>;
6363
}
6464
if (type.raw === 'EdgeInsetsPropType') {
6565
return '{top: number, left: number, bottom: number, right: number}';

0 commit comments

Comments
 (0)