Skip to content

Commit

Permalink
[labs/react] Do not override attribtues with event listeners (#3067)
Browse files Browse the repository at this point in the history
* faster types, clearer types

* faster build that builds

* faster builds actually builds

* reduce type complexity, remove events

* more atomic typing, default event object

* remove unnecessary type

* move static non-generated types to top of doc

* add changeset

* remove extra react import

* use Events as base type

* put types back

* reduce to smallest change

* add test to catch unmapped event errors

* better changeset message

* more straightforward names

* mention compiling TS in changeset

* comment to explain test
  • Loading branch information
taylor-vann committed Jul 11, 2022
1 parent 7d185b4 commit f3e3cdd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-cherries-enjoy.md
@@ -0,0 +1,5 @@
---
'@lit-labs/react': patch
---

Fixed an error that occurs when when compiling TS. The error occurs when createComponent() is not provided an event map causing instance properties to be confused with event handlers.
2 changes: 1 addition & 1 deletion packages/labs/react/src/create-component.ts
Expand Up @@ -127,7 +127,7 @@ type EventProps<R extends Events> = {
* messages. Default value is inferred from the name of custom element class
* registered via `customElements.define`.
*/
export const createComponent = <I extends HTMLElement, E extends Events>(
export const createComponent = <I extends HTMLElement, E extends Events = {}>(
React: typeof ReactModule,
tagName: string,
elementClass: Constructor<I>,
Expand Down
23 changes: 23 additions & 0 deletions packages/labs/react/src/test/create-component_test.tsx
Expand Up @@ -93,6 +93,29 @@ suite('createComponent', () => {
await el.updateComplete;
};

/*
The following test will not build if an incorrect typing occurs
when events are not provided to `createComponent`.
*/
test('renders element without optional event map', async () => {
const ComponentWithoutEventMap = createComponent(
window.React,
elementName,
BasicElement,
);

const name = 'Component without event map.';
window.ReactDOM.render(
<ComponentWithoutEventMap>{name}</ComponentWithoutEventMap>,
container
);

el = container.querySelector(elementName)! as BasicElement;
await el.updateComplete;

assert.equal(el.textContent, 'Component without event map.');
});

test('works with text children', async () => {
const name = 'World';
window.ReactDOM.render(
Expand Down

0 comments on commit f3e3cdd

Please sign in to comment.