Skip to content

Commit

Permalink
adjusted implementation according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Oct 8, 2024
1 parent 9fd5b46 commit 5c13980
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
31 changes: 22 additions & 9 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type PropNames<Props> = Array<PropName<Props>>

export interface R2WCOptions<Props> {
shadow?: "open" | "closed"
dispatchEvents?: EventInit
props?: PropNames<Props> | Partial<Record<PropName<Props>, R2WCType>>
events?: PropNames<Props> | Partial<Record<PropName<Props>, EventInit>>
}

export interface R2WCRenderer<Props, Context> {
Expand Down Expand Up @@ -46,12 +46,19 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
? (Object.keys(ReactComponent.propTypes) as PropNames<Props>)
: []
}
if (!options.events) {
options.events = []
}

const propNames = Array.isArray(options.props)
? options.props.slice()
: (Object.keys(options.props) as PropNames<Props>)
const eventNames = Array.isArray(options.events)
? options.events.slice()
: (Object.keys(options.events) as PropNames<Props>)

const propTypes = {} as Partial<Record<PropName<Props>, R2WCType>>
const eventParams = {} as Partial<Record<PropName<Props>, EventInit>>
const mapPropAttribute = {} as Record<PropName<Props>, string>
const mapAttributeProp = {} as Record<string, PropName<Props>>
for (const prop of propNames) {
Expand All @@ -64,6 +71,11 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
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() {
Expand Down Expand Up @@ -97,14 +109,15 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
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] }),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<theme-select-events></theme-select-events>"
Expand Down

0 comments on commit 5c13980

Please sign in to comment.