Allow path prop on <Route/> to be any type that impl std::fmt::Display

This commit is contained in:
Greg Johnston 2023-01-03 18:37:43 -05:00
parent 6a4cbbf266
commit 263d5b1d89
3 changed files with 6 additions and 5 deletions

View file

@ -10,12 +10,12 @@ use crate::{
/// Describes a portion of the nested layout of the app, specifying the route it should match,
/// the element it should display, and data that should be loaded alongside the route.
#[component(transparent)]
pub fn Route<E, F>(
pub fn Route<E, F, P>(
cx: Scope,
/// The path fragment that this route should match. This can be static (`users`),
/// include a parameter (`:id`) or an optional parameter (`:id?`), or match a
/// wildcard (`user/*any`).
path: &'static str,
path: P,
/// The view that should be shown when this route is matched. This can be any function
/// that takes a [Scope] and returns an [Element] (like `|cx| view! { cx, <p>"Show this"</p> })`
/// or `|cx| view! { cx, <MyComponent/>` } or even, for a component with no props, `MyComponent`).
@ -27,6 +27,7 @@ pub fn Route<E, F>(
where
E: IntoView,
F: Fn(Scope) -> E + 'static,
P: std::fmt::Display,
{
let children = children
.map(|children| {
@ -43,7 +44,7 @@ where
})
.unwrap_or_default();
RouteDefinition {
path,
path: path.to_string(),
children,
view: Rc::new(move |cx| view(cx).into_view(cx)),
}

View file

@ -273,7 +273,7 @@ fn create_routes(route_def: &RouteDefinition, base: &str) -> Vec<RouteData> {
let RouteDefinition { children, .. } = route_def;
let is_leaf = children.is_empty();
let mut acc = Vec::new();
for original_path in expand_optionals(route_def.path) {
for original_path in expand_optionals(&route_def.path) {
let path = join_paths(base, &original_path);
let pattern = if is_leaf {
path

View file

@ -5,7 +5,7 @@ use leptos::*;
#[derive(Clone)]
pub struct RouteDefinition {
pub path: &'static str,
pub path: String,
pub children: Vec<RouteDefinition>,
pub view: Rc<dyn Fn(Scope) -> View>,
}