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

fix: Verify when null value should be undefined in Select #17013

Merged
merged 3 commits into from
Oct 8, 2021
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
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 @@ -205,6 +205,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 @@ -225,9 +240,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