Skip to content

Commit

Permalink
Merge pull request #11819 from aimane-chnaif/fix-11650
Browse files Browse the repository at this point in the history
set default value on picker when data has only 1 element
  • Loading branch information
tgolen authored Nov 2, 2022
2 parents 451c971 + 5029369 commit 1a20841
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/Picker/BasePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ class BasePicker extends React.Component {
this.executeOnCloseAndOnBlur = this.executeOnCloseAndOnBlur.bind(this);
}

componentDidMount() {
this.setDefaultValue();
}

componentDidUpdate(prevProps) {
if (prevProps.items === this.props.items) {
return;
}
this.setDefaultValue();
}

setDefaultValue() {
// When there is only 1 element in the selector, we do the user a favor and automatically select it for them
// so they don't have to spend extra time selecting the only possible value.
if (this.props.value || !this.props.items || this.props.items.length !== 1 || !this.props.onInputChange) {
return;
}
this.props.onInputChange(this.props.items[0].key);
}

executeOnCloseAndOnBlur() {
// Picker's onClose is not executed on Web and Desktop, so props.onClose has to be called with onBlur callback.
this.props.onClose();
Expand Down

0 comments on commit 1a20841

Please sign in to comment.