|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import React, { forwardRef } from 'react'; |
10 | | -import { SchemaNode, ComponentRegistry } from '@object-ui/core'; |
| 9 | +import React, { forwardRef, useContext, useMemo } from 'react'; |
| 10 | +import { SchemaNode, ComponentRegistry, ExpressionEvaluator } from '@object-ui/core'; |
| 11 | +import { SchemaRendererContext } from './context/SchemaRendererContext'; |
11 | 12 |
|
12 | 13 | export const SchemaRenderer = forwardRef<any, { schema: SchemaNode } & Record<string, any>>(({ schema, ...props }, _ref) => { |
13 | | - if (!schema) return null; |
| 14 | + const context = useContext(SchemaRendererContext); |
| 15 | + const dataSource = context?.dataSource || {}; |
| 16 | + |
| 17 | + // Evaluate schema expressions against the data source |
| 18 | + const evaluatedSchema = useMemo(() => { |
| 19 | + if (!schema || typeof schema === 'string') return schema; |
| 20 | + |
| 21 | + const evaluator = new ExpressionEvaluator({ data: dataSource }); |
| 22 | + const newSchema = { ...schema }; |
| 23 | + |
| 24 | + // Evaluate 'content' (common in Text, Button) |
| 25 | + if (typeof newSchema.content === 'string') { |
| 26 | + newSchema.content = evaluator.evaluate(newSchema.content); |
| 27 | + } |
| 28 | + |
| 29 | + // Evaluate 'props' |
| 30 | + if (newSchema.props) { |
| 31 | + const newProps = { ...newSchema.props }; |
| 32 | + for (const [key, val] of Object.entries(newProps)) { |
| 33 | + newProps[key] = evaluator.evaluate(val); |
| 34 | + } |
| 35 | + newSchema.props = newProps; |
| 36 | + } |
| 37 | + |
| 38 | + return newSchema; |
| 39 | + }, [schema, dataSource]); |
| 40 | + |
| 41 | + if (!evaluatedSchema) return null; |
14 | 42 | // If schema is just a string, render it as text |
15 | | - if (typeof schema === 'string') return <>{schema}</>; |
| 43 | + if (typeof evaluatedSchema === 'string') return <>{evaluatedSchema}</>; |
16 | 44 |
|
17 | | - const Component = ComponentRegistry.get(schema.type); |
| 45 | + const Component = ComponentRegistry.get(evaluatedSchema.type); |
18 | 46 |
|
19 | 47 | if (!Component) { |
20 | 48 | return ( |
21 | 49 | <div className="p-4 border border-red-500 rounded text-red-500 bg-red-50 my-2"> |
22 | | - Unknown component type: <strong>{schema.type}</strong> |
23 | | - <pre className="text-xs mt-2 overflow-auto">{JSON.stringify(schema, null, 2)}</pre> |
| 50 | + Unknown component type: <strong>{evaluatedSchema.type}</strong> |
| 51 | + <pre className="text-xs mt-2 overflow-auto">{JSON.stringify(evaluatedSchema, null, 2)}</pre> |
24 | 52 | </div> |
25 | 53 | ); |
26 | 54 | } |
27 | 55 |
|
28 | 56 | // Note: We don't forward the ref to the Component because components in the registry |
29 | 57 | // may not support refs. The SchemaRenderer itself can still receive refs for its own use. |
30 | 58 | return React.createElement(Component, { |
31 | | - schema, |
32 | | - ...(schema.props || {}), |
33 | | - className: schema.className, |
34 | | - 'data-obj-id': schema.id, |
35 | | - 'data-obj-type': schema.type, |
| 59 | + schema: evaluatedSchema, |
| 60 | + ...(evaluatedSchema.props || {}), |
| 61 | + className: evaluatedSchema.className, |
| 62 | + 'data-obj-id': evaluatedSchema.id, |
| 63 | + 'data-obj-type': evaluatedSchema.type, |
36 | 64 | ...props |
37 | 65 | }); |
38 | 66 | }); |
|
0 commit comments