diff --git a/superset-frontend/src/components/Select/AsyncSelect.test.tsx b/superset-frontend/src/components/Select/AsyncSelect.test.tsx index 4a2ba0007c..8e8e151b66 100644 --- a/superset-frontend/src/components/Select/AsyncSelect.test.tsx +++ b/superset-frontend/src/components/Select/AsyncSelect.test.tsx @@ -939,6 +939,18 @@ test('pasting an existing option does not duplicate it in multiple mode', async expect(await findAllSelectOptions()).toHaveLength(4); }); +test('does not fire onChange if the same value is selected in single mode', async () => { + const onChange = jest.fn(); + render(); + const optionText = 'Emma'; + await open(); + expect(onChange).toHaveBeenCalledTimes(0); + userEvent.click(await findSelectOption(optionText)); + expect(onChange).toHaveBeenCalledTimes(1); + userEvent.click(await findSelectOption(optionText)); + expect(onChange).toHaveBeenCalledTimes(1); +}); + /* TODO: Add tests that require scroll interaction. Needs further investigation. - Fetches more data when scrolling and more data is available diff --git a/superset-frontend/src/components/Select/AsyncSelect.tsx b/superset-frontend/src/components/Select/AsyncSelect.tsx index d41c87f047..3fb8bceaf4 100644 --- a/superset-frontend/src/components/Select/AsyncSelect.tsx +++ b/superset-frontend/src/components/Select/AsyncSelect.tsx @@ -50,10 +50,12 @@ import { mapOptions, getOption, isObject, + isEqual as utilsIsEqual, } from './utils'; import { AsyncSelectProps, AsyncSelectRef, + RawValue, SelectOptionsPagePromise, SelectOptionsType, SelectOptionsTypePage, @@ -220,7 +222,16 @@ const AsyncSelect = forwardRef( const handleOnSelect: SelectProps['onSelect'] = (selectedItem, option) => { if (isSingleMode) { + // on select is fired in single value mode if the same value is selected + const valueChanged = !utilsIsEqual( + selectedItem, + selectValue as RawValue | AntdLabeledValue, + 'value', + ); setSelectValue(selectedItem); + if (valueChanged) { + fireOnChange(); + } } else { setSelectValue(previousState => { const array = ensureIsArray(previousState); @@ -234,8 +245,8 @@ const AsyncSelect = forwardRef( } return previousState; }); + fireOnChange(); } - fireOnChange(); onSelect?.(selectedItem, option); }; diff --git a/superset-frontend/src/components/Select/Select.test.tsx b/superset-frontend/src/components/Select/Select.test.tsx index 2910353295..1daff06d4d 100644 --- a/superset-frontend/src/components/Select/Select.test.tsx +++ b/superset-frontend/src/components/Select/Select.test.tsx @@ -1053,6 +1053,18 @@ test('pasting an existing option does not duplicate it in multiple mode', async expect(await findAllSelectOptions()).toHaveLength(4); }); +test('does not fire onChange if the same value is selected in single mode', async () => { + const onChange = jest.fn(); + render(