Skip to content

Commit

Permalink
Partial props support + Fix for property setter not re-rendering (bit…
Browse files Browse the repository at this point in the history
…ovi#148)

This allows not mapping all props as attributes
This fixes an issue where properties not mapped as attributes didn't cause a re-render when set.

---------

Co-authored-by: Christopher J Baker <[email protected]>
  • Loading branch information
vsilvar and Christopher J Baker authored Nov 19, 2023
1 parent 5d1bc1f commit 002a007
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PropNames<Props> = Array<PropName<Props>>

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

export interface R2WCRenderer<Props, Context> {
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
? options.props.slice()
: (Object.keys(options.props) as PropNames<Props>)

const propTypes = {} as Record<PropName<Props>, R2WCType>
const propTypes = {} as Partial<Record<PropName<Props>, R2WCType>>
const mapPropAttribute = {} as Record<PropName<Props>, string>
const mapAttributeProp = {} as Record<string, PropName<Props>>
for (const prop of propNames) {
Expand Down Expand Up @@ -93,7 +93,7 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
const attribute = mapPropAttribute[prop]
const value = this.getAttribute(attribute)
const type = propTypes[prop]
const transform = transforms[type]
const transform = type ? transforms[type] : null

if (transform?.parse && value) {
//@ts-ignore
Expand Down Expand Up @@ -123,7 +123,7 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
) {
const prop = mapAttributeProp[attribute]
const type = propTypes[prop]
const transform = transforms[type]
const transform = type ? transforms[type] : null

if (prop in propTypes && transform?.parse && value) {
//@ts-ignore
Expand Down Expand Up @@ -161,7 +161,7 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
set(value) {
this[propsSymbol][prop] = value

const transform = transforms[type]
const transform = type ? transforms[type] : null
if (transform?.stringify) {
//@ts-ignore
const attributeValue = transform.stringify(value, attribute, this)
Expand All @@ -170,6 +170,8 @@ export default function r2wc<Props extends R2WCBaseProps, Context>(
if (oldAttributeValue !== attributeValue) {
this.setAttribute(attribute, attributeValue)
}
} else {
this[renderSymbol]()
}
},
})
Expand Down

0 comments on commit 002a007

Please sign in to comment.