From ce7b1aabb423a7fec7f61f98aea7db079e2bfc7e Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Sat, 7 May 2016 15:51:22 -0700 Subject: [PATCH] Fix Android picker controlling Reviewed By: mkonicek Differential Revision: D3261602 fb-gh-sync-id: da67d28420c5d855d672b481deeb92fa8b5d017c fbshipit-source-id: da67d28420c5d855d672b481deeb92fa8b5d017c --- .../Picker/PickerAndroid.android.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Libraries/Components/Picker/PickerAndroid.android.js b/Libraries/Components/Picker/PickerAndroid.android.js index 797653d001d..ff91aac7081 100644 --- a/Libraries/Components/Picker/PickerAndroid.android.js +++ b/Libraries/Components/Picker/PickerAndroid.android.js @@ -52,7 +52,11 @@ var PickerAndroid = React.createClass({ }, getInitialState: function() { - return this._stateFromProps(this.props); + var state = this._stateFromProps(this.props); + return { + ...state, + initialSelectedIndex: state.selectedIndex, + }; }, componentWillReceiveProps: function(nextProps) { @@ -87,7 +91,7 @@ var PickerAndroid = React.createClass({ mode: this.props.mode, onSelect: this._onChange, prompt: this.props.prompt, - selected: this.state.selectedIndex, + selected: this.state.initialSelectedIndex, testID: this.props.testID, style: [styles.pickerAndroid, this.props.style], }; @@ -105,15 +109,24 @@ var PickerAndroid = React.createClass({ this.props.onValueChange(null, position); } } + this._lastNativePosition = event.nativeEvent.position; + this.forceUpdate(); + }, + componentDidMount: function() { + this._lastNativePosition = this.state.initialSelectedIndex; + }, + + componentDidUpdate: function() { // The picker is a controlled component. This means we expect the // on*Change handlers to be in charge of updating our // `selectedValue` prop. That way they can also // disallow/undo/mutate the selection of certain values. In other // words, the embedder of this component should be the source of // truth, not the native component. - if (this.refs[REF_PICKER] && this.state.selectedIndex !== event.nativeEvent.position) { + if (this.refs[REF_PICKER] && this.state.selectedIndex !== this._lastNativePosition) { this.refs[REF_PICKER].setNativeProps({selected: this.state.selectedIndex}); + this._lastNativePosition = this.state.selectedIndex; } }, });