Skip to content

Commit

Permalink
fix(runtime): call form-associated lifecycle callbacks w/ this (#5104)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alicewriteswrongs committed Nov 28, 2023
1 parent 57a6317 commit 1ac8aa3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/runtime/proxy-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export const proxyComponent = (
const elm = BUILD.lazyLoad ? hostRef.$hostElement$ : this;
const instance: d.ComponentInterface = BUILD.lazyLoad ? hostRef.$lazyInstance$ : elm;
if (!instance) {
hostRef.$onReadyPromise$.then((instance: d.ComponentInterface) => instance[cbName]?.(...args));
hostRef.$onReadyPromise$.then((instance: d.ComponentInterface) => {
const cb = instance[cbName];
typeof cb === 'function' && cb.call(instance, ...args);
});
} else {
const cb = instance[cbName];
typeof cb === 'function' && cb(...args);
typeof cb === 'function' && cb.call(instance, ...args);
}
},
}),
Expand Down
3 changes: 3 additions & 0 deletions test/karma/test-app/form-associated/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class FormAssociatedCmp {

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

render() {
Expand Down

0 comments on commit 1ac8aa3

Please sign in to comment.