4
4
from typing import Any
5
5
from uuid import uuid4
6
6
7
- from reactpy import component , html
7
+ from reactpy import component , event , html
8
8
from reactpy .backend .types import Location
9
9
from reactpy .core .types import VdomChild , VdomDict
10
10
from reactpy .web .module import export , module_from_file
@@ -26,15 +26,16 @@ def link(*children: VdomChild, to: str, **attributes: Any) -> VdomDict:
26
26
# properly sets the location. When a client-server communication layer is added to a \
27
27
# future ReactPy release, this component will need to be rewritten to use that instead. \
28
28
set_location = _use_route_state ().set_location
29
- class_uuid = f"link-{ uuid4 ().hex } "
30
29
30
+ @event (prevent_default = True )
31
31
def on_click (_event : dict [str , Any ]) -> None :
32
32
pathname , search = to .split ("?" , 1 ) if "?" in to else (to , "" )
33
33
if search :
34
34
search = f"?{ search } "
35
35
set_location (Location (pathname , search ))
36
36
37
- class_name = class_uuid
37
+ uuid_string = f"link-{ uuid4 ().hex } "
38
+ class_name = f"{ uuid_string } "
38
39
if "className" in attributes :
39
40
class_name = " " .join ([attributes .pop ("className" ), class_name ])
40
41
# TODO: This can be removed when ReactPy stops supporting underscores in attribute names
@@ -45,9 +46,9 @@ def on_click(_event: dict[str, Any]) -> None:
45
46
** attributes ,
46
47
"href" : to ,
47
48
"onClick" : on_click ,
48
- "className" : class_uuid ,
49
+ "className" : class_name ,
49
50
}
50
- return html ._ (html .a (attrs , * children ), html .script (link_js_content .replace ("UUID" , class_uuid )))
51
+ return html ._ (html .a (attrs , * children ), html .script (link_js_content .replace ("UUID" , uuid_string )))
51
52
52
53
53
54
def route (path : str , element : Any | None , * routes : Route ) -> Route :
0 commit comments