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: can't access this in form-associated callbacks #5106

Closed
3 tasks done
alicewriteswrongs opened this issue Nov 27, 2023 · 1 comment · Fixed by #5104
Closed
3 tasks done

bug: can't access this in form-associated callbacks #5106

alicewriteswrongs opened this issue Nov 27, 2023 · 1 comment · Fixed by #5104
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil

Comments

@alicewriteswrongs
Copy link
Contributor

alicewriteswrongs commented Nov 27, 2023

Prerequisites

Stencil Version

4.8.0

Current Behavior

In #4939 we added support for form-associated custom element lifecycle callbacks like formAssociatedCallback, however the way they're currently being called doesn't resolve the correct this value to ensure that properties on a Stencil component (e.g. ones decorated with @State) can be resolved properly.

So a little component like this one won't work correctly 😢:

import { Component, h, State } from '@stencil/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
  formAssociated: true,
})
export class MyComponent {
  @State() showWords: boolean = false;

  formAssociatedCallback(form: any) {
    console.log('formAssociated called');
    console.log(this.showWords);
  }

  render() {
    return <div>Hello, World!</div>;
  }
}

Expected Behavior

If I try to access this in a method as in the example it should work correctly.

System Info

No response

Steps to Reproduce

In any of the form-associated callbacks (e.g. formAssociatedCallback) try to access something on this, like a @State property. You should get an error that it's not found (cannot read property 'so and so' of undefined). See the attached repro case.

Code Reproduction URL

https://github.com/alicewriteswrongs/stencil-fa-cb-repro

Additional Information

No response

@alicewriteswrongs alicewriteswrongs added the Bug: Validated This PR or Issue is verified to be a bug within Stencil label Nov 27, 2023
github-merge-queue bot pushed a commit that referenced this issue Nov 28, 2023
This fixes an issue where form-associated lifecycle callbacks were not
being called with the correct `this` value, making it impossible to set
something like a `@State` value from within a callback.

fixes #5106
@tanner-reits
Copy link
Contributor

The fix for this was included as a part of today's v4.8.1 release!

alicewriteswrongs added a commit that referenced this issue Mar 14, 2024
If you need to write a wdio test which references a host element
interface the test will not compile correctly, complaining that it can't
resolve the interface. This problem was revealed while working on
https://github.com/ionic-team/stencil/compare/ap/wdio/form-associated,
where the component has a method like this:

```tsx
  formAssociatedCallback(form: HTMLFormAssociatedElement) {
    form.ariaLabel = 'formAssociated called';
    // this is a regression test for #5106 which ensures that `this` is
    // resolved correctly
    this.internals.setValidity({});
  }
```

The `HTMLFormAssociatedElement` interface is defined in
`test/wdio/src/components.d.ts` and should be part of the global type
namespace for the `test/wdio` project, but our `include/` in
`test/wdio/tsconfig-stencil.json` wasn't including the component types
correctly so that the interface wasn't being resolved.
github-merge-queue bot pushed a commit that referenced this issue Mar 14, 2024
…5470)

If you need to write a wdio test which references a host element
interface the test will not compile correctly, complaining that it can't
resolve the interface. This problem was revealed while working on
https://github.com/ionic-team/stencil/compare/ap/wdio/form-associated,
where the component has a method like this:

```tsx
  formAssociatedCallback(form: HTMLFormAssociatedElement) {
    form.ariaLabel = 'formAssociated called';
    // this is a regression test for #5106 which ensures that `this` is
    // resolved correctly
    this.internals.setValidity({});
  }
```

The `HTMLFormAssociatedElement` interface is defined in
`test/wdio/src/components.d.ts` and should be part of the global type
namespace for the `test/wdio` project, but our `include/` in
`test/wdio/tsconfig-stencil.json` wasn't including the component types
correctly so that the interface wasn't being resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants