Skip to content

Commit 1bac7d4

Browse files
author
Olga Torkhanova
committed
Blur doesn't fire on click on nonfocusable element
The root cause of the issue is that the blur event is not triggered on the current active element in the click helper in case if the clicked element is not focusable. If the clicked element is focusable the previous active element gets blur event trough the __focus__ helper. A call to __blur__ was added for the case when the clicked element is not focusable.
1 parent 77022fb commit 1bac7d4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

addon-test-support/@ember/test-helpers/dom/click.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import isFormControl from './-is-form-control';
99
import Target from './-target';
1010
import { log } from '@ember/test-helpers/dom/-logging';
1111
import { runHooks, registerHook } from '../-internal/helper-hooks';
12+
import { __blur__ } from './blur';
1213

1314
const PRIMARY_BUTTON = 1;
1415
const MAIN_BUTTON_PRESSED = 0;
@@ -36,6 +37,12 @@ export function __click__(element: Element | Document | Window, options: MouseEv
3637

3738
if (isFocusable(element)) {
3839
__focus__(element);
40+
} else if (
41+
document.activeElement &&
42+
document.activeElement !== element &&
43+
isFocusable(document.activeElement)
44+
) {
45+
__blur__(document.activeElement, null);
3946
}
4047

4148
fireEvent(element, 'mouseup', options);

tests/unit/dom/click-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ module('DOM Helper: click', function (hooks) {
233233
assert.verifySteps(['mousedown', 'mouseup', 'click']);
234234
});
235235
});
236+
237+
module('focusable and non-focusable elements interaction', function () {
238+
test('clicking on non-focusable element triggers blur on active element', async function (assert) {
239+
element = document.createElement('div');
240+
241+
insertElement(element);
242+
243+
const focusableElement = buildInstrumentedElement('input');
244+
245+
await click(focusableElement);
246+
await click(element);
247+
248+
assert.verifySteps(['mousedown', 'focus', 'focusin', 'mouseup', 'click', 'blur', 'focusout']);
249+
});
250+
251+
test('clicking on focusable element triggers blur on active element', async function (assert) {
252+
element = document.createElement('input');
253+
254+
insertElement(element);
255+
256+
const focusableElement = buildInstrumentedElement('input');
257+
258+
await click(focusableElement);
259+
await click(element);
260+
261+
assert.verifySteps(['mousedown', 'focus', 'focusin', 'mouseup', 'click', 'blur', 'focusout']);
262+
});
263+
});
236264
});
237265

238266
module('DOM Helper: click with window', function () {

0 commit comments

Comments
 (0)