Skip to content

Commit

Permalink
fix: avoid setting props in any case
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Apr 24, 2024
1 parent b8c0608 commit a412b1d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/react/src/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type WebComponentProps<I extends HTMLElement> = React.DetailedHTMLProps<
*/
export type ReactWebComponent<
I extends HTMLElement,
E extends EventNames = {},
E extends EventNames = {}
> = React.ForwardRefExoticComponent<
// TODO(augustjk): Remove and use `React.PropsWithoutRef` when
// https://github.com/preactjs/preact/issues/4124 is fixed.
Expand Down Expand Up @@ -227,7 +227,7 @@ const setProperty = <E extends Element>(
*/
export const createComponent = <
I extends HTMLElement,
E extends EventNames = {},
E extends EventNames = {}
>({
react: React,
tagName,
Expand Down Expand Up @@ -262,6 +262,7 @@ export const createComponent = <

// Props to be passed to React.createElement
const reactProps: Record<string, unknown> = {};
// Props to be set on element with setProperty
const elementProps: Record<string, unknown> = {};
// A map of element properties with meta information.
const elementProperties: Map<PropertyKey, PropertyDeclaration> | undefined =
Expand All @@ -273,11 +274,9 @@ export const createComponent = <
// coerce it to `class` so it's handled correctly.
reactProps[k === 'className' ? 'class' : k] = v;
continue;
}

if (eventProps.has(k) || k in elementClass.prototype) {
elementProps[k] = v;
} else if (eventProps.has(k) || k in elementClass.prototype) {
if (!renderAttributesOnCreate) {
elementProps[k] = v;
continue;
}

Expand All @@ -293,6 +292,7 @@ export const createComponent = <
elementProperty.attribute === false ||
(typeof v === 'object' && !toAttribute)
) {
elementProps[k] = v;
continue;
}

Expand All @@ -307,6 +307,11 @@ export const createComponent = <
} else if (v) {
// Only render boolean properties/attributes if they evaluate to true.
reactProps[attributeName] = '';
// Some boolean props like `checked` don't react to mutations after user interaction.
// To ensure expectation from React/JSX, we set also the corresponding property in this case.
elementProps[k] = v;
} else {
elementProps[k] = v;
}
continue;
}
Expand Down

0 comments on commit a412b1d

Please sign in to comment.