Skip to content

Commit ff71ad4

Browse files
authored
fix(input): clear button is not activated on swipe (#25825)
resolves #24857
1 parent 5270151 commit ff71ad4

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

core/src/components/input/input.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,6 @@ export class Input implements ComponentInterface {
402402
this.isComposing = false;
403403
};
404404

405-
private clearTextOnEnter = (ev: KeyboardEvent) => {
406-
if (ev.key === 'Enter') {
407-
this.clearTextInput(ev);
408-
}
409-
};
410-
411405
private clearTextInput = (ev?: Event) => {
412406
if (this.clearInput && !this.readonly && !this.disabled && ev) {
413407
ev.preventDefault();
@@ -496,8 +490,15 @@ export class Input implements ComponentInterface {
496490
aria-label="reset"
497491
type="button"
498492
class="input-clear-icon"
499-
onPointerDown={this.clearTextInput}
500-
onKeyDown={this.clearTextOnEnter}
493+
onPointerDown={(ev) => {
494+
/**
495+
* This prevents mobile browsers from
496+
* blurring the input when the clear
497+
* button is activated.
498+
*/
499+
ev.preventDefault();
500+
}}
501+
onClick={this.clearTextInput}
501502
/>
502503
)}
503504
</Host>

core/src/components/input/test/basic/input.e2e.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,47 @@ test.describe('input: basic', () => {
157157
});
158158
});
159159
});
160+
161+
test.describe('input: clear button', () => {
162+
test.beforeEach(({ skip }) => {
163+
skip.rtl();
164+
});
165+
test('should clear the input when pressed', async ({ page }) => {
166+
await page.setContent(`
167+
<ion-input value="abc" clear-input="true"></ion-input>
168+
`);
169+
170+
const input = page.locator('ion-input');
171+
const clearButton = input.locator('.input-clear-icon');
172+
173+
await expect(input).toHaveJSProperty('value', 'abc');
174+
175+
await clearButton.click();
176+
await page.waitForChanges();
177+
178+
await expect(input).toHaveJSProperty('value', '');
179+
});
180+
/**
181+
* Note: This only tests the desktop focus behavior.
182+
* Mobile browsers have different restrictions around
183+
* focusing inputs, so these platforms should always
184+
* be tested when making changes to the focus behavior.
185+
*/
186+
test('should keep the input focused when the clear button is pressed', async ({ page }) => {
187+
await page.setContent(`
188+
<ion-input value="abc" clear-input="true"></ion-searchbar>
189+
`);
190+
191+
const input = page.locator('ion-input');
192+
const nativeInput = input.locator('input');
193+
const clearButton = input.locator('.input-clear-icon');
194+
195+
await input.click();
196+
await expect(nativeInput).toBeFocused();
197+
198+
await clearButton.click();
199+
await page.waitForChanges();
200+
201+
await expect(nativeInput).toBeFocused();
202+
});
203+
});

0 commit comments

Comments
 (0)