Skip to content

Commit 37760bb

Browse files
committed
Fix link component
1 parent 03b8f2c commit 37760bb

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

docs/examples/python/nested-routes.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import TypedDict
22

33
from reactpy import component, html, run
4+
45
from reactpy_router import browser_router, link, route
56

67
message_data: list["MessageDataType"] = [
@@ -40,10 +41,7 @@ def home():
4041

4142
@component
4243
def all_messages():
43-
last_messages = {
44-
", ".join(msg["with"]): msg
45-
for msg in sorted(message_data, key=lambda m: m["id"])
46-
}
44+
last_messages = {", ".join(msg["with"]): msg for msg in sorted(message_data, key=lambda m: m["id"])}
4745
return html.div(
4846
html.h1("All Messages 💬"),
4947
html.ul(
@@ -66,7 +64,7 @@ def all_messages():
6664

6765
@component
6866
def messages_with(*names):
69-
messages = [msg for msg in message_data if set(msg["with"]) == names]
67+
messages = [msg for msg in message_data if tuple(msg["with"]) == names]
7068
return html.div(
7169
html.h1(f"Messages with {', '.join(names)} 💬"),
7270
html.ul(

docs/examples/python/route-parameters.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import TypedDict
22

33
from reactpy import component, html, run
4+
45
from reactpy_router import browser_router, link, route
56
from reactpy_router.core import use_params
67

@@ -39,10 +40,7 @@ def home():
3940

4041
@component
4142
def all_messages():
42-
last_messages = {
43-
", ".join(msg["with"]): msg
44-
for msg in sorted(message_data, key=lambda m: m["id"])
45-
}
43+
last_messages = {", ".join(msg["with"]): msg for msg in sorted(message_data, key=lambda m: m["id"])}
4644
return html.div(
4745
html.h1("All Messages 💬"),
4846
html.ul(
@@ -66,7 +64,7 @@ def all_messages():
6664
@component
6765
def messages_with():
6866
names = set(use_params()["names"].split("-")) # and here we use the path param
69-
messages = [msg for msg in message_data if set(msg["with"]) == names]
67+
messages = [msg for msg in message_data if tuple(msg["with"]) == names]
7068
return html.div(
7169
html.h1(f"Messages with {', '.join(names)} 💬"),
7270
html.ul(

src/reactpy_router/core.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,22 @@ def router_component(
8383

8484
@component
8585
def link(*children: VdomChild, to: str, **attributes: Any) -> VdomDict:
86-
"""A component that renders a link to the given path"""
86+
"""A component that renders a link to the given path.
87+
88+
FIXME: This currently works in a "dumb" way by trusting that ReactPy's script tag
89+
properly sets the location. When a client-server communication layer is added to
90+
ReactPy, this component will need to be rewritten to use that instead."""
8791
set_location = _use_route_state().set_location
8892
uuid = uuid4().hex
93+
94+
def on_click(_event: dict[str, Any]) -> None:
95+
pathname, search = to.split("?", 1) if "?" in to else (to, "")
96+
set_location(Location(pathname, search))
97+
8998
attrs = {
9099
**attributes,
91-
"to": to,
92-
"onClick": lambda event: set_location(Location(**event)),
100+
"href": to,
101+
"onClick": on_click,
93102
"id": uuid,
94103
}
95104
return html._(html.a(attrs, *children), html.script(link_js_content.replace("UUID", uuid)))

src/reactpy_router/static/link.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
document.getElementById("UUID").addEventListener("click", (event) => {
2-
event.preventDefault();
3-
window.history.pushState({}, to, new URL(to, window.location));
4-
onClick({
5-
pathname: window.location.pathname,
6-
search: window.location.search,
7-
});
2+
event.preventDefault();
3+
let to = event.target.getAttribute("href");
4+
window.history.pushState({}, to, new URL(to, window.location));
85
});

0 commit comments

Comments
 (0)