|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { ComponentRegistry } from '@object-ui/core'; |
| 10 | +import type { DashboardSchema } from '@object-ui/types'; |
| 11 | +import { SchemaRenderer } from '@object-ui/react'; |
| 12 | +import { forwardRef } from 'react'; |
| 13 | +import { cn } from '../../lib/utils'; |
| 14 | + |
| 15 | +const DashboardRenderer = forwardRef<HTMLDivElement, { schema: DashboardSchema; className?: string; [key: string]: any }>( |
| 16 | + ({ schema, className, ...props }, ref) => { |
| 17 | + const columns = schema.columns || 3; |
| 18 | + const gap = schema.gap || 4; |
| 19 | + |
| 20 | + return ( |
| 21 | + <div |
| 22 | + ref={ref} |
| 23 | + className={cn("grid", className)} |
| 24 | + style={{ |
| 25 | + gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`, |
| 26 | + gap: gap * 4 // Assuming tailwind scale |
| 27 | + }} |
| 28 | + {...props} |
| 29 | + > |
| 30 | + {schema.widgets?.map((widget) => ( |
| 31 | + <div |
| 32 | + key={widget.id} |
| 33 | + className={cn("border rounded-lg p-4 bg-card text-card-foreground shadow-sm")} |
| 34 | + style={widget.layout ? { |
| 35 | + gridColumn: `span ${widget.layout.w}`, |
| 36 | + gridRow: `span ${widget.layout.h}` |
| 37 | + }: undefined} |
| 38 | + > |
| 39 | + {widget.title && <h3 className="font-semibold mb-2">{widget.title}</h3>} |
| 40 | + <SchemaRenderer schema={widget.component} /> |
| 41 | + </div> |
| 42 | + ))} |
| 43 | + </div> |
| 44 | + ); |
| 45 | + } |
| 46 | +); |
| 47 | + |
| 48 | +ComponentRegistry.register( |
| 49 | + 'dashboard', |
| 50 | + DashboardRenderer, |
| 51 | + { |
| 52 | + label: 'Dashboard', |
| 53 | + category: 'Complex', |
| 54 | + icon: 'layout-dashboard', |
| 55 | + inputs: [ |
| 56 | + { name: 'columns', type: 'number', label: 'Columns', defaultValue: 3 }, |
| 57 | + { name: 'gap', type: 'number', label: 'Gap', defaultValue: 4 }, |
| 58 | + { name: 'className', type: 'string', label: 'CSS Class' } |
| 59 | + ], |
| 60 | + defaultProps: { |
| 61 | + columns: 3, |
| 62 | + widgets: [] |
| 63 | + } |
| 64 | + } |
| 65 | +); |
0 commit comments