diff --git a/src/components/Picker/BasePicker/index.js b/src/components/Picker/BasePicker/index.js index bdd5def02ec3..eb3021ab54a9 100644 --- a/src/components/Picker/BasePicker/index.js +++ b/src/components/Picker/BasePicker/index.js @@ -10,17 +10,26 @@ class BasePicker extends React.Component { constructor(props) { super(props); - this.state = { - selectedValue: this.props.defaultValue, - }; + this.pickerValue = this.props.defaultValue; this.updateSelectedValueAndExecuteOnChange = this.updateSelectedValueAndExecuteOnChange.bind(this); this.executeOnCloseAndOnBlur = this.executeOnCloseAndOnBlur.bind(this); + this.setNativeProps = this.setNativeProps.bind(this); + } + + /** + * This method mimicks RN's setNativeProps method. It's exposed to Picker's ref and can be used by other components + * to directly manipulate Picker's value when Picker is used as an uncontrolled input. + * + * @param {*} value + */ + setNativeProps({value}) { + this.pickerValue = value; } updateSelectedValueAndExecuteOnChange(value) { this.props.onInputChange(value); - this.setState({selectedValue: value}); + this.pickerValue = value; } executeOnCloseAndOnBlur() { @@ -38,7 +47,7 @@ class BasePicker extends React.Component { style={this.props.size === 'normal' ? basePickerStyles(this.props.disabled, hasError, this.props.focused) : styles.pickerSmall} useNativeAndroidPickerStyle={false} placeholder={this.props.placeholder} - value={this.props.value || this.state.selectedValue} + value={this.props.value || this.pickerValue} Icon={() => this.props.icon(this.props.size)} disabled={this.props.disabled} fixAndroidTouchableBug @@ -47,7 +56,16 @@ class BasePicker extends React.Component { pickerProps={{ onFocus: this.props.onOpen, onBlur: this.executeOnCloseAndOnBlur, - ref: this.props.innerRef, + }} + ref={(node) => { + if (!node || !_.isFunction(this.props.innerRef)) { + return; + } + + this.props.innerRef(node); + + // eslint-disable-next-line no-param-reassign + node.setNativeProps = this.setNativeProps; }} /> ); diff --git a/src/components/StatePicker.js b/src/components/StatePicker.js index 0ae82af6baee..a76fd7ba0197 100644 --- a/src/components/StatePicker.js +++ b/src/components/StatePicker.js @@ -55,7 +55,8 @@ const StatePicker = forwardRef((props, ref) => ( placeholder={{value: '', label: '-'}} items={STATES} onInputChange={props.onInputChange} - value={props.value ? props.value : props.defaultValue} + value={props.value} + defaultValue={props.defaultValue} label={props.label || props.translate('common.state')} errorText={props.errorText} onBlur={props.onBlur}