-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
bug: getInputElement does not wait for element to be rendered #28283
Comments
Thank you for submitting the issue! There's a dev build with a fix: Feel free to test it out. |
…le (#28362) Issue number: resolves #28283 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> `getInputElement()` is used to access the native input. If the component has yet to render, then the function will return `undefined`. This happens mostly when using `ref` on React. ```tsx <IonInput ref={async input => { const nativeInput = await input.getInputElement(); // nativeInput is undefined }} /> ``` ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - `getInputElement()` will wait to return once the component is ready. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `7.5.1-dev.11697488622.175c9183`
Thanks for the issue! This has been resolved via #28362 and will be available in an upcoming release of Ionic. |
…le (#28362) Issue number: resolves #28283 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> `getInputElement()` is used to access the native input. If the component has yet to render, then the function will return `undefined`. This happens mostly when using `ref` on React. ```tsx <IonInput ref={async input => { const nativeInput = await input.getInputElement(); // nativeInput is undefined }} /> ``` ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - `getInputElement()` will wait to return once the component is ready. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `7.5.1-dev.11697488622.175c9183`
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
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 theinput
/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 usingrequestAnimationFrame
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.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
The text was updated successfully, but these errors were encountered: