1
1
from __future__ import annotations
2
2
3
3
import logging
4
- from typing import Any , Mapping , cast
4
+ from typing import Any , DefaultDict , Mapping , cast
5
5
6
6
from fastjsonschema import compile as compile_json_schema
7
7
15
15
from idom .core .types import (
16
16
ComponentType ,
17
17
EventHandlerDict ,
18
- EventHandlerMapping ,
19
18
EventHandlerType ,
20
19
ImportSourceDict ,
21
20
Key ,
@@ -157,7 +156,7 @@ def vdom(
157
156
158
157
flattened_children : list [VdomChild ] = []
159
158
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
161
160
warn (
162
161
(
163
162
"Element constructor signatures have changed! This will be an error "
@@ -203,62 +202,28 @@ def with_import_source(element: VdomDict, import_source: ImportSourceDict) -> Vd
203
202
return {** element , "importSource" : import_source }
204
203
205
204
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
-
222
205
def make_vdom_constructor (
223
206
tag : str ,
224
207
allow_children : bool = True ,
225
208
import_source : ImportSourceDict | None = None ,
226
- event_handlers : EventHandlerMapping | None = None ,
227
209
) -> VdomDictConstructor :
228
210
"""Return a constructor for VDOM dictionaries with the given tag name.
229
211
230
212
The resulting callable will have the same interface as :func:`vdom` but without its
231
213
first ``tag`` argument.
232
214
"""
233
215
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." )
235
221
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 )
244
225
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
262
227
263
228
# replicate common function attributes
264
229
constructor .__name__ = tag
@@ -280,7 +245,7 @@ def separate_attributes_and_event_handlers(
280
245
attributes : Mapping [str , Any ]
281
246
) -> tuple [dict [str , Any ], EventHandlerDict ]:
282
247
separated_attributes = {}
283
- separated_event_handlers : dict [str , list [EventHandlerType ]] = {}
248
+ separated_handlers : DefaultDict [str , list [EventHandlerType ]] = DefaultDict ( list )
284
249
285
250
for k , v in attributes .items ():
286
251
@@ -299,13 +264,10 @@ def separate_attributes_and_event_handlers(
299
264
separated_attributes [k ] = v
300
265
continue
301
266
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 )
306
268
307
269
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 ()
309
271
}
310
272
311
273
return separated_attributes , flat_event_handlers_dict
0 commit comments