Skip to content

Error Generating React Wrappers with Custom Event Types #178

@binlabs

Description

@binlabs

Hey Burton, I'm trying to generate React wrappers using customElementReactWrapperPlugin for my component that has events which extend from the native event class. I was trying to base my custom event types on the Truly Custom Events article you wrote on dev.to.

The code for a minimal reproduction is shown below, or you can see it in-full here:

// my-component/events.ts
export class SearchCancelEvent extends Event {
  constructor() {
    super('universal-search-cancel', {
      bubbles: true,
      composed: true,
    });
  }
}

// my-component/my-component.ts
import { html, LitElement } from "lit";
import { SearchCancelEvent } from "./events";

export class MyComponent extends LitElement {
  override firstUpdated() {
    this.dispatchEvent(new SearchCancelEvent());
  }
  override render() {
    return html``;
  }
}

Unfortunately, when running the repro on StackBlitz, analyzer just hangs and never finishes and can't seem to even be cancelled with CTRL+C, so if you want to see the error you'll probably have to clone it locally.

Here is the error that occurs when running the analyzer:

TypeError: Cannot read properties of undefined (reading 'replace')
    at toPascalCase (file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:131:16)
    at createEventName (file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:161:39)
    at file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:561:18
    at Array.map (<anonymous>)
    at getEventNames (file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:558:29)
    at file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:471:20
    at Array.forEach (<anonymous>)
    at generateReactWrappers (file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:470:14)
    at packageLinkPhase (file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/custom-element-react-wrappers/dist/index.js:831:7)
    at file:///Users/my-username/Development/Personal/cem-react-wrapper-event-error/node_modules/@custom-elements-manifest/analyzer/src/create.js:80:25

Here is what the wrappers plugin is seeing from the manifest when it's trying get the event names by mapping over component.events:

{
  kind: "class",
  description: "",
  name: "MyComponent",
  members: [
  ],
  events: [
    {
      type: {
        text: "SearchCancelEvent",
      },
    },
  ],
  superclass: {
    name: "LitElement",
    package: "lit",
  },
  tagName: "my-component",
  customElement: true,
  modulePath: "src/components/my-component/my-component.ts",
  definitionPath: "src/components/my-component/index.ts",
}

Because the event doesn't end up with a name property, it errors out in toPascalCase().

I'll probably try switching over to CustomEvent for the time being, unless you are able to see something I've done wrong.

Let me know if there's anything I can do to help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions