Skip to content

Commit a93f9ef

Browse files
authored
refactor: only prevent mousedown on mobile if input has focus (#10270)
1 parent 0c07ace commit a93f9ef

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

packages/date-picker/src/vaadin-date-picker-mixin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,23 @@ export const DatePickerMixin = (subclass) =>
616616
return !this._shouldKeepFocusRing;
617617
}
618618

619+
/**
620+
* Override method inherited from `ClearButtonMixin`
621+
* to not blur on clear button mousedown when opened
622+
* so that focus remains in the input field.
623+
*
624+
* @return {boolean}
625+
* @protected
626+
* @override
627+
*/
628+
_shouldKeepFocusOnClearMousedown() {
629+
if (this.opened) {
630+
return true;
631+
}
632+
633+
return super._shouldKeepFocusOnClearMousedown();
634+
}
635+
619636
/**
620637
* Override method inherited from `FocusMixin`
621638
* to prevent removing the `focused` attribute:

packages/date-picker/test/basic.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,30 @@ describe('clear button', () => {
362362
click(clearButton);
363363
expect(datePicker.opened).to.be.not.ok;
364364
});
365+
366+
it('should not prevent default on clear button mousedown if input is not focused', () => {
367+
datePicker.value = '2001-01-01';
368+
const event = new CustomEvent('mousedown', { cancelable: true });
369+
clearButton.dispatchEvent(event);
370+
expect(event.defaultPrevented).to.be.false;
371+
});
372+
373+
it('should prevent default on clear button mousedown if input is focused', () => {
374+
datePicker.value = '2001-01-01';
375+
datePicker.inputElement.focus();
376+
const event = new CustomEvent('mousedown', { cancelable: true });
377+
clearButton.dispatchEvent(event);
378+
expect(event.defaultPrevented).to.be.true;
379+
});
380+
381+
it('should prevent default on clear button mousedown when opened', async () => {
382+
datePicker.value = '2001-01-01';
383+
await open(datePicker);
384+
datePicker.inputElement.blur();
385+
const event = new CustomEvent('mousedown', { cancelable: true });
386+
clearButton.dispatchEvent(event);
387+
expect(event.defaultPrevented).to.be.true;
388+
});
365389
});
366390

367391
describe('initial value attribute', () => {

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()) {
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)