Prerequisites
Ionic Framework Version
v7.x
Current Behavior
Ionic Framework offers an API on certain form controls, such as on ion-input, to access the HTML form element that is rendered inside the web component.
This API is typically named getInputElement and is useful when a developer needs access to the input/textarea form control element to attach additional functionality.
A key feature where this is used is in input masking, where libraries like Maskito need access to the form element to attach the input masking behavior.
When using Ionic's API, the element is not always initially available until after the component template renders. As a result, developers that try to access the method will return an element reference that is undefined. As a result, developers need to hack around this limitation by either waiting a period with setTimeout or using requestAnimationFrame to give enough time for the web component to render, prior to accessing the element reference. This is an extra implementation detail and not developer friendly.
This is most prevalent when trying to access the element reference inside of the ref binding in React.
<IonInput ref={async input => {
const nativeInput = await input.getInputElement();
// nativeInput is undefined
}} />
Expected Behavior
Since all public method APIs with Ionic's web components are asynchronous, Ionic should wait to resolve the element reference until after the web component has rendered.
For instance:
private nativeInput: HTMLInputElement;
private componentReadyResolve!: () => void;
private componentReady = new Promise<void>((resolve) => (this.componentReadyResolve = resolve));
componentDidLoad() {
this.componentReadyResolve();
}
@Method()
async getInputElement(): Promise<HTMLInputElement> {
await this.componentReady;
return this.nativeInput!;
}
render() {
return (
<input ref={ref => (this.nativeInput = ref)} />
);
}
Steps to Reproduce
- See "Current Behavior" and/or reproduction
- Observe: In reproduction, alert will be presented when the element reference is undefined
Code Reproduction URL
https://stackblitz.com/edit/bpfynd?file=src%2Fmain.tsx
Ionic Info
N/A
Additional Information
No response
Prerequisites
Ionic Framework Version
v7.x
Current Behavior
Ionic Framework offers an API on certain form controls, such as on
ion-input, to access the HTML form element that is rendered inside the web component.This API is typically named
getInputElementand is useful when a developer needs access to theinput/textareaform control element to attach additional functionality.A key feature where this is used is in input masking, where libraries like Maskito need access to the form element to attach the input masking behavior.
When using Ionic's API, the element is not always initially available until after the component template renders. As a result, developers that try to access the method will return an element reference that is undefined. As a result, developers need to hack around this limitation by either waiting a period with
setTimeoutor usingrequestAnimationFrameto give enough time for the web component to render, prior to accessing the element reference. This is an extra implementation detail and not developer friendly.This is most prevalent when trying to access the element reference inside of the
refbinding in React.Expected Behavior
Since all public method APIs with Ionic's web components are asynchronous, Ionic should wait to resolve the element reference until after the web component has rendered.
For instance:
Steps to Reproduce
Code Reproduction URL
https://stackblitz.com/edit/bpfynd?file=src%2Fmain.tsx
Ionic Info
N/A
Additional Information
No response