Skip to content

Commit f3a299a

Browse files
authored
Merge pull request #47 from bcdev/forman-x-dont_render_falsifying_children
Don't render falsy components
2 parents 503417b + e753d88 commit f3a299a

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

chartlets.js/src/lib/components/Box.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export interface BoxProps extends Omit<BoxState, "type"> {
88
onChange: ComponentChangeHandler;
99
}
1010

11-
export function Box({ id, style, children: components, onChange }: BoxProps) {
11+
export function Box({ id, style, children, onChange }: BoxProps) {
1212
return (
1313
<MuiBox id={id} style={style}>
14-
<ComponentChildren components={components} onChange={onChange} />
14+
<ComponentChildren nodes={children} onChange={onChange} />
1515
</MuiBox>
1616
);
1717
}

chartlets.js/src/lib/components/ComponentChildren.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
import { type ComponentChangeHandler } from "@/lib/types/state/event";
22
import {
3-
type ComponentItem,
3+
type ComponentNode,
44
isComponentState,
55
} from "@/lib/types/state/component";
66
import { Component } from "./Component";
77

88
export interface ComponentChildrenProps {
9-
components?: ComponentItem[];
9+
nodes?: ComponentNode[];
1010
onChange: ComponentChangeHandler;
1111
}
1212

13-
export function ComponentChildren({
14-
components,
15-
onChange,
16-
}: ComponentChildrenProps) {
17-
if (!components || components.length === 0) {
13+
export function ComponentChildren({ nodes, onChange }: ComponentChildrenProps) {
14+
if (!nodes || nodes.length === 0) {
1815
return null;
1916
}
2017
return (
2118
<>
22-
{components.map((item, index) => {
23-
if (isComponentState(item)) {
24-
const key = item.id || index;
25-
return <Component key={key} {...item} onChange={onChange} />;
19+
{nodes.map((node, index) => {
20+
if (isComponentState(node)) {
21+
const key = node.id || index;
22+
return <Component key={key} {...node} onChange={onChange} />;
23+
} else if (typeof node === "string") {
24+
return node;
25+
} else if (!node) {
26+
// This is ok, just as with React, don't render
2627
} else {
27-
return item;
28+
console.warn("chartlets: invalid child node encountered:", node);
2829
}
2930
})}
3031
</>

chartlets.js/src/lib/components/Typography.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ export interface TypographyProps extends Omit<TypographyState, "type"> {
88
onChange: ComponentChangeHandler;
99
}
1010

11-
export function Typography({
12-
id,
13-
style,
14-
children: components,
15-
onChange,
16-
}: TypographyProps) {
11+
export function Typography({ id, style, children, onChange }: TypographyProps) {
1712
return (
1813
<MuiTypography id={id} style={style}>
19-
<ComponentChildren components={components} onChange={onChange} />
14+
<ComponentChildren nodes={children} onChange={onChange} />
2015
</MuiTypography>
2116
);
2217
}

chartlets.js/src/lib/types/state/component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@ export type ComponentType =
1010
| "Select"
1111
| "Typography";
1212

13+
export type ComponentNode =
14+
| ComponentState
15+
| string
16+
| 0
17+
| false
18+
| null
19+
| undefined;
20+
1321
export interface ComponentState {
22+
// TODO: Rename to tag, so we can also have
23+
// (Html)ElementState along with ComponentState
1424
type: ComponentType;
15-
label?: string;
16-
children?: ComponentItem[];
25+
children?: ComponentNode[];
1726
// common HTML attributes
1827
id?: string;
1928
name?: string;
2029
value?: boolean | string | number;
2130
style?: CSSProperties;
2231
disabled?: boolean;
32+
label?: string;
2333
}
2434

25-
export type ComponentItem = ComponentState | string;
26-
2735
export interface ContainerState extends ComponentState {
28-
children: ComponentItem[];
36+
children: ComponentNode[];
2937
}
3038

3139
export type SelectOption =

chartlets.py/chartlets/component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Component(ABC):
3434
color: str | None = None
3535
"""HTML `color` attribute. Optional."""
3636

37-
children: list[Union["Component", str]] | None = None
37+
children: list[Union["Component", str, None]] | None = None
3838
"""Children used by many specific components. Optional."""
3939

4040
@property

0 commit comments

Comments
 (0)