Skip to content

Commit

Permalink
Fix Android picker controlling
Browse files Browse the repository at this point in the history
Reviewed By: mkonicek

Differential Revision: D3261602

fb-gh-sync-id: da67d28420c5d855d672b481deeb92fa8b5d017c
fbshipit-source-id: da67d28420c5d855d672b481deeb92fa8b5d017c
  • Loading branch information
sophiebits authored and Facebook Github Bot 3 committed May 7, 2016
1 parent 0fab691 commit 0cd2904
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Libraries/Components/Picker/PickerAndroid.android.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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],
};
Expand All @@ -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;
}
},
});
Expand Down

0 comments on commit 0cd2904

Please sign in to comment.