Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Picker and expose setNativeProps #9118

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/components/Picker/BasePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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;
}}
/>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/StatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down