Move the history provider into the context (#3048)
* move history providers into a separate crate * start moving route providers into the renderers * clean up intoroutable * remove into routable * fix router tests * Provide history providers in each renderer * implement nested routers * move the lens out of the history crate * re-export dioxus history trait in the prelude * also re-export the history function * fix history doctests * some light cleanups --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
e5a1a62644
commit
281087469a
48 files changed
+875
-1419
No files matched your search
@@ -1,13 +1,23 @@
|
||||
use dioxus::prelude::*;
|
||||
use std::str::FromStr;
|
||||
use dioxus_history::{History, MemoryHistory};
|
||||
use dioxus_router::components::HistoryProvider;
|
||||
use std::{rc::Rc, str::FromStr};
|
||||
|
||||
fn prepare<R: Routable>() -> String
|
||||
where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
prepare_at::<R>("/")
|
||||
}
|
||||
|
||||
fn prepare_at<R: Routable>(at: impl ToString) -> String
|
||||
where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
let mut vdom = VirtualDom::new_with_props(
|
||||
App,
|
||||
AppProps::<R> {
|
||||
at: at.to_string(),
|
||||
phantom: std::marker::PhantomData,
|
||||
},
|
||||
);
|
||||
@@ -16,12 +26,14 @@ where
|
||||
|
||||
#[derive(Props)]
|
||||
struct AppProps<R: Routable> {
|
||||
at: String,
|
||||
phantom: std::marker::PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<R: Routable> Clone for AppProps<R> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
at: self.at.clone(),
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -34,14 +46,15 @@ where
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn App<R: Routable>(_props: AppProps<R>) -> Element
|
||||
fn App<R: Routable>(props: AppProps<R>) -> Element
|
||||
where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
rsx! {
|
||||
h1 { "App" }
|
||||
Router::<R> {
|
||||
config: |_| RouterConfig::default().history(MemoryHistory::default())
|
||||
HistoryProvider {
|
||||
history: move |_| Rc::new(MemoryHistory::with_initial_path(props.at.clone())) as Rc<dyn History>,
|
||||
Router::<R> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -345,3 +358,76 @@ fn with_rel() {
|
||||
|
||||
assert_eq!(prepare::<Route>(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_child_route() {
|
||||
#[derive(Routable, Clone, PartialEq, Debug)]
|
||||
enum ChildRoute {
|
||||
#[route("/")]
|
||||
ChildRoot {},
|
||||
#[route("/:not_static")]
|
||||
NotStatic { not_static: String },
|
||||
}
|
||||
|
||||
#[derive(Routable, Clone, PartialEq, Debug)]
|
||||
enum Route {
|
||||
#[route("/")]
|
||||
Root {},
|
||||
#[route("/test")]
|
||||
Test {},
|
||||
#[child("/child")]
|
||||
Nested { child: ChildRoute },
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Test() -> Element {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Root() -> Element {
|
||||
rsx! {
|
||||
Link {
|
||||
to: Route::Test {},
|
||||
"Parent Link"
|
||||
}
|
||||
Link {
|
||||
to: Route::Nested { child: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() } },
|
||||
"Child Link"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn ChildRoot() -> Element {
|
||||
rsx! {
|
||||
Link {
|
||||
to: Route::Test {},
|
||||
"Parent Link"
|
||||
}
|
||||
Link {
|
||||
to: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() },
|
||||
"Child Link 1"
|
||||
}
|
||||
Link {
|
||||
to: Route::Nested { child: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() } },
|
||||
"Child Link 2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn NotStatic(not_static: String) -> Element {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
prepare_at::<Route>("/"),
|
||||
"<h1>App</h1><a href=\"/test\">Parent Link</a><a href=\"/child/this-is-a-child-route\">Child Link</a>"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
prepare_at::<Route>("/child"),
|
||||
"<h1>App</h1><a href=\"/test\">Parent Link</a><a href=\"/child/this-is-a-child-route\">Child Link 1</a><a href=\"/child/this-is-a-child-route\">Child Link 2</a>"
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_history::{History, MemoryHistory};
|
||||
use dioxus_router::components::HistoryProvider;
|
||||
use dioxus_router::prelude::*;
|
||||
|
||||
fn prepare(path: impl Into<String>) -> VirtualDom {
|
||||
@@ -38,10 +42,9 @@ fn prepare(path: impl Into<String>) -> VirtualDom {
|
||||
fn App(path: Route) -> Element {
|
||||
rsx! {
|
||||
h1 { "App" }
|
||||
Router::<Route> {
|
||||
config: move |_| {
|
||||
RouterConfig::default().history(MemoryHistory::with_initial_path(path.clone()))
|
||||
}
|
||||
HistoryProvider {
|
||||
history: move |_| Rc::new(MemoryHistory::with_initial_path(path.clone())) as Rc<dyn History>,
|
||||
Router::<Route> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
use std::str::FromStr;
|
||||
use dioxus_history::{History, MemoryHistory};
|
||||
use dioxus_router::components::HistoryProvider;
|
||||
use std::{rc::Rc, str::FromStr};
|
||||
|
||||
// Tests for regressions of <https://github.com/DioxusLabs/dioxus/issues/2549>
|
||||
#[test]
|
||||
@@ -33,10 +35,9 @@ fn Home(lang: String) -> Element {
|
||||
#[component]
|
||||
fn App(path: Route) -> Element {
|
||||
rsx! {
|
||||
Router::<Route> {
|
||||
config: {
|
||||
move |_| RouterConfig::default().history(MemoryHistory::with_initial_path(path.clone()))
|
||||
}
|
||||
HistoryProvider {
|
||||
history: move |_| Rc::new(MemoryHistory::with_initial_path(path.clone())) as Rc<dyn History>,
|
||||
Router::<Route> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_history::{History, MemoryHistory};
|
||||
use dioxus_router::components::HistoryProvider;
|
||||
|
||||
// Tests for regressions of <https://github.com/DioxusLabs/dioxus/issues/2468>
|
||||
#[test]
|
||||
@@ -32,10 +36,9 @@ fn Test() -> Element {
|
||||
#[component]
|
||||
fn App(path: Route) -> Element {
|
||||
rsx! {
|
||||
Router::<Route> {
|
||||
config: {
|
||||
move || RouterConfig::default().history(MemoryHistory::with_initial_path(path))
|
||||
}
|
||||
HistoryProvider {
|
||||
history: move |_| Rc::new(MemoryHistory::with_initial_path(path)) as Rc<dyn History>,
|
||||
Router::<Route> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user