Skip to content

Commit

Permalink
fix(searchbar): keypress can activate clear button (#25824)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi authored Aug 26, 2022
1 parent 0436fd7 commit c270756
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
18 changes: 10 additions & 8 deletions core/src/components/searchbar/searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,9 @@ export class Searchbar implements ComponentInterface {
/**
* Clears the input field and triggers the control change.
*/
private onClearInput = (ev?: Event, shouldFocus?: boolean) => {
private onClearInput = (shouldFocus?: boolean) => {
this.ionClear.emit();

if (ev) {
ev.preventDefault();
ev.stopPropagation();
}

// setTimeout() fixes https://github.com/ionic-team/ionic/issues/7527
// wait for 4 frames
setTimeout(() => {
Expand Down Expand Up @@ -533,8 +528,15 @@ export class Searchbar implements ComponentInterface {
type="button"
no-blur
class="searchbar-clear-button"
onMouseDown={(ev) => this.onClearInput(ev, true)}
onTouchStart={(ev) => this.onClearInput(ev, true)}
onPointerDown={(ev) => {
/**
* This prevents mobile browsers from
* blurring the input when the clear
* button is activated.
*/
ev.preventDefault();
}}
onClick={() => this.onClearInput(true)}
>
<ion-icon
aria-hidden="true"
Expand Down
44 changes: 44 additions & 0 deletions core/src/components/searchbar/test/basic/searchbar.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,47 @@ test.describe('searchbar: basic', () => {
await expect(cancelButton).toHaveCount(0);
});
});

test.describe('searchbar: clear button', () => {
test.beforeEach(({ skip }) => {
skip.rtl();
});
test('should clear the input when pressed', async ({ page }) => {
await page.setContent(`
<ion-searchbar value="abc" show-clear-button="always"></ion-searchbar>
`);

const searchbar = page.locator('ion-searchbar');
const clearButton = searchbar.locator('.searchbar-clear-button');

await expect(searchbar).toHaveJSProperty('value', 'abc');

await clearButton.click();
await page.waitForChanges();

await expect(searchbar).toHaveJSProperty('value', '');
});
/**
* Note: This only tests the desktop focus behavior.
* Mobile browsers have different restrictions around
* focusing inputs, so these platforms should always
* be tested when making changes to the focus behavior.
*/
test('should keep the input focused when the clear button is pressed', async ({ page }) => {
await page.setContent(`
<ion-searchbar value="abc"></ion-searchbar>
`);

const searchbar = page.locator('ion-searchbar');
const nativeInput = searchbar.locator('input');
const clearButton = searchbar.locator('.searchbar-clear-button');

await searchbar.click();
await expect(nativeInput).toBeFocused();

await clearButton.click();
await page.waitForChanges();

await expect(nativeInput).toBeFocused();
});
});

0 comments on commit c270756

Please sign in to comment.