Skip to content

Commit dce3804

Browse files
committed
Add docs for custom routers
1 parent ae400a6 commit dce3804

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Don't forget to remove deprecated code on each major release!
1919

2020
## [Unreleased]
2121

22+
### Added
23+
24+
- Support for custom routers.
25+
2226
### Changed
2327

2428
- Set maximum ReactPy version to `<2.0.0`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from reactpy_router.resolvers import ConversionInfo, ReactPyResolver
2+
3+
4+
# Create a custom resolver that uses the following pattern: "{name:type}"
5+
class CustomResolver(ReactPyResolver):
6+
def __init__(
7+
self,
8+
route,
9+
param_pattern=r"{(?P<name>\w+)(?P<type>:\w+)?}", # Match parameters that use the "{name:type}" format
10+
converters={ # Enable matching for the following types: int, str, any
11+
"int": ConversionInfo(regex=r"\d+", func=int),
12+
"str": ConversionInfo(regex=r"[^/]+", func=str),
13+
"any": ConversionInfo(regex=r".*", func=str),
14+
},
15+
) -> None:
16+
super().__init__(route, param_pattern, converters)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from example.resolvers import CustomResolver
2+
3+
from reactpy_router.routers import create_router
4+
5+
# This can be used in any location where `browser_router` was previously used
6+
custom_router = create_router(CustomResolver)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from reactpy_router.resolvers import ReactPyResolver
2+
3+
4+
class CustomResolver(ReactPyResolver): ...

docs/mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ nav:
66
- Advanced Topics:
77
- Routers, Routes, and Links: learn/routers-routes-and-links.md
88
- Hooks: learn/hooks.md
9-
- Creating a Custom Router 🚧: learn/custom-router.md
9+
- Creating a Custom Router: learn/custom-router.md
1010
- Reference:
1111
- Routers: reference/routers.md
1212
- Components: reference/components.md

docs/src/learn/custom-router.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1-
# Custom Router
1+
Custom routers can be used to define custom routing logic for your application. This is useful when you need to implement a custom routing algorithm or when you need to integrate with an existing URL routing system.
22

3-
Under construction 🚧
3+
---
4+
5+
## Step 1: Creating a custom resolver
6+
7+
You may want to create a custom resolver to allow ReactPy to utilize an existing routing syntax.
8+
9+
To start off, you will need to create a subclass of `#!python ReactPyResolver`. Within this subclass, you have two attributes which you can modify to support your custom routing syntax:
10+
11+
- `#!python param_pattern`: A regular expression pattern that matches the parameters in your URL. This pattern must contain the regex named groups `name` and `type`.
12+
- `#!python converters`: A dictionary that maps a `type` to it's respective `regex` pattern and a converter `func`.
13+
14+
=== "resolver.py"
15+
16+
```python
17+
{% include "../../examples/python/custom_router_easy_resolver.py" %}
18+
```
19+
20+
## Step 2: Creating a custom router
21+
22+
Then, you can use this resolver to create your custom router...
23+
24+
=== "resolver.py"
25+
26+
```python
27+
{% include "../../examples/python/custom_router_easy_router.py" %}
28+
```

src/reactpy_router/resolvers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ReactPyResolver:
2020
def __init__(
2121
self,
2222
route: Route,
23-
param_pattern=r"{(?P<name>\w+)(?P<type>:\w+)?}",
23+
param_pattern: str = r"{(?P<name>\w+)(?P<type>:\w+)?}",
2424
converters: dict[str, ConversionInfo] | None = None,
2525
) -> None:
2626
self.element = route.element

0 commit comments

Comments
 (0)