|
16 | 16 |
|
17 | 17 | import {LitElement, nothing} from 'lit'; |
18 | 18 | import {property} from 'lit/decorators.js'; |
19 | | -import {ComponentContext, ComponentApi} from '@a2ui/web_core/v0_9'; |
| 19 | +import {ComponentContext, ComponentApi, type ComponentId} from '@a2ui/web_core/v0_9'; |
20 | 20 | import {renderA2uiNode} from './surface/render-a2ui-node.js'; |
21 | 21 | import {A2uiController} from '@a2ui/lit/v0_9'; |
22 | 22 |
|
23 | 23 | /** |
24 | | - * Represents a reference to a child component that should be rendered. |
25 | | - * In A2UI, a child can be provided in one of three ways: |
26 | | - * |
27 | | - * 1. A string ID (e.g., "submit_button"). Tells the renderer to look up the component |
28 | | - * from the Surface's registry. The child will inherit the parent's data context path. |
29 | | - * 2. A reference object (e.g., { id: 'foo', basePath: '/bar' }). Tells the renderer where |
30 | | - * to find the component AND binds it to a specific slice of the data model. |
31 | | - * 3. An inline component object (e.g., { type: 'Button', props: { ... } }). Provides the |
32 | | - * full component definition directly instead of looking it up by ID. |
33 | | - * |
34 | | - * (This probably should come from the binder in web_core!) |
| 24 | + * A reference to a child component to render. Either a string ID, or an object |
| 25 | + * pairing an ID with an explicit data context path. |
35 | 26 | */ |
36 | | -type A2uiChildRef = string | {id?: string; basePath?: string; type?: string}; |
| 27 | +type A2uiChildRef = ComponentId | {id: ComponentId; basePath: string}; |
37 | 28 |
|
38 | 29 | /** |
39 | 30 | * A base class for A2UI Lit elements that manages the A2uiController lifecycle. |
@@ -63,35 +54,33 @@ export abstract class A2uiLitElement<Api extends ComponentApi> extends LitElemen |
63 | 54 | * Helper method to render a child A2UI node. |
64 | 55 | * Abstracts away the need to manually create a ComponentContext. |
65 | 56 | * |
66 | | - * @param childRef The reference to the child component to render. Can be a string ID, |
67 | | - * a reference object containing `{ id, basePath }`, or a full inline component definition. |
| 57 | + * @param childRef The reference to the child component to render. Either a string ID |
| 58 | + * or a reference object containing `{id, basePath}`. |
68 | 59 | * @param customPath An explicit data model path to bind the child to. If provided, |
69 | | - * this completely overrides any path defined in the `childRef` object. |
70 | | - * If omitted, it falls back to the `childRef`'s `basePath`, or the current component's path. |
| 60 | + * this overrides any path defined in the `childRef` object. If omitted, |
| 61 | + * falls back to the `childRef`'s `basePath`, or the current component's path. |
71 | 62 | * |
72 | 63 | * @returns A Lit template result containing the rendered child component, or `nothing` if the reference is empty. |
73 | 64 | */ |
74 | 65 | protected renderNode(childRef?: A2uiChildRef, customPath?: string) { |
75 | 66 | if (!childRef) return nothing; |
76 | | - let model: any = childRef; |
77 | 67 | const {surface, path: parentPath} = this.context.dataContext; |
78 | 68 |
|
79 | | - // Path is resolved in the following order: |
80 | | - // customPath > childRef.basePath > parentPath |
| 69 | + // Path resolution order: customPath > childRef.basePath > parentPath |
| 70 | + let componentId: ComponentId; |
81 | 71 | let path = customPath; |
82 | | - |
83 | | - // We check !childRef.type because an inline component definition (e.g. { type: 'Button' }) |
84 | | - // should be passed directly to the ComponentContext. If it doesn't have a .type, |
85 | | - // we treat it as a child reference object (e.g. { id: 'foo', basePath: '/bar' }). |
86 | | - if (typeof childRef === 'object' && childRef !== null && childRef.id && !childRef.type) { |
87 | | - model = childRef.id; |
| 72 | + if (typeof childRef === 'object') { |
| 73 | + componentId = childRef.id; |
88 | 74 | path = path ?? childRef.basePath; |
| 75 | + } else { |
| 76 | + componentId = childRef; |
89 | 77 | } |
90 | 78 |
|
91 | | - // Fallback to the current component's context. |
| 79 | + // Keep this fallback because the previous A2uiChildRef type allowed object |
| 80 | + // refs without a basePath. |
92 | 81 | path = path ?? parentPath; |
93 | 82 |
|
94 | | - return renderA2uiNode(new ComponentContext(surface, model, path), surface.catalog); |
| 83 | + return renderA2uiNode(new ComponentContext(surface, componentId, path), surface.catalog); |
95 | 84 | } |
96 | 85 |
|
97 | 86 | /** |
|
0 commit comments