25
25
EventHandlerType ,
26
26
ImportSourceDict ,
27
27
JavaScript ,
28
+ JSExecutableDict ,
28
29
VdomAttributes ,
29
30
VdomChildren ,
30
31
VdomDict ,
46
47
"children" : {"$ref" : "#/definitions/elementChildren" },
47
48
"attributes" : {"type" : "object" },
48
49
"eventHandlers" : {"$ref" : "#/definitions/elementEventHandlers" },
50
+ "jsExecutables" : {"$ref" : "#/definitions/elementJSExecutables" },
49
51
"importSource" : {"$ref" : "#/definitions/importSource" },
50
52
},
51
53
# The 'tagName' is required because its presence is a useful indicator of
75
77
},
76
78
"required" : ["target" ],
77
79
},
80
+ "elementJSExecutables" : {
81
+ "type" : "object" ,
82
+ "patternProperties" : {
83
+ ".*" : "str" ,
84
+ },
85
+ },
78
86
"importSource" : {
79
87
"type" : "object" ,
80
88
"properties" : {
@@ -164,7 +172,9 @@ def __call__(
164
172
"""The entry point for the VDOM API, for example reactpy.html(<WE_ARE_HERE>)."""
165
173
attributes , children = separate_attributes_and_children (attributes_and_children )
166
174
key = attributes .get ("key" , None )
167
- attributes , event_handlers = separate_attributes_and_event_handlers (attributes )
175
+ attributes , event_handlers , js_executables = (
176
+ separate_attributes_handlers_and_executables (attributes )
177
+ )
168
178
if REACTPY_CHECK_JSON_ATTRS .current :
169
179
json .dumps (attributes )
170
180
@@ -184,6 +194,7 @@ def __call__(
184
194
** ({"children" : children } if children else {}),
185
195
** ({"attributes" : attributes } if attributes else {}),
186
196
** ({"eventHandlers" : event_handlers } if event_handlers else {}),
197
+ ** ({"jsExecutables" : js_executables } if js_executables else {}),
187
198
** ({"importSource" : self .import_source } if self .import_source else {}),
188
199
}
189
200
@@ -216,28 +227,26 @@ def separate_attributes_and_children(
216
227
return _attributes , _children
217
228
218
229
219
- def separate_attributes_and_event_handlers (
230
+ def separate_attributes_handlers_and_executables (
220
231
attributes : Mapping [str , Any ],
221
- ) -> tuple [VdomAttributes , EventHandlerDict ]:
232
+ ) -> tuple [VdomAttributes , EventHandlerDict , JSExecutableDict ]:
222
233
_attributes : VdomAttributes = {}
223
- _event_handlers : dict [str , EventHandlerType | JavaScript ] = {}
234
+ _event_handlers : dict [str , EventHandlerType ] = {}
235
+ _js_executables : dict [str , JavaScript ] = {}
224
236
225
237
for k , v in attributes .items ():
226
- handler : EventHandlerType | JavaScript
227
-
228
238
if callable (v ):
229
- handler = EventHandler (to_event_handler_function (v ))
239
+ _event_handlers [k ] = EventHandler (to_event_handler_function (v ))
240
+ elif isinstance (v , EventHandler ):
241
+ _event_handlers [k ] = v
230
242
elif EVENT_ATTRIBUTE_PATTERN .match (k ) and isinstance (v , str ):
231
- handler = JavaScript (v )
232
- elif isinstance (v , ( EventHandler , JavaScript ) ):
233
- handler = v
243
+ _js_executables [ k ] = JavaScript (v )
244
+ elif isinstance (v , JavaScript ):
245
+ _js_executables [ k ] = v
234
246
else :
235
247
_attributes [k ] = v
236
- continue
237
-
238
- _event_handlers [k ] = handler
239
248
240
- return _attributes , _event_handlers
249
+ return _attributes , _event_handlers , _js_executables
241
250
242
251
243
252
def _flatten_children (children : Sequence [Any ]) -> list [Any ]:
0 commit comments