From 5c13980412de0595a6b90f72ea91beba2050f34a Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Wed, 9 Oct 2024 01:43:45 +0300 Subject: [PATCH] adjusted implementation according to code review --- packages/core/src/core.ts | 31 +++++++++++++------ .../src/react-to-web-component.test.tsx | 3 +- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index a305d7c..8f144af 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -6,8 +6,8 @@ type PropNames = Array> export interface R2WCOptions { shadow?: "open" | "closed" - dispatchEvents?: EventInit props?: PropNames | Partial, R2WCType>> + events?: PropNames | Partial, EventInit>> } export interface R2WCRenderer { @@ -46,12 +46,19 @@ export default function r2wc( ? (Object.keys(ReactComponent.propTypes) as PropNames) : [] } + if (!options.events) { + options.events = [] + } const propNames = Array.isArray(options.props) ? options.props.slice() : (Object.keys(options.props) as PropNames) + const eventNames = Array.isArray(options.events) + ? options.events.slice() + : (Object.keys(options.events) as PropNames) const propTypes = {} as Partial, R2WCType>> + const eventParams = {} as Partial, EventInit>> const mapPropAttribute = {} as Record, string> const mapAttributeProp = {} as Record> for (const prop of propNames) { @@ -64,6 +71,11 @@ export default function r2wc( mapPropAttribute[prop] = attribute mapAttributeProp[attribute] = prop } + for (const event of eventNames) { + eventParams[event] = Array.isArray(options.events) + ? {} + : options.events[event] + } class ReactWebComponent extends HTMLElement { static get observedAttributes() { @@ -97,14 +109,15 @@ export default function r2wc( if (transform?.parse && value) { //@ts-ignore this[propsSymbol][prop] = transform.parse(value, attribute, this) - } else if (options.dispatchEvents && type === "function") { - //@ts-ignore - this[propsSymbol][prop] = (detail) => { - const name = prop.replace(/^on/, "").toLowerCase() - this.dispatchEvent( - new CustomEvent(name, { detail, ...options.dispatchEvents }), - ) - } + } + } + for (const event of eventNames) { + //@ts-ignore + this[propsSymbol][event] = (detail) => { + const name = event.replace(/^on/, "").toLowerCase() + this.dispatchEvent( + new CustomEvent(name, { detail, ...eventParams[event] }), + ) } } } diff --git a/packages/react-to-web-component/src/react-to-web-component.test.tsx b/packages/react-to-web-component/src/react-to-web-component.test.tsx index 03047d5..4d89e7e 100644 --- a/packages/react-to-web-component/src/react-to-web-component.test.tsx +++ b/packages/react-to-web-component/src/react-to-web-component.test.tsx @@ -340,8 +340,7 @@ describe("react-to-web-component 1", () => { } const WebThemeSelect = r2wc(ThemeSelect, { - props: { onSelect: "function" }, - dispatchEvents: { bubbles: true }, + events: { onSelect: { bubbles: true } }, }) customElements.define("theme-select-events", WebThemeSelect) document.body.innerHTML = ""