Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(searchbar): keypress can activate clear button #25824

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});