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

bug: getInputElement does not wait for element to be rendered #28283

Closed
3 tasks done
sean-perkins opened this issue Oct 4, 2023 · 3 comments · Fixed by #28362
Closed
3 tasks done

bug: getInputElement does not wait for element to be rendered #28283

sean-perkins opened this issue Oct 4, 2023 · 3 comments · Fixed by #28362
Labels
package: core @ionic/core package type: bug a confirmed bug report

Comments

@sean-perkins
Copy link
Contributor

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

  1. See "Current Behavior" and/or reproduction
  2. 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

@thetaPC
Copy link
Contributor

thetaPC commented Oct 16, 2023

Thank you for submitting the issue! There's a dev build with a fix:
npm install @ionic/[email protected] @ionic/[email protected]

Feel free to test it out.

github-merge-queue bot pushed a commit that referenced this issue Oct 23, 2023
…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`
@thetaPC
Copy link
Contributor

thetaPC commented Oct 23, 2023

Thanks for the issue! This has been resolved via #28362 and will be available in an upcoming release of Ionic.

sean-perkins pushed a commit that referenced this issue Oct 27, 2023
…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`
Copy link

ionitron-bot bot commented Nov 22, 2023

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: core @ionic/core package type: bug a confirmed bug report
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants