async function focusElement(ref) {
if (ref) {
const el = await ref.getInputElement();
el.focus();
} else {
console.log("focusElement got no ref");
}
}
// Component:
function SomeComponent() {
const someRef = React.useRef()
return (
<button onClick={ () => {
focusElement(someRef.current)
}
>
focus
</button>
<IonInput ref={someRef} />
)
}
import { IonSearchbar } from '@ionic/angular';
export class MyComponent {
@ViewChild('searchbar') searchbar:IonSearchbar;
/* some stuff here */
...
anyMethodName() {
this.searchbar.setFocus();
}
}