11from __future__ import annotations
22
33import logging
4- from typing import Any , Mapping , cast
4+ from typing import Any , DefaultDict , Mapping , cast
55
66from fastjsonschema import compile as compile_json_schema
77
1515from idom .core .types import (
1616 ComponentType ,
1717 EventHandlerDict ,
18- EventHandlerMapping ,
1918 EventHandlerType ,
2019 ImportSourceDict ,
2120 Key ,
@@ -157,7 +156,7 @@ def vdom(
157156
158157 flattened_children : list [VdomChild ] = []
159158 for child in children :
160- if isinstance (child , dict ) and "tagName" not in child :
159+ if isinstance (child , dict ) and "tagName" not in child : # pragma: no cover
161160 warn (
162161 (
163162 "Element constructor signatures have changed! This will be an error "
@@ -203,62 +202,28 @@ def with_import_source(element: VdomDict, import_source: ImportSourceDict) -> Vd
203202 return {** element , "importSource" : import_source }
204203
205204
206- def with_event_handlers (
207- element : VdomDict , event_handlers : EventHandlerMapping
208- ) -> VdomDict :
209- if "eventHandlers" in element :
210- old_handlers = element ["eventHandlers" ]
211- new_handlers = {
212- merge_event_handlers ((old_handlers [k ], event_handlers [h ]))
213- if k in old_handlers
214- else h
215- for k , h in event_handlers
216- }
217- return {** element , "eventHandlers" : new_handlers }
218- else :
219- return {** element , "eventHandlers" : dict (event_handlers )}
220-
221-
222205def make_vdom_constructor (
223206 tag : str ,
224207 allow_children : bool = True ,
225208 import_source : ImportSourceDict | None = None ,
226- event_handlers : EventHandlerMapping | None = None ,
227209) -> VdomDictConstructor :
228210 """Return a constructor for VDOM dictionaries with the given tag name.
229211
230212 The resulting callable will have the same interface as :func:`vdom` but without its
231213 first ``tag`` argument.
232214 """
233215
234- if import_source is not None :
216+ def constructor (
217+ * children : VdomChild , key : Key | None = None , ** attributes : Any
218+ ) -> VdomDict :
219+ if not allow_children and children :
220+ raise TypeError (f"{ tag !r} nodes cannot have children." )
235221
236- def constructor (
237- * children : VdomChild , key : Key | None = None , ** attributes : Any
238- ) -> VdomDict :
239- if not allow_children and children :
240- raise TypeError (f"{ tag !r} nodes cannot have children." )
241- return with_import_source (
242- vdom (tag , * children , key = key , ** attributes ), import_source
243- )
222+ model = vdom (tag , * children , key = key , ** attributes ), import_source
223+ if import_source is not None :
224+ model = with_import_source (model , import_source )
244225
245- else :
246-
247- def constructor (
248- * children : VdomChild , key : Key | None = None , ** attributes : Any
249- ) -> VdomDict :
250- if not allow_children and children :
251- raise TypeError (f"{ tag !r} nodes cannot have children." )
252- return vdom (tag , * children , key = key , ** attributes )
253-
254- if event_handlers :
255- _constructor = constructor
256-
257- def constructor (
258- * children : VdomChild , key : Key | None = None , ** attributes : Any
259- ) -> VdomDict :
260- model = _constructor (* children , key = key , ** attributes )
261- return with_event_handlers (model , event_handlers )
226+ return model
262227
263228 # replicate common function attributes
264229 constructor .__name__ = tag
@@ -280,7 +245,7 @@ def separate_attributes_and_event_handlers(
280245 attributes : Mapping [str , Any ]
281246) -> tuple [dict [str , Any ], EventHandlerDict ]:
282247 separated_attributes = {}
283- separated_event_handlers : dict [str , list [EventHandlerType ]] = {}
248+ separated_handlers : DefaultDict [str , list [EventHandlerType ]] = DefaultDict ( list )
284249
285250 for k , v in attributes .items ():
286251
@@ -299,13 +264,10 @@ def separate_attributes_and_event_handlers(
299264 separated_attributes [k ] = v
300265 continue
301266
302- if k not in separated_event_handlers :
303- separated_event_handlers [k ] = [handler ]
304- else :
305- separated_event_handlers [k ].append (handler )
267+ separated_handlers [k ].append (handler )
306268
307269 flat_event_handlers_dict = {
308- k : merge_event_handlers (h ) for k , h in separated_event_handlers .items ()
270+ k : merge_event_handlers (h ) for k , h in separated_handlers .items ()
309271 }
310272
311273 return separated_attributes , flat_event_handlers_dict
0 commit comments