55
66@dataclass (frozen = True )
77class Component (ABC ):
8- id : str = None
9- name : str = None
10- value : str | int | float = None
11- style : dict [str , Any ] = None
8+ # Common HTML properties
9+ id : str | None = None
10+ name : str | None = None
11+ value : bool | int | float | str | None = None
12+ style : dict [str , Any ] | None = None
13+ # We may add more here later
14+ #
15+ # Special non-HTML properties
16+ label : str | None = None
17+ children : list ["Component" ] | None = None
1218
1319 @property
1420 def type (self ):
@@ -21,25 +27,14 @@ def to_dict(self) -> dict[str, Any]:
2127 attr_name : attr_value
2228 for attr_name , attr_value in self .__dict__ .items ()
2329 if attr_value is not None
24- and not attr_name .startswith ("_" )
2530 and attr_name
31+ and attr_name != "children"
32+ and not attr_name .startswith ("_" )
2633 }
2734 )
28- return d
29-
30-
31- @dataclass (frozen = True )
32- class Container (Component , ABC ):
33- children : list [Component ] = field (default_factory = list )
34-
35- def add (self , component : Component ):
36- self .children .append (component )
37-
38- def to_dict (self ) -> dict [str , Any ]:
39- d = super ().to_dict ()
40- # Note we use "components" instead of "children" in order
41- # to avoid later problems with React component's "children"
42- # property
43- d .pop ("children" )
44- d .update (components = list (c .to_dict () for c in self .children ))
35+ if self .children is not None :
36+ # Note we use "components" instead of "children" in order
37+ # to avoid later problems with React component's "children"
38+ # property
39+ d .update (components = list (c .to_dict () for c in self .children ))
4540 return d
0 commit comments