Skip to content

Commit 2e3b806

Browse files
committed
refactor: only prevent mousedown on mobile if input has focus
1 parent 9900f9d commit 2e3b806

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/field-base/src/clear-button-mixin.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2021 - 2025 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { isElementFocused } from '@vaadin/a11y-base/src/focus-utils.js';
67
import { KeyboardMixin } from '@vaadin/a11y-base/src/keyboard-mixin.js';
78
import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
89
import { InputMixin } from './input-mixin.js';
@@ -71,7 +72,10 @@ export const ClearButtonMixin = (superclass) =>
7172
* @protected
7273
*/
7374
_onClearButtonMouseDown(event) {
74-
event.preventDefault();
75+
if (this._shouldKeepFocusOnClearMousedown(event)) {
76+
event.preventDefault();
77+
}
78+
7579
if (!isTouch) {
7680
this.inputElement.focus();
7781
}
@@ -109,4 +113,17 @@ export const ClearButtonMixin = (superclass) =>
109113
this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
110114
this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
111115
}
116+
117+
/**
118+
* Whether to keep focus inside the field on clear button
119+
* mousedown. By default, if the field has focus, it gets
120+
* preserved using `preventDefault()` on mousedown event
121+
* in order to avoid blur and change events.
122+
*
123+
* @protected
124+
* @return {boolean}
125+
*/
126+
_shouldKeepFocusOnClearMousedown() {
127+
return isElementFocused(this.inputElement);
128+
}
112129
};

packages/field-base/test/clear-button-mixin.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ describe('ClearButtonMixin', () => {
6767
expect(spy.calledOnce).to.be.true;
6868
});
6969

70-
it('should prevent default on clear button mousedown', () => {
70+
it('should not prevent default on clear button mousedown if input is not focused', () => {
71+
const event = new CustomEvent('mousedown', { cancelable: true });
72+
clearButton.dispatchEvent(event);
73+
expect(event.defaultPrevented).to.be.false;
74+
});
75+
76+
it('should prevent default on clear button mousedown if input is focused', () => {
77+
input.focus();
7178
const event = new CustomEvent('mousedown', { cancelable: true });
7279
clearButton.dispatchEvent(event);
7380
expect(event.defaultPrevented).to.be.true;

0 commit comments

Comments
 (0)