Skip to content

Commit

Permalink
fix: improve creation of react component (#2337)
Browse files Browse the repository at this point in the history
- Avoid setting element property if already set as attribute.
- Always set id as attribute
  • Loading branch information
jeripeierSBB authored Jan 15, 2024
1 parent 654bda5 commit a64a3eb
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/react/core/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ export interface Options<I extends HTMLElement, E extends EventNames = {}> {

type Constructor<T> = { new (): T };

const reservedReactProperties = new Set(['children', 'localName', 'ref', 'style', 'className']);
const reservedReactProperties = new Set([
'children',
'localName',
'ref',
'style',
'className',
'id',
]);

const listenedEvents = new WeakMap<Element, Map<string, EventListenerObject>>();

Expand Down Expand Up @@ -259,19 +266,15 @@ export const createComponent = <I extends HTMLElement, E extends EventNames = {}
// React does *not* handle `className` for custom elements so
// 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) {
const elementProperty = elementProperties?.get(k);
const toAttribute = (elementProperty?.converter as ComplexAttributeConverter)?.toAttribute;
if (
!elementProperty ||
elementProperty.attribute === false ||
(typeof v === 'object' && !toAttribute)
) {
elementProps[k] = v;
continue;
}

Expand All @@ -281,6 +284,8 @@ export const createComponent = <I extends HTMLElement, E extends EventNames = {}
reactProps[attributeName] = toAttribute ? toAttribute(v) : v;
} else if (v) {
reactProps[attributeName] = '';
} else {
elementProps[k] = v;
}
continue;
}
Expand Down

0 comments on commit a64a3eb

Please sign in to comment.