Skip to content

Commit 21737dc

Browse files
authored
refactor(react, lit): tighten ChildList any/unknown types (#1510)
1 parent 6c5aa4b commit 21737dc

7 files changed

Lines changed: 61 additions & 36 deletions

File tree

renderers/lit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
- (v0_9) Tighten resolved child list types in the basic catalog layout components.
34
- (v0_9) Narrow `A2uiChildRef` to the supported child reference shapes used by
45
`renderNode`.
56

renderers/lit/src/v0_9/catalogs/basic/basic-catalog-a2ui-lit-element.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {ComponentApi} from '@a2ui/web_core/v0_9';
17+
import {ComponentApi, type ComponentId} from '@a2ui/web_core/v0_9';
1818
import {A2uiLitElement} from '../../a2ui-lit-element.js';
1919
import {injectBasicCatalogStyles, computeColorVariant} from '@a2ui/web_core/v0_9/basic_catalog';
2020

21+
export type ResolvedChildRef =
22+
| ComponentId
23+
| {
24+
id: ComponentId;
25+
basePath: string;
26+
};
27+
28+
export type ResolvedChildList = ResolvedChildRef[];
29+
2130
/**
2231
* A base class for A2UI basic catalog components.
2332
*

renderers/lit/src/v0_9/catalogs/basic/components/Column.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import {html, nothing, css, PropertyValues} from 'lit';
1818
import {customElement} from 'lit/decorators.js';
1919
import {map} from 'lit/directives/map.js';
2020
import {ColumnApi} from '@a2ui/web_core/v0_9/basic_catalog';
21-
import {BasicCatalogA2uiLitElement} from '../basic-catalog-a2ui-lit-element.js';
21+
import {
22+
BasicCatalogA2uiLitElement,
23+
type ResolvedChildList,
24+
} from '../basic-catalog-a2ui-lit-element.js';
2225
import {A2uiController} from '@a2ui/lit/v0_9';
2326

2427
const JUSTIFY_MAP: Record<string, string> = {
@@ -71,9 +74,9 @@ export class A2uiBasicColumnElement extends BasicCatalogA2uiLitElement<typeof Co
7174
const props = this.controller.props;
7275
if (!props) return nothing;
7376

74-
const children = Array.isArray(props.children) ? props.children : [];
77+
const children: ResolvedChildList = Array.isArray(props.children) ? props.children : [];
7578

76-
return html` ${map(children, (child: any) => html`${this.renderNode(child)}`)} `;
79+
return html` ${map(children, child => html`${this.renderNode(child)}`)} `;
7780
}
7881
}
7982

renderers/lit/src/v0_9/catalogs/basic/components/List.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import {html, nothing, css, PropertyValues} from 'lit';
1818
import {customElement} from 'lit/decorators.js';
1919
import {map} from 'lit/directives/map.js';
2020
import {ListApi} from '@a2ui/web_core/v0_9/basic_catalog';
21-
import {BasicCatalogA2uiLitElement} from '../basic-catalog-a2ui-lit-element.js';
21+
import {
22+
BasicCatalogA2uiLitElement,
23+
type ResolvedChildList,
24+
} from '../basic-catalog-a2ui-lit-element.js';
2225
import {A2uiController} from '@a2ui/lit/v0_9';
2326

2427
@customElement('a2ui-list')
@@ -48,8 +51,8 @@ export class A2uiListElement extends BasicCatalogA2uiLitElement<typeof ListApi>
4851
const props = this.controller.props;
4952
if (!props) return nothing;
5053

51-
const children = Array.isArray(props.children) ? props.children : [];
52-
return html`${map(children, (child: any) => html`${this.renderNode(child)}`)}`;
54+
const children: ResolvedChildList = Array.isArray(props.children) ? props.children : [];
55+
return html`${map(children, child => html`${this.renderNode(child)}`)}`;
5356
}
5457
}
5558

renderers/lit/src/v0_9/catalogs/basic/components/Row.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import {html, nothing, css, PropertyValues} from 'lit';
1818
import {customElement} from 'lit/decorators.js';
1919
import {map} from 'lit/directives/map.js';
2020
import {RowApi} from '@a2ui/web_core/v0_9/basic_catalog';
21-
import {BasicCatalogA2uiLitElement} from '../basic-catalog-a2ui-lit-element.js';
21+
import {
22+
BasicCatalogA2uiLitElement,
23+
type ResolvedChildList,
24+
} from '../basic-catalog-a2ui-lit-element.js';
2225
import {A2uiController} from '@a2ui/lit/v0_9';
2326

2427
const JUSTIFY_MAP: Record<string, string> = {
@@ -71,9 +74,9 @@ export class A2uiBasicRowElement extends BasicCatalogA2uiLitElement<typeof RowAp
7174
const props = this.controller.props;
7275
if (!props) return nothing;
7376

74-
const children = Array.isArray(props.children) ? props.children : [];
77+
const children: ResolvedChildList = Array.isArray(props.children) ? props.children : [];
7578

76-
return html` ${map(children, (child: any) => html`${this.renderNode(child)}`)} `;
79+
return html` ${map(children, child => html`${this.renderNode(child)}`)} `;
7780
}
7881
}
7982

renderers/react/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
- (v0_9) Tighten resolved child list types in the basic catalog layout components.
4+
35
## 0.10.0
46

57
- **BREAKING CHANGE**: (v0_9) Rename Icon `path` property to `svgPath` and update component to correctly render SVG elements.

renderers/react/src/v0_9/catalog/basic/components/ChildList.tsx

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,39 @@
1515
*/
1616

1717
import React from 'react';
18-
import {type ComponentContext} from '@a2ui/web_core/v0_9';
18+
import {type ComponentContext, type ComponentId} from '@a2ui/web_core/v0_9';
19+
20+
type ResolvedChildRef =
21+
| ComponentId
22+
| {
23+
id: ComponentId;
24+
basePath: string;
25+
};
26+
27+
type ResolvedChildList = ResolvedChildRef[];
1928

2029
export const ChildList: React.FC<{
21-
childList: unknown;
30+
childList: ResolvedChildList;
2231
context: ComponentContext;
23-
buildChild: (id: string, basePath?: string) => React.ReactNode;
32+
buildChild: (id: ComponentId, basePath?: string) => React.ReactNode;
2433
}> = ({childList, buildChild}) => {
25-
if (Array.isArray(childList)) {
26-
return (
27-
<>
28-
{childList.map((item: unknown, i: number) => {
29-
// The new binder outputs objects like { id: string, basePath: string } for arrays of structural nodes
30-
if (item && typeof item === 'object' && 'id' in item) {
31-
const node = item as {id: string; basePath?: string};
32-
return (
33-
<React.Fragment key={`${node.id}-${i}`}>
34-
{buildChild(node.id, node.basePath)}
35-
</React.Fragment>
36-
);
37-
}
38-
// Fallback for static string lists
39-
if (typeof item === 'string') {
40-
return <React.Fragment key={`${item}-${i}`}>{buildChild(item)}</React.Fragment>;
41-
}
42-
return null;
43-
})}
44-
</>
45-
);
46-
}
34+
if (!Array.isArray(childList)) return null;
35+
36+
return (
37+
<>
38+
{childList.map((childRef, index) => {
39+
if (typeof childRef === 'string') {
40+
return (
41+
<React.Fragment key={`${childRef}-${index}`}>{buildChild(childRef)}</React.Fragment>
42+
);
43+
}
4744

48-
return null;
45+
return (
46+
<React.Fragment key={`${childRef.id}-${childRef.basePath}`}>
47+
{buildChild(childRef.id, childRef.basePath)}
48+
</React.Fragment>
49+
);
50+
})}
51+
</>
52+
);
4953
};

0 commit comments

Comments
 (0)