Skip to content

Commit

Permalink
add support for React-style camelCased event props
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Kim committed Feb 4, 2024
1 parent b1244a5 commit 9533847
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export const impl: Partial<RendererImpl<Node, string>> = {

break;
default: {
if (
name[0] === "o" &&
name[1] === "n" &&
name[2] === name[2].toUpperCase() &&
typeof value === "function"
) {
// Support React-style event names (onClick, onChange, etc.)
name = name.toLowerCase();
}

if (
name in node &&
// boolean properties will coerce strings, but sometimes they map to
Expand Down
11 changes: 11 additions & 0 deletions test/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ test("onevent", () => {
Assert.is(mock.callCount, 3);
});

test("onevent camelCased", () => {
const mock = Sinon.fake();
renderer.render(<button onClick={mock}>Click me</button>, document.body);

const button = document.body.firstChild as HTMLButtonElement;
button.click()!;
button.click()!;
button.click()!;
Assert.is(mock.callCount, 3);
});

test("onevent SVG", () => {
const mock = Sinon.fake();
renderer.render(<svg onclick={mock} />, document.body);
Expand Down

0 comments on commit 9533847

Please sign in to comment.