From f3c453530cdca975d0cdbc8f819f76ac48a72748 Mon Sep 17 00:00:00 2001 From: anantakumarghosh Date: Tue, 1 Oct 2024 13:24:14 +0530 Subject: [PATCH] feat(core): :zap: add CoreComponentRenderer changes to render CoreComponents ref: #54 --- .../ReactComponentRenderer.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 app/components/page-builder/content-components/ReactComponentRenderer.js diff --git a/app/components/page-builder/content-components/ReactComponentRenderer.js b/app/components/page-builder/content-components/ReactComponentRenderer.js new file mode 100644 index 0000000..650459d --- /dev/null +++ b/app/components/page-builder/content-components/ReactComponentRenderer.js @@ -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; \ No newline at end of file