From aae84c07889218dba2f382acd672400bfa4fff85 Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Fri, 20 Jan 2023 16:53:47 -0500 Subject: [PATCH] fix: Unexpected error on simple filter --- .../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 70ca4e6903342..f32ac797b5953 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]);