Skip to content

Commit fee3c1e

Browse files
authored
fix: event handler binding in fast-html (#7124)
<!--- Thanks for filing a pull request 😄 ! Before you submit, please read the following: Search open/closed issues before submitting. Someone may have pushed the same thing before! Provide a summary of your changes in the title field above. --> ## 📖 Description <!--- Provide some background and a description of your work. What problem does this change solve? Is this a breaking change, chore, fix, feature, etc? --> ### 🎫 Issues Currently, if the event handler is defined as a method (`method() {...} `)instead of a property (`method = () => {...}`), the event handler will not be bound to the correct `this` context, hence `this` inside the event handler would be `undefined`. ## 📑 Test Plan Test added. ## ✅ Checklist ### General <!--- Review the list and put an x in the boxes that apply. --> - [x] I have included a change request file using `$ npm run change` - [x] I have added tests for my changes. - [x] I have tested my changes. - [ ] I have updated the project documentation to reflect my changes. - [x] I have read the [CONTRIBUTING](https://github.com/microsoft/fast/blob/main/CONTRIBUTING.md) documentation and followed the [standards](https://github.com/microsoft/fast/blob/main/CODE_OF_CONDUCT.md#our-standards) for this project.
1 parent 8a25911 commit fee3c1e

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "fix: event handler binding",
4+
"packageName": "@microsoft/fast-html",
5+
"email": "[email protected]",
6+
"dependentChangeType": "none"
7+
}

packages/web-components/fast-html/src/components/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class TemplateElement extends FASTElement {
333333
closingParenthesis
334334
);
335335
const binding = (x: any, c: any) =>
336-
pathResolver(propName, self)(x, c)(
336+
pathResolver(propName, self)(x, c).bind(x)(
337337
...(arg === "e" ? [c.event] : []),
338338
...(arg !== "e" && arg !== ""
339339
? [pathResolver(arg)(x, c)]

packages/web-components/fast-html/src/fixtures/event/event.fixture.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<button @click="{handleNoArgsClick()}">No arguments</button>
1818
<button @click="{handleEventArgClick(e)}">event argument</button>
1919
<button @click="{handleAttributeArgClick(foo)}">attribute argument</button>
20+
<button @click="{handleModifyAttributeClick()}">modify foo</button>
2021
</template>
2122
</f-template>
2223
<script type="module" src="/event/main.js"></script>

packages/web-components/fast-html/src/fixtures/event/event.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,15 @@ test.describe("f-template", async () => {
3737

3838
expect(message).toEqual("bar");
3939
});
40+
test("should properly bind events with `this`", async ({ page }) => {
41+
await page.goto("/event");
42+
43+
const customElement = await page.locator("test-element");
44+
45+
await expect(customElement).toHaveJSProperty("foo", "bar");
46+
47+
await customElement.locator("button").nth(3).click();
48+
49+
await expect(customElement).toHaveJSProperty("foo", "modified-by-click");
50+
});
4051
});

packages/web-components/fast-html/src/fixtures/event/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class TestElement extends FASTElement {
1616
public handleAttributeArgClick = (foo: string): void => {
1717
console.log(foo);
1818
};
19+
20+
public handleModifyAttributeClick() {
21+
this.foo = "modified-by-click";
22+
}
1923
}
2024
TestElement.define({
2125
name: "test-element",

0 commit comments

Comments
 (0)