Skip to content

Commit

Permalink
fix: Fixed React named default-only slots when rendering dynamic non-…
Browse files Browse the repository at this point in the history
…paper components
  • Loading branch information
alexgrozav committed Mar 22, 2022
1 parent e5ffd0d commit a310d36
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/react/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ export const h: HoistFn<VNode, FC<any>> = (
*
* @example h(Component, {}, { slotName: () => children });
*/
if (typeof type === 'function' && typeof children[0] === 'object' && !Array.isArray(children[0]) && !(children[0] as any).$$typeof) {
children = Object.entries(children[0] as Record<string, () => VNode>)
.map(([rawSlotName, childrenFn]) => {
const slotName = capitalizeFirst(rawSlotName);
const slotComponent = (type as FC<any> & { [key: string]: any })[slotName];
if (typeof type !== 'string' && typeof children[0] === 'object' && !Array.isArray(children[0]) && !(children[0] as any).$$typeof) {
const slots = children[0] as Record<string, () => VNode>;
const slotKeys = Object.keys(slots);

return h(slotComponent, { key: slotName }, childrenFn());
});
if (slotKeys.length === 1 && slotKeys[0] === 'default') {
children = [slots.default()].flat();
} else {
children = slotKeys
.map((slotKey) => {
const slotName = capitalizeFirst(slotKey);
const slotComponent = (type as FC<any> & { [key: string]: any })[slotName];

return h(slotComponent, { key: slotName }, slots[slotKey]());
});
}
}

return createElement(type, props, ...children);
Expand Down

0 comments on commit a310d36

Please sign in to comment.