question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ion-searchbar autofocus

See original GitHub issue

Feature Request

Ionic version:

[x] 4.x

Describe the Feature Request

Add support for autofocus property to ionic core for feature parity with ion-input. Previous patch for v3 in https://github.com/ionic-team/ionic/pull/8232 but probably outdated.

Describe Preferred Solution

Boolean property for ion-searchbar will do.

<ion-searchbar autofocus></ion-searchbar>

Describe Alternatives

The alternative uses a simple timeout (don’t follow this, not tested) based on angular.

<ion-seachbar #autofocus></ion-searchbar>
@Component(...)
export class SimpleSearch implements OnInit {
  @ViewContent('#autofocus') searchbar;

  ngOnInit() {
    setTimeout(() => this.searchbar.focus(), 300);
  }
}

Related Code

Additional Context

https://github.com/ionic-team/ionic/pull/8232

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
UTSOURCEcommented, Mar 13, 2019

@ViewChild(IonSearchbar) myInput: IonSearchbar; setTimeout(() => { this.myInput.setFocus(); }, 150); import { IonSearchbar} from '@ionic/angular';

7reactions
zer09commented, Mar 4, 2020

Without setTimeout

<ion-searchbar #searchbar></ion-searchbar>

@ViewChild('searchbar', { static: false }) searchbar: IonSearchbar;

ionViewDidEnter() {
  this.searchbar.setFocus();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Autofocus on ion-searchbar - Ionic Forum
There is not autofocus ability for ion-searchbar. Have looked into it quite a bit, we decided that autofocus was too problematic. Working across ......
Read more >
How to focus ion-searchbar on button click - Stack Overflow
I am trying to focus ion-searchbar on button click but it is not working. This is my code. Typescript @ViewChild('search') search:ElementRef ...
Read more >
Ionic Searchbar Setfocus - StackBlitz
Starter project for Ionic apps that exports to the Ionic CLI.
Read more >
How to add and set ionic autofocus input - Edupala
In your template add ion-searchbar tag or any input tag with #autofocus as a local variable, this variable can be used in our...
Read more >
Auto-focus Input and Open Keypad On View/ Page Load in ...
In Ionic application too we can achieve this behavior by 2 simple steps, the best thing is we don't need any plugin for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found