Skip to content

Commit 7650d53

Browse files
authored
revert: Fixes unexpected autosuggest dropdown reopen when clicking outside browser window and back (#3316)
1 parent e309845 commit 7650d53

File tree

3 files changed

+11
-36
lines changed

3 files changed

+11
-36
lines changed

src/autosuggest/__tests__/autosuggest.test.tsx

+11-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { KeyCode } from '@cloudscape-design/test-utils-core/utils';
88

99
import '../../__a11y__/to-validate-a11y';
1010
import Autosuggest, { AutosuggestProps } from '../../../lib/components/autosuggest';
11-
import { documentHasFocus } from '../../../lib/components/internal/utils/dom';
1211
import createWrapper from '../../../lib/components/test-utils/dom';
1312

1413
import itemStyles from '../../../lib/components/internal/components/selectable-item/styles.css.js';
@@ -58,15 +57,8 @@ jest.mock('@cloudscape-design/component-toolkit/internal', () => {
5857
warnOnce: jest.fn(),
5958
};
6059
});
61-
62-
jest.mock('../../../lib/components/internal/utils/dom', () => ({
63-
...jest.requireActual('../../../lib/components/internal/utils/dom'),
64-
documentHasFocus: jest.fn(() => true),
65-
}));
66-
6760
beforeEach(() => {
68-
(warnOnce as jest.Mock).mockClear();
69-
(documentHasFocus as jest.Mock).mockClear();
61+
(warnOnce as any).mockClear();
7062
});
7163

7264
test('renders correct labels when focused', () => {
@@ -143,24 +135,21 @@ test('entered text option should not get screenreader override', () => {
143135
).toBeFalsy();
144136
});
145137

146-
test('should close dropdown on blur when document is in focus', () => {
147-
const { wrapper } = renderAutosuggest(<Autosuggest enteredTextLabel={v => v} value="1" options={defaultOptions} />);
138+
test('should not close dropdown when no realted target in blur', () => {
139+
const { wrapper, container } = renderAutosuggest(
140+
<div>
141+
<Autosuggest enteredTextLabel={v => v} value="1" options={defaultOptions} />
142+
<button id="focus-target">focus target</button>
143+
</div>
144+
);
148145
wrapper.findNativeInput().focus();
149146
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null);
150147

151-
wrapper.findNativeInput().blur();
152-
expect(wrapper.findDropdown().findOpenDropdown()).toBe(null);
153-
});
154-
155-
test('should not close dropdown on blur when document is not in focus', () => {
156-
(documentHasFocus as jest.Mock).mockImplementation(() => false);
157-
158-
const { wrapper } = renderAutosuggest(<Autosuggest enteredTextLabel={v => v} value="1" options={defaultOptions} />);
159-
wrapper.findNativeInput().focus();
148+
document.body.focus();
160149
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null);
161150

162-
wrapper.findNativeInput().blur();
163-
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null);
151+
createWrapper(container).find('#focus-target')!.focus();
152+
expect(wrapper.findDropdown().findOpenDropdown()).toBe(null);
164153
});
165154

166155
it('should warn if recoveryText is provided without associated handler', () => {

src/internal/components/autosuggest-input/index.tsx

-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { FormFieldValidationControlProps, useFormFieldContext } from '../../cont
1818
import { BaseKeyDetail, fireCancelableEvent, fireNonCancelableEvent, NonCancelableEventHandler } from '../../events';
1919
import { InternalBaseComponentProps } from '../../hooks/use-base-component';
2020
import { KeyCode } from '../../keycode';
21-
import { documentHasFocus } from '../../utils/dom';
2221
import { nodeBelongs } from '../../utils/node-belongs';
2322
import Dropdown from '../dropdown';
2423
import { ExpandToViewport } from '../dropdown/interfaces';
@@ -131,11 +130,6 @@ const AutosuggestInput = React.forwardRef(
131130
}));
132131

133132
const handleBlur = () => {
134-
// When the document in not in focus it means the focus moved outside the page. In that case the dropdown shall stay open
135-
// so that when the user comes back it does not re-open unexpectedly when the focus is restored by the browser.
136-
if (!documentHasFocus()) {
137-
return;
138-
}
139133
if (!preventCloseOnBlurRef.current) {
140134
closeDropdown();
141135
fireNonCancelableEvent(onBlur, null);

src/internal/utils/dom.ts

-8
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,3 @@ export function isSVGElement(target: unknown): target is SVGElement {
128128
typeof target.ownerSVGElement === 'object')
129129
);
130130
}
131-
132-
export function documentHasFocus() {
133-
// In JSDOM the hasFocus() always returns false which differs from the in-browser experience, see: https://github.com/jsdom/jsdom/issues/3794.
134-
// Thus, when detecting JSDOM environment we return true here explicitly for the tests to work expectedly.
135-
// The detection depends on the default userAgent set in JSDOM, see: https://github.com/jsdom/jsdom?tab=readme-ov-file#advanced-configuration.
136-
const isJSDOM = typeof navigator !== 'undefined' && navigator.userAgent?.includes('jsdom');
137-
return isJSDOM || (typeof document !== undefined && document.hasFocus());
138-
}

0 commit comments

Comments
 (0)