From d479009e35a86dfda321492afeda2a1683a9345a Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" <70410625+michael-s-molina@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:25:28 -0500 Subject: [PATCH] fix: Unexpected error on simple filter (#22814) --- .../src/components/Select/Select.test.tsx | 13 ++++++-- .../src/components/Select/Select.tsx | 31 ++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/superset-frontend/src/components/Select/Select.test.tsx b/superset-frontend/src/components/Select/Select.test.tsx index 52f834d7cda0d..220ad4fe9875c 100644 --- a/superset-frontend/src/components/Select/Select.test.tsx +++ b/superset-frontend/src/components/Select/Select.test.tsx @@ -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( , + ); expect(screen.queryByText(selectAllOptionLabel(1))).not.toBeInTheDocument(); - await type(`Kyle2{enter}`); + await type(`${NEW_OPTION}{enter}`); expect(screen.queryByText(selectAllOptionLabel(2))).toBeInTheDocument(); }); diff --git a/superset-frontend/src/components/Select/Select.tsx b/superset-frontend/src/components/Select/Select.tsx index ac119423c88a7..68bc60871944d 100644 --- a/superset-frontend/src/components/Select/Select.tsx +++ b/superset-frontend/src/components/Select/Select.tsx @@ -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( @@ -329,7 +338,7 @@ const Select = forwardRef( if ( !isSingleMode && ensureIsArray(value).length === fullSelectOptions.length && - fullSelectOptions.length > 0 + selectOptions.length > 0 ) { setSelectValue( labelInValue @@ -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]);