Skip to content

Commit

Permalink
fix: Unexpected error on simple filter (apache#22814)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina authored Jan 23, 2023
1 parent 0b31b2c commit d479009
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
13 changes: 10 additions & 3 deletions superset-frontend/src/components/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -808,14 +808,21 @@ test('"Select All" is checked when unchecking a newly added option and all the o
});

test('does not render "Select All" when there are 0 or 1 options', async () => {
render(
const { rerender } = render(
<Select {...defaultProps} options={[]} mode="multiple" allowNewOptions />,
);
await open();
expect(screen.queryByText(selectAllOptionLabel(0))).not.toBeInTheDocument();
await type(`${NEW_OPTION}{enter}`);
rerender(
<Select
{...defaultProps}
options={OPTIONS.slice(0, 1)}
mode="multiple"
allowNewOptions
/>,
);
expect(screen.queryByText(selectAllOptionLabel(1))).not.toBeInTheDocument();
await type(`Kyle2{enter}`);
await type(`${NEW_OPTION}{enter}`);
expect(screen.queryByText(selectAllOptionLabel(2))).toBeInTheDocument();
});

Expand Down
31 changes: 23 additions & 8 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,17 @@ const Select = forwardRef(
}, [selectOptions, selectValue]);

const selectAllEnabled = useMemo(
() => !isSingleMode && fullSelectOptions.length > 1 && !inputValue,
[fullSelectOptions, isSingleMode, inputValue],
() =>
!isSingleMode &&
selectOptions.length > 0 &&
fullSelectOptions.length > 1 &&
!inputValue,
[
isSingleMode,
selectOptions.length,
fullSelectOptions.length,
inputValue,
],
);

const selectAllMode = useMemo(
Expand Down Expand Up @@ -329,7 +338,7 @@ const Select = forwardRef(
if (
!isSingleMode &&
ensureIsArray(value).length === fullSelectOptions.length &&
fullSelectOptions.length > 0
selectOptions.length > 0
) {
setSelectValue(
labelInValue
Expand All @@ -340,18 +349,24 @@ const Select = forwardRef(
] as AntdLabeledValue[]),
);
}
}, [value, isSingleMode, labelInValue, fullSelectOptions.length]);
}, [
value,
isSingleMode,
labelInValue,
fullSelectOptions.length,
selectOptions.length,
]);

useEffect(() => {
const checkSelectAll = ensureIsArray(selectValue).some(
v => getValue(v) === SELECT_ALL_VALUE,
);
if (checkSelectAll && !selectAllMode) {
setSelectValue(
labelInValue
? ([...fullSelectOptions, selectAllOption] as AntdLabeledValue[])
: ([...fullSelectOptions, SELECT_ALL_VALUE] as AntdLabeledValue[]),
const optionsToSelect = fullSelectOptions.map(option =>
labelInValue ? option : option.value,
);
optionsToSelect.push(labelInValue ? selectAllOption : SELECT_ALL_VALUE);
setSelectValue(optionsToSelect);
}
}, [selectValue, selectAllMode, labelInValue, fullSelectOptions]);

Expand Down

0 comments on commit d479009

Please sign in to comment.