|
3 | 3 | * Copyright (c) 2021 - 2025 Vaadin Ltd. |
4 | 4 | * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
5 | 5 | */ |
| 6 | +import { isElementFocused } from '@vaadin/a11y-base/src/focus-utils.js'; |
6 | 7 | import { KeyboardMixin } from '@vaadin/a11y-base/src/keyboard-mixin.js'; |
7 | 8 | import { isTouch } from '@vaadin/component-base/src/browser-utils.js'; |
8 | 9 | import { InputMixin } from './input-mixin.js'; |
@@ -71,7 +72,10 @@ export const ClearButtonMixin = (superclass) => |
71 | 72 | * @protected |
72 | 73 | */ |
73 | 74 | _onClearButtonMouseDown(event) { |
74 | | - event.preventDefault(); |
| 75 | + if (this._shouldKeepFocusOnClearMousedown(event)) { |
| 76 | + event.preventDefault(); |
| 77 | + } |
| 78 | + |
75 | 79 | if (!isTouch) { |
76 | 80 | this.inputElement.focus(); |
77 | 81 | } |
@@ -109,4 +113,17 @@ export const ClearButtonMixin = (superclass) => |
109 | 113 | this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true })); |
110 | 114 | this.inputElement.dispatchEvent(new Event('change', { bubbles: true })); |
111 | 115 | } |
| 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 | + } |
112 | 129 | }; |
0 commit comments