Skip to content

Commit

Permalink
feat(core): ⚡ add CoreComponentRenderer
Browse files Browse the repository at this point in the history
changes to render CoreComponents

ref: #54
  • Loading branch information
anantakumarghosh committed Oct 1, 2024
1 parent 740ed76 commit f3c4535
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { CoreComponentsRegistry } from "@wrappid/core";

const CoreComponentRenderer = ({ componentData }) => {
const renderCoreComponent = (data) => {
if (!data || typeof data !== 'object') {
return null;
}

const ComponentInfo = CoreComponentsRegistry[data.component];
const ComponentToRender = ComponentInfo?.comp;

if (!ComponentToRender) {
console.warn(`Component ${data.component} not found in CoreComponentsRegistry`);
return null;
}

const childrenElements = (data.children || []).map((child, index) =>
renderCoreComponent({ ...child, key: index })
);

// Add default Props

// Add default props for specific components
let defaultProps = { };
if (data.component === 'CoreIcon') {
defaultProps = { ...defaultProps, icon: 'star' }; // Example default icon
} else if (data.component === 'CoreAvatar') {
defaultProps = { ...defaultProps, alt: 'User' };
}
// Add more component-specific default props as needed

return React.createElement(
ComponentToRender,
{
key: data.key,
...defaultProps,
// Pass props here
},
childrenElements.length > 0 ? childrenElements : null
);
};

return renderCoreComponent(componentData);
};

export default CoreComponentRenderer;

0 comments on commit f3c4535

Please sign in to comment.