Skip to content

Commit

Permalink
fix: Verify when null value should be undefined in Select (#17013)
Browse files Browse the repository at this point in the history
* Check for null value

* Safety chek SelectControl and SelectAsyncControl
  • Loading branch information
geido authored and eschutho committed Nov 22, 2021
1 parent 7c966c5 commit b775193
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ const SelectAsyncControl = ({
onChange(onChangeVal);
};

const getValue = () => {
const currentValue =
value || (props.default !== undefined ? props.default : undefined);

// safety check - the value is intended to be undefined but null was used
if (currentValue === null && !options.find(o => o.value === null)) {
return undefined;
}
return currentValue;
};

useEffect(() => {
const onError = (response: Response) =>
getClientErrorObject(response).then(e => {
Expand All @@ -93,7 +104,7 @@ const SelectAsyncControl = ({
<Select
allowClear={allowClear}
ariaLabel={ariaLabel || t('Select ...')}
value={value || (props.default !== undefined ? props.default : undefined)}
value={getValue()}
header={<ControlHeader {...props} />}
mode={multi ? 'multiple' : 'single'}
onChange={handleOnChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ export default class SelectControl extends React.PureComponent {
danger,
};

const getValue = () => {
const currentValue =
value ||
(this.props.default !== undefined ? this.props.default : undefined);

// safety check - the value is intended to be undefined but null was used
if (
currentValue === null &&
!this.state.options.find(o => o.value === null)
) {
return undefined;
}
return currentValue;
};

const selectProps = {
allowNewOptions: freeForm,
autoFocus,
Expand All @@ -223,9 +238,7 @@ export default class SelectControl extends React.PureComponent {
optionRenderer,
options: this.state.options,
placeholder,
value:
value ||
(this.props.default !== undefined ? this.props.default : undefined),
value: getValue(),
};

return (
Expand Down

0 comments on commit b775193

Please sign in to comment.