1717 EventHandlerMapping ,
1818 EventHandlerType ,
1919 ImportSourceDict ,
20- VdomAttributesAndChildren ,
20+ Key ,
21+ VdomChild ,
2122 VdomDict ,
2223 VdomJson ,
2324)
@@ -129,43 +130,47 @@ def is_vdom(value: Any) -> bool:
129130
130131def vdom (
131132 tag : str ,
132- * attributes_and_children : VdomAttributesAndChildren ,
133- key : str | int | None = None ,
134- event_handlers : Optional [EventHandlerMapping ] = None ,
135- import_source : Optional [ImportSourceDict ] = None ,
133+ / ,
134+ * children : VdomChild ,
135+ key : Key | None = None ,
136+ event_handlers : EventHandlerMapping | None = None ,
137+ import_source : ImportSourceDict | None = None ,
138+ ** attributes : Any ,
136139) -> VdomDict :
137140 """A helper function for creating VDOM dictionaries.
138141
139142 Parameters:
140143 tag:
141144 The type of element (e.g. 'div', 'h1', 'img')
142- attributes_and_children:
143- An optional attribute mapping followed by any number of children or
144- iterables of children. The attribute mapping **must** precede the children,
145- or children which will be merged into their respective parts of the model.
145+ children:
146+ String, compoennts, or other VDOM elements that are this element's children.
146147 key:
147- A string idicating the identity of a particular element. This is significant
148- to preserve event handlers across updates - without a key, a re-render would
149- cause these handlers to be deleted, but with a key, they would be redirected
150- to any newly defined handlers.
148+ A string or integer idicating the identity of a particular element. This is
149+ significant to preserve event handlers across updates - without a key, a
150+ re-render would cause these handlers to be deleted, but with a key, they
151+ would be redirected to any newly defined handlers.
151152 event_handlers:
152- Maps event types to coroutines that are responsible for handling those events.
153+ Maps event types to coroutines that are responsible for handling those
154+ events.
153155 import_source:
154156 (subject to change) specifies javascript that, when evaluated returns a
155157 React component.
158+ attributes:
159+ Remaining attributes of this element.
156160 """
157161 model : VdomDict = {"tagName" : tag }
158162
159- attributes , children = coalesce_attributes_and_children (attributes_and_children )
160163 attributes , event_handlers = separate_attributes_and_event_handlers (
161- attributes , event_handlers or {}
164+ attributes , event_handlers
162165 )
163166
164167 if attributes :
168+ if "cls" in attributes :
169+ attributes ["class" ] = attributes .pop ("cls" )
165170 model ["attributes" ] = attributes
166171
167172 if children :
168- model ["children" ] = children
173+ model ["children" ] = flatten_children ( children )
169174
170175 if event_handlers :
171176 model ["eventHandlers" ] = event_handlers
@@ -179,17 +184,6 @@ def vdom(
179184 return model
180185
181186
182- class _VdomDictConstructor (Protocol ):
183- def __call__ (
184- self ,
185- * attributes_and_children : VdomAttributesAndChildren ,
186- key : str | int | None = ...,
187- event_handlers : Optional [EventHandlerMapping ] = ...,
188- import_source : Optional [ImportSourceDict ] = ...,
189- ) -> VdomDict :
190- ...
191-
192-
193187def make_vdom_constructor (
194188 tag : str , allow_children : bool = True
195189) -> _VdomDictConstructor :
@@ -199,19 +193,8 @@ def make_vdom_constructor(
199193 first ``tag`` argument.
200194 """
201195
202- def constructor (
203- * attributes_and_children : VdomAttributesAndChildren ,
204- key : str | int | None = None ,
205- event_handlers : Optional [EventHandlerMapping ] = None ,
206- import_source : Optional [ImportSourceDict ] = None ,
207- ) -> VdomDict :
208- model = vdom (
209- tag ,
210- * attributes_and_children ,
211- key = key ,
212- event_handlers = event_handlers ,
213- import_source = import_source ,
214- )
196+ def constructor (* args : Any , ** kwargs : Any ) -> VdomDict :
197+ model = vdom (* args , ** kwargs )
215198 if not allow_children and "children" in model :
216199 raise TypeError (f"{ tag !r} nodes cannot have children." )
217200 return model
@@ -232,35 +215,24 @@ def constructor(
232215 return constructor
233216
234217
235- def coalesce_attributes_and_children (
236- values : Sequence [Any ],
237- ) -> Tuple [Mapping [str , Any ], List [Any ]]:
238- if not values :
239- return {}, []
240-
241- children_or_iterables : Sequence [Any ]
242- attributes , * children_or_iterables = values
243- if not _is_attributes (attributes ):
244- attributes = {}
245- children_or_iterables = values
246-
247- children : List [Any ] = []
248- for child in children_or_iterables :
218+ def flatten_children (children : Sequence [VdomChild ]) -> Sequence [VdomChild ]:
219+ child_list : list [VdomChild ] = []
220+ for child in children :
249221 if _is_single_child (child ):
250- children .append (child )
222+ child_list .append (child )
251223 else :
252- children .extend (child )
253-
254- return attributes , children
224+ child_list .extend (child )
225+ return child_list
255226
256227
257228def separate_attributes_and_event_handlers (
258- attributes : Mapping [str , Any ], event_handlers : EventHandlerMapping
229+ attributes : Mapping [str , Any ],
230+ event_handlers : EventHandlerMapping | None = None ,
259231) -> Tuple [Dict [str , Any ], EventHandlerDict ]:
260232 separated_attributes = {}
261233 separated_event_handlers : Dict [str , List [EventHandlerType ]] = {}
262234
263- for k , v in event_handlers .items ():
235+ for k , v in ( event_handlers or {}) .items ():
264236 separated_event_handlers [k ] = [v ]
265237
266238 for k , v in attributes .items ():
@@ -339,3 +311,17 @@ def _is_single_child(value: Any) -> bool:
339311 class _EllipsisRepr :
340312 def __repr__ (self ) -> str :
341313 return "..."
314+
315+
316+ class _VdomDictConstructor (Protocol ):
317+ def __call__ (
318+ self ,
319+ tag : str ,
320+ / ,
321+ * children : VdomChild ,
322+ key : str | int | None = None ,
323+ event_handlers : EventHandlerMapping | None = None ,
324+ import_source : ImportSourceDict | None = None ,
325+ ** attributes : Any ,
326+ ) -> VdomDict :
327+ ...
0 commit comments