Skip to content

Commit

Permalink
Safety chek SelectControl and SelectAsyncControl
Browse files Browse the repository at this point in the history
  • Loading branch information
geido committed Oct 8, 2021
1 parent 5302d2a commit a5aabfb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
9 changes: 2 additions & 7 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,8 @@ const Select = ({
}, [options]);

useEffect(() => {
let selectValue = value;
// special case where null is intended to be undefined
if (value === null && !selectOptions.find(o => o.value === null)) {
selectValue = undefined;
}
setSelectValue(selectValue);
}, [value, selectOptions]);
setSelectValue(value);
}, [value]);

useEffect(() => {
if (selectValue) {
Expand Down
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

0 comments on commit a5aabfb

Please sign in to comment.