'use client'; import { type ComponentRenderProps } from '@json-render/react'; import { useData } from '@json-render/react'; import { getByPath } from '@json-render/core'; export function Table({ element }: ComponentRenderProps) { const { title, dataPath, columns } = element.props as { title?: string | null; dataPath: string; columns: Array<{ key: string; label: string; format?: string | null }>; }; const { data } = useData(); const tableData = getByPath(data, dataPath) as Array> | undefined; if (!tableData || !Array.isArray(tableData)) { return
No data
; } const formatCell = (value: unknown, format?: string | null) => { if (value === null || value === undefined) return '-'; if (format === 'currency' && typeof value === 'number') { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value); } if (format === 'date' && typeof value === 'string') { return new Date(value).toLocaleDateString(); } if (format === 'badge') { return ( {String(value)} ); } return String(value); }; return (
{title &&

{title}

} {columns.map((col) => ( ))} {tableData.map((row, i) => ( {columns.map((col) => ( ))} ))}
{col.label}
{formatCell(row[col.key], col.format)}
); }