From 6d65a90017718ce763cc818a553e32856c5084d3 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Tue, 2 Feb 2016 07:11:41 -0800 Subject: [PATCH] Allow customized refreshControl in ScrollView for Android 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 https://github.com/facebook/react-native/pull/5623 Reviewed By: svcscm Differential Revision: D2890072 Pulled By: nicklockwood fb-gh-sync-id: a8fc7746bcc050a6e46fedf3583979f4cb9021b6 --- .../RefreshControl/RefreshControl.js | 17 ++++++++--------- Libraries/Components/ScrollView/ScrollView.js | 18 ++++++------------ .../PullToRefreshViewAndroid.android.js | 3 +-- Libraries/ReactIOS/requireNativeComponent.js | 1 + .../SwipeRefreshLayoutManager.java | 2 +- 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Libraries/Components/RefreshControl/RefreshControl.js b/Libraries/Components/RefreshControl/RefreshControl.js index 319f054c3..1ae8119a1 100644 --- a/Libraries/Components/RefreshControl/RefreshControl.js +++ b/Libraries/Components/RefreshControl/RefreshControl.js @@ -13,6 +13,7 @@ const React = require('React'); const Platform = require('Platform'); const ColorPropType = require('ColorPropType'); +const View = require('View'); const requireNativeComponent = require('requireNativeComponent'); @@ -33,6 +34,7 @@ const RefreshControl = React.createClass({ }, propTypes: { + ...View.propTypes, /** * Called when the view starts refreshing. */ @@ -74,15 +76,7 @@ const RefreshControl = React.createClass({ }, render() { - if (Platform.OS === 'ios') { - return ; - } 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 ; }, }); @@ -91,6 +85,11 @@ if (Platform.OS === 'ios') { 'RCTRefreshControl', RefreshControl ); +} else if (Platform.OS === 'android') { + var NativeRefreshControl = requireNativeComponent( + 'AndroidSwipeRefreshLayout', + RefreshControl + ); } module.exports = RefreshControl; diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index 89cc17921..623951b5e 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -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; @@ -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 ( - - - {contentContainer} - - + return React.cloneElement( + refreshControl, + {style: this.props.style}, + + {contentContainer} + ); } } @@ -574,7 +569,6 @@ if (Platform.OS === 'android') { 'AndroidHorizontalScrollView', ScrollView ); - var AndroidSwipeRefreshLayout = requireNativeComponent('AndroidSwipeRefreshLayout'); } else if (Platform.OS === 'ios') { var RCTScrollView = requireNativeComponent('RCTScrollView', ScrollView); } diff --git a/Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js b/Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js index 1c37d2ec9..c0f498ba2 100644 --- a/Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js +++ b/Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js @@ -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'; @@ -68,7 +67,7 @@ var PullToRefreshViewAndroid = React.createClass({ render: function() { return (