Skip to content

Commit

Permalink
Allow customized refreshControl in ScrollView for Android
Browse files Browse the repository at this point in the history
Summary:
Original Android's refreshControl in ScrollView is tightly coupled with AndroidSwipeRefreshLayout. If someone use `ref=` for RefreshControl in ScrollView, it does nothing since RefreshControl in Android return null.

This change allows customized  RefreshControl especially for `ref=` as well as making ScrollView's code clearer.
Closes #5623

Reviewed By: svcscm

Differential Revision: D2890072

Pulled By: nicklockwood

fb-gh-sync-id: a8fc7746bcc050a6e46fedf3583979f4cb9021b6
  • Loading branch information
Kudo authored and facebook-github-bot-1 committed Feb 2, 2016
1 parent a0bed63 commit 6d65a90
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
17 changes: 8 additions & 9 deletions Libraries/Components/RefreshControl/RefreshControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const React = require('React');
const Platform = require('Platform');
const ColorPropType = require('ColorPropType');
const View = require('View');

const requireNativeComponent = require('requireNativeComponent');

Expand All @@ -33,6 +34,7 @@ const RefreshControl = React.createClass({
},

propTypes: {
...View.propTypes,
/**
* Called when the view starts refreshing.
*/
Expand Down Expand Up @@ -74,15 +76,7 @@ const RefreshControl = React.createClass({
},

render() {
if (Platform.OS === 'ios') {
return <NativeRefreshControl {...this.props}/>;
} else {
// On Android the ScrollView is wrapped so this component doesn't render
// anything and only acts as a way to configure the wrapper view.
// ScrollView will wrap itself in a AndroidSwipeRefreshLayout using props
// from this.
return null;
}
return <NativeRefreshControl {...this.props} />;
},
});

Expand All @@ -91,6 +85,11 @@ if (Platform.OS === 'ios') {
'RCTRefreshControl',
RefreshControl
);
} else if (Platform.OS === 'android') {
var NativeRefreshControl = requireNativeComponent(
'AndroidSwipeRefreshLayout',
RefreshControl
);
}

module.exports = RefreshControl;
18 changes: 6 additions & 12 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var insetsDiffer = require('insetsDiffer');
var invariant = require('invariant');
var pointsDiffer = require('pointsDiffer');
var requireNativeComponent = require('requireNativeComponent');
var processColor = require('processColor');
var processDecelerationRate = require('processDecelerationRate');
var PropTypes = React.PropTypes;

Expand Down Expand Up @@ -507,16 +506,12 @@ var ScrollView = React.createClass({
// On Android wrap the ScrollView with a AndroidSwipeRefreshLayout.
// Since the ScrollView is wrapped add the style props to the
// AndroidSwipeRefreshLayout and use flex: 1 for the ScrollView.
var refreshProps = refreshControl.props;
return (
<AndroidSwipeRefreshLayout
{...refreshProps}
colors={refreshProps.colors && refreshProps.colors.map(processColor)}
style={props.style}>
<ScrollViewClass {...props} style={styles.base} ref={SCROLLVIEW}>
{contentContainer}
</ScrollViewClass>
</AndroidSwipeRefreshLayout>
return React.cloneElement(
refreshControl,
{style: this.props.style},
<ScrollViewClass {...props} style={styles.base} ref={SCROLLVIEW}>
{contentContainer}
</ScrollViewClass>
);
}
}
Expand Down Expand Up @@ -574,7 +569,6 @@ if (Platform.OS === 'android') {
'AndroidHorizontalScrollView',
ScrollView
);
var AndroidSwipeRefreshLayout = requireNativeComponent('AndroidSwipeRefreshLayout');
} else if (Platform.OS === 'ios') {
var RCTScrollView = requireNativeComponent('RCTScrollView', ScrollView);
}
Expand Down
3 changes: 1 addition & 2 deletions Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var RefreshLayoutConsts = require('UIManager').AndroidSwipeRefreshLayout.Constan
var View = require('View');

var onlyChild = require('onlyChild');
var processColor = require('processColor');
var requireNativeComponent = require('requireNativeComponent');

var NATIVE_REF = 'native_swiperefreshlayout';
Expand Down Expand Up @@ -68,7 +67,7 @@ var PullToRefreshViewAndroid = React.createClass({
render: function() {
return (
<NativePullToRefresh
colors={this.props.colors && this.props.colors.map(processColor)}
colors={this.props.colors}
enabled={this.props.enabled}
onRefresh={this._onRefresh}
progressBackgroundColor={this.props.progressBackgroundColor}
Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/requireNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var TypeToProcessorMap = {
RCTImageSource: resolveAssetSource,
// Android Types
Color: processColor,
ColorArray: processColorArray,
};

module.exports = requireNativeComponent;
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void setEnabled(ReactSwipeRefreshLayout view, boolean enabled) {
view.setEnabled(enabled);
}

@ReactProp(name = "colors")
@ReactProp(name = "colors", customType = "ColorArray")
public void setColors(ReactSwipeRefreshLayout view, @Nullable ReadableArray colors) {
if (colors != null) {
int[] colorValues = new int[colors.size()];
Expand Down

0 comments on commit 6d65a90

Please sign in to comment.