Skip to content

Commit c618058

Browse files
committed
Refactoring related to ReactPy v2.0.0
1 parent 898c8fd commit c618058

11 files changed

Lines changed: 29 additions & 39 deletions

File tree

docs/examples/python/use_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@component
77
def user():
88
params = use_params()
9-
return html._(html.h1(f"User {params['id']} 👤"), html.p("Nothing (yet)."))
9+
return html(html.h1(f"User {params['id']} 👤"), html.p("Nothing (yet)."))
1010

1111

1212
@component

docs/examples/python/use_search_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@component
77
def search():
88
search_params = use_search_params()
9-
return html._(html.h1(f"Search Results for {search_params['query'][0]} 🔍"), html.p("Nothing (yet)."))
9+
return html(html.h1(f"Search Results for {search_params['query'][0]} 🔍"), html.p("Nothing (yet)."))
1010

1111

1212
@component

docs/src/learn/routers-routes-and-links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The [`browser_router`][reactpy_router.browser_router] component is one possible
99
!!! abstract "Note"
1010

1111
The current location is determined based on the browser's current URL and can be found
12-
by checking the [`use_location`][reactpy.backend.hooks.use_location] hook.
12+
by checking the [`use_location`][reactpy.use_location] hook.
1313

1414
Here's a basic example showing how to use `#!python browser_router` with two routes.
1515

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ classifiers = [
2828
"Environment :: Web Environment",
2929
"Typing :: Typed",
3030
]
31-
dependencies = ["reactpy>=1.1.0, <2.0.0", "typing_extensions"]
31+
dependencies = [
32+
"reactpy[asgi]>=2.0.0b10, <3.0.0",
33+
"typing_extensions",
34+
"jsonpointer==3.*",
35+
]
3236
dynamic = ["version"]
3337
urls.Changelog = "https://reactive-python.github.io/reactpy-router/latest/about/changelog/"
3438
urls.Documentation = "https://reactive-python.github.io/reactpy-router/latest/"

src/js/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface ReactPyLocation {
2-
pathname: string;
3-
search: string;
2+
path: string;
3+
query_string: string;
44
}
55

66
export interface HistoryProps {

src/js/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { ReactPyLocation } from "./types";
22

33
export function createLocationObject(): ReactPyLocation {
44
return {
5-
pathname: window.location.pathname,
6-
search: window.location.search,
5+
path: window.location.pathname,
6+
query_string: window.location.search,
77
};
88
}
99

src/reactpy_router/components.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,22 @@
55
from uuid import uuid4
66

77
from reactpy import component, html, use_connection, use_ref
8-
from reactpy.backend.types import Location
9-
from reactpy.web.module import export, module_from_file
8+
from reactpy.types import Location
9+
from reactpy.reactjs import component_from_file
1010

1111
from reactpy_router.hooks import _use_route_state
1212
from reactpy_router.types import Route
1313

1414
if TYPE_CHECKING:
15-
from reactpy.core.component import Component
16-
from reactpy.core.types import Key, VdomDict
15+
from reactpy.types import Key, VdomDict, Component
1716

18-
History = export(
19-
module_from_file("reactpy-router", file=Path(__file__).parent / "static" / "bundle.js"),
20-
("History"),
21-
)
17+
History = component_from_file(Path(__file__).parent / "static" / "bundle.js", import_names="History", name="reactpy-router")
2218
"""Client-side portion of history handling"""
2319

24-
Link = export(
25-
module_from_file("reactpy-router", file=Path(__file__).parent / "static" / "bundle.js"),
26-
("Link"),
27-
)
20+
Link = component_from_file(Path(__file__).parent / "static" / "bundle.js", import_names="Link", name="reactpy-router")
2821
"""Client-side portion of link handling"""
2922

30-
Navigate = export(
31-
module_from_file("reactpy-router", file=Path(__file__).parent / "static" / "bundle.js"),
32-
("Navigate"),
33-
)
23+
Navigate = component_from_file(Path(__file__).parent / "static" / "bundle.js", import_names="Navigate", name="reactpy-router")
3424
"""Client-side portion of the navigate component"""
3525

3626

@@ -74,7 +64,7 @@ def _link(attributes: dict[str, Any], *children: Any) -> VdomDict:
7464
def on_click_callback(_event: dict[str, Any]) -> None:
7565
set_location(Location(**_event))
7666

77-
return html._(Link({"onClickCallback": on_click_callback, "linkClass": class_name}), html.a(attrs, *children))
67+
return html(Link({"onClickCallback": on_click_callback, "linkClass": class_name}), html.a(attrs, *children))
7868

7969

8070
def route(path: str, element: Any | None, *routes: Route) -> Route:
@@ -118,7 +108,7 @@ def _navigate(to: str, replace: bool = False) -> VdomDict | None:
118108
def on_navigate_callback(_event: dict[str, Any]) -> None:
119109
set_location(Location(**_event))
120110

121-
if location.pathname != pathname:
111+
if location.path != pathname:
122112
return Navigate({"onNavigateCallback": on_navigate_callback, "to": to, "replace": replace})
123113

124114
return None

src/reactpy_router/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def use_search_params(
5959
A dictionary of the current URL's query string parameters.
6060
"""
6161
location = use_location()
62-
query_string = location.search[1:] if len(location.search) > 1 else ""
62+
query_string = location.query_string[1:] if len(location.query_string) > 1 else ""
6363

6464
# TODO: In order to match `react-router`, this will need to return a tuple of the search params \
6565
# and a function to update them. This is currently not possible without reactpy core having a \

src/reactpy_router/routers.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@
77
from typing import TYPE_CHECKING, Any, Union, cast
88

99
from reactpy import component, use_memo, use_state
10-
from reactpy.backend.types import Connection, Location
1110
from reactpy.core.hooks import ConnectionContext, use_connection
12-
from reactpy.types import ComponentType, VdomDict
11+
from reactpy.types import Component, VdomDict, Connection, Location
1312

1413
from reactpy_router.components import History
1514
from reactpy_router.hooks import RouteState, _route_state_context
1615
from reactpy_router.resolvers import ReactPyResolver
1716

1817
if TYPE_CHECKING:
1918
from collections.abc import Iterator, Sequence
20-
21-
from reactpy.core.component import Component
22-
2319
from reactpy_router.types import CompiledRoute, MatchedRoute, Resolver, Route, Router
2420

2521
__all__ = ["browser_router", "create_router"]
@@ -105,11 +101,11 @@ def _add_route_key(match: MatchedRoute, key: str | int) -> Any:
105101
"""Add a key to the VDOM or component on the current route, if it doesn't already have one."""
106102
element = match.element
107103
if hasattr(element, "render") and not element.key:
108-
element = cast(ComponentType, element)
104+
element = cast(Component, element)
109105
element.key = key
110106
elif isinstance(element, dict) and not element.get("key", None):
111107
element = cast(VdomDict, element)
112-
element["key"] = key
108+
element["attributes"]["key"] = key
113109
return match
114110

115111

@@ -118,10 +114,10 @@ def _match_route(
118114
location: Location,
119115
) -> MatchedRoute | None:
120116
for resolver in compiled_routes:
121-
match = resolver.resolve(location.pathname)
117+
match = resolver.resolve(location.path)
122118
if match is not None:
123119
return _add_route_key(match, resolver.key)
124120

125-
_logger.debug("No matching route found for %s", location.pathname)
121+
_logger.debug("No matching route found for %s", location.path)
126122

127123
return None

src/reactpy_router/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if TYPE_CHECKING:
1212
from collections.abc import Sequence
1313

14-
from reactpy.backend.types import Location
14+
from reactpy.types import Location
1515
from reactpy.core.component import Component
1616
from reactpy.types import Key
1717

@@ -42,7 +42,7 @@ class Route:
4242

4343
def __hash__(self) -> int:
4444
el = self.element
45-
key = el["key"] if is_vdom(el) and "key" in el else getattr(el, "key", id(el))
45+
key = el["attributes"]["key"] if is_vdom(el) and "attributes" in el and "key" in el["attributes"] else getattr(el, "key", id(el))
4646
return hash((self.path, key, self.routes))
4747

4848

0 commit comments

Comments
 (0)