Make new enum router less breaking

This commit is contained in:
Evan Almloff committed 2023-07-24 11:57:56 -07:00
1 parent b51ffa0590
commit 61dc549ba1
16 files changed
+74 -63

No files matched your search

+4 -4
View File
@@ -44,8 +44,8 @@ fn NavBar(cx: Scope) -> Element {
render! { render! {
nav { nav {
ul { ul {
li { Link { target: Route::Home {}, "Home" } } li { Link { to: Route::Home {}, "Home" } }
li { Link { target: Route::BlogList {}, "Blog" } } li { Link { to: Route::BlogList {}, "Blog" } }
} }
} }
Outlet {} Outlet {}
@@ -77,13 +77,13 @@ fn BlogList(cx: Scope) -> Element {
ul { ul {
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 1".into() }, to: Route::BlogPost { name: "Blog post 1".into() },
"Read the first blog post" "Read the first blog post"
} }
} }
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 2".into() }, to: Route::BlogPost { name: "Blog post 2".into() },
"Read the second blog post" "Read the second blog post"
} }
} }
+1 -1
View File
@@ -20,7 +20,7 @@ fn main() {}
fn GoToDioxus(cx: Scope) -> Element { fn GoToDioxus(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: NavigationTarget::External("https://dioxuslabs.com".into()), to: NavigationTarget::External("https://dioxuslabs.com".into()),
"ExternalTarget target" "ExternalTarget target"
} }
} }
+4 -4
View File
@@ -48,8 +48,8 @@ fn NavBar(cx: Scope) -> Element {
render! { render! {
nav { nav {
ul { ul {
li { Link { target: Route::Home {}, "Home" } } li { Link { to: Route::Home {}, "Home" } }
li { Link { target: Route::BlogList {}, "Blog" } } li { Link { to: Route::BlogList {}, "Blog" } }
} }
} }
Outlet {} Outlet {}
@@ -78,13 +78,13 @@ fn BlogList(cx: Scope) -> Element {
ul { ul {
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 1".into() }, to: Route::BlogPost { name: "Blog post 1".into() },
"Read the first blog post" "Read the first blog post"
} }
} }
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 2".into() }, to: Route::BlogPost { name: "Blog post 2".into() },
"Read the second blog post" "Read the second blog post"
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ fn NavBar(cx: Scope) -> Element {
Link { Link {
// The Link component will navigate to the route specified // The Link component will navigate to the route specified
// in the target prop which is checked to exist at compile time // in the target prop which is checked to exist at compile time
target: Route::Home {}, to: Route::Home {},
"Home" "Home"
} }
} }
+4 -4
View File
@@ -58,12 +58,12 @@ fn ClientList(cx: Scope) -> Element {
h2 { "List of Clients" } h2 { "List of Clients" }
Link { Link {
target: Route::ClientAdd {}, to: Route::ClientAdd {},
class: "pure-button pure-button-primary", class: "pure-button pure-button-primary",
"Add Client" "Add Client"
} }
Link { Link {
target: Route::Settings {}, to: Route::Settings {},
class: "pure-button", class: "pure-button",
"Settings" "Settings"
} }
@@ -161,7 +161,7 @@ fn ClientAdd(cx: Scope) -> Element {
"Save" "Save"
} }
Link { Link {
target: Route::ClientList {}, to: Route::ClientList {},
class: "pure-button pure-button-primary red", class: "pure-button pure-button-primary red",
"Cancel" "Cancel"
} }
@@ -190,7 +190,7 @@ fn Settings(cx: Scope) -> Element {
} }
Link { Link {
target: Route::ClientList {}, to: Route::ClientList {},
class: "pure-button", class: "pure-button",
"Go back" "Go back"
} }
+4 -4
View File
@@ -48,10 +48,10 @@ fn Footer(cx: Scope) -> Element {
nav { nav {
ul { ul {
li { Link { target: Route::Home {}, "Home" } } li { Link { to: Route::Home {}, "Home" } }
li { Link { target: Route::Games {}, "Games" } } li { Link { to: Route::Games {}, "Games" } }
li { Link { target: Route::Play {}, "Play" } } li { Link { to: Route::Play {}, "Play" } }
li { Link { target: Route::Settings {}, "Settings" } } li { Link { to: Route::Settings {}, "Settings" } }
} }
} }
} }
+2 -2
View File
@@ -43,8 +43,8 @@ fn Header(cx: Scope) -> Element {
render! { render! {
h1 { "Your app here" } h1 { "Your app here" }
ul { ul {
li { Link { target: Route::Home {}, "home" } } li { Link { to: Route::Home {}, "home" } }
li { Link { target: Route::Settings {}, "settings" } } li { Link { to: Route::Settings {}, "settings" } }
} }
Outlet {} Outlet {}
} }
+4 -4
View File
@@ -48,8 +48,8 @@ fn NavBar(cx: Scope) -> Element {
render! { render! {
nav { nav {
ul { ul {
li { Link { target: Route::Home {}, "Home" } } li { Link { to: Route::Home {}, "Home" } }
li { Link { target: Route::BlogList {}, "Blog" } } li { Link { to: Route::BlogList {}, "Blog" } }
} }
} }
Outlet {} Outlet {}
@@ -78,13 +78,13 @@ fn BlogList(cx: Scope) -> Element {
ul { ul {
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 1".into() }, to: Route::BlogPost { name: "Blog post 1".into() },
"Read the first blog post" "Read the first blog post"
} }
} }
li { li {
Link { Link {
target: Route::BlogPost { name: "Blog post 2".into() }, to: Route::BlogPost { name: "Blog post 2".into() },
"Read the second blog post" "Read the second blog post"
} }
} }
+6 -6
View File
@@ -41,11 +41,11 @@ fn NavBar(cx: Scope) -> Element {
render! { render! {
h1 { "Your app here" } h1 { "Your app here" }
ul { ul {
li { Link { target: Route::Home {}, "home" } } li { Link { to: Route::Home {}, "home" } }
li { Link { target: Route::BlogList {}, "blog" } } li { Link { to: Route::BlogList {}, "blog" } }
li { Link { target: Route::BlogPost { post: "tim".into() }, "tims' blog" } } li { Link { to: Route::BlogPost { post: "tim".into() }, "tims' blog" } }
li { Link { target: Route::BlogPost { post: "bill".into() }, "bills' blog" } } li { Link { to: Route::BlogPost { post: "bill".into() }, "bills' blog" } }
li { Link { target: Route::BlogPost { post: "james".into() }, "james amazing' blog" } } li { Link { to: Route::BlogPost { post: "james".into() }, "james amazing' blog" } }
} }
Outlet {} Outlet {}
} }
@@ -70,7 +70,7 @@ fn BlogPost(cx: Scope, post: String) -> Element {
render! { render! {
div { div {
h3 { "blog post: {post}" } h3 { "blog post: {post}" }
Link { target: Route::BlogList {}, "back to blog list" } Link { to: Route::BlogList {}, "back to blog list" }
} }
} }
} }
@@ -28,7 +28,7 @@ enum Route {
#[inline_props] #[inline_props]
fn Blog(cx: Scope, id: i32) -> Element { fn Blog(cx: Scope, id: i32) -> Element {
render! { render! {
Link { target: Route::Home {}, "Go to counter" } Link { to: Route::Home {}, "Go to counter" }
table { table {
tbody { tbody {
for _ in 0..*id { for _ in 0..*id {
@@ -50,7 +50,7 @@ fn Home(cx: Scope) -> Element {
cx.render(rsx! { cx.render(rsx! {
Link { Link {
target: Route::Blog { to: Route::Blog {
id: *count.get() id: *count.get()
}, },
"Go to blog" "Go to blog"
@@ -52,7 +52,7 @@ enum Route {
#[inline_props] #[inline_props]
fn Blog(cx: Scope) -> Element { fn Blog(cx: Scope) -> Element {
render! { render! {
Link { target: Route::Home {}, "Go to counter" } Link { to: Route::Home {}, "Go to counter" }
table { table {
tbody { tbody {
for _ in 0..100 { for _ in 0..100 {
@@ -74,7 +74,7 @@ fn Home(cx: Scope) -> Element {
cx.render(rsx! { cx.render(rsx! {
Link { Link {
target: Route::Blog {}, to: Route::Blog {},
"Go to blog" "Go to blog"
} }
div { div {
+3 -3
View File
@@ -60,7 +60,7 @@ fn Index(cx: Scope) -> Element {
render! { render! {
h1 { "Index" } h1 { "Index" }
Link { Link {
target: Route::BlogList {}, to: Route::BlogList {},
"Go to the blog" "Go to the blog"
} }
} }
@@ -79,11 +79,11 @@ fn BlogList(cx: Scope) -> Element {
render! { render! {
h2 { "List of blog posts" } h2 { "List of blog posts" }
Link { Link {
target: Route::BlogPost { blog_id: 0 }, to: Route::BlogPost { blog_id: 0 },
"Blog post 1" "Blog post 1"
} }
Link { Link {
target: Route::BlogPost { blog_id: 1 }, to: Route::BlogPost { blog_id: 1 },
"Blog post 2" "Blog post 2"
} }
} }
+5 -5
View File
@@ -39,12 +39,12 @@ fn Route1(cx: Scope, user_id: usize, dynamic: usize, query: String, extra: Strin
"Route1{{\n\tuser_id:{user_id},\n\tdynamic:{dynamic},\n\tquery:{query},\n\textra:{extra}\n}}" "Route1{{\n\tuser_id:{user_id},\n\tdynamic:{dynamic},\n\tquery:{query},\n\textra:{extra}\n}}"
} }
Link { Link {
target: Route::Route1 { user_id: *user_id, dynamic: *dynamic, query: String::new(), extra: extra.clone() + "." }, to: Route::Route1 { user_id: *user_id, dynamic: *dynamic, query: String::new(), extra: extra.clone() + "." },
"Route1 with extra+\".\"" "Route1 with extra+\".\""
} }
p { "Footer" } p { "Footer" }
Link { Link {
target: Route::Route3 { dynamic: String::new() }, to: Route::Route3 { dynamic: String::new() },
"Home" "Home"
} }
} }
@@ -59,7 +59,7 @@ fn Route2(cx: Scope, user_id: usize) -> Element {
(0..*user_id).map(|i| rsx!{ p { "{i}" } }), (0..*user_id).map(|i| rsx!{ p { "{i}" } }),
p { "Footer" } p { "Footer" }
Link { Link {
target: Route::Route3 { dynamic: String::new() }, to: Route::Route3 { dynamic: String::new() },
"Home" "Home"
} }
} }
@@ -86,7 +86,7 @@ fn Route3(cx: Scope, dynamic: String) -> Element {
} }
"dynamic: {dynamic}" "dynamic: {dynamic}"
Link { Link {
target: Route::Route2 { user_id: 8888 }, to: Route::Route2 { user_id: 8888 },
"hello world link" "hello world link"
} }
button { button {
@@ -101,7 +101,7 @@ fn Route3(cx: Scope, dynamic: String) -> Element {
if route != current_route { if route != current_route {
render! { render! {
Link { Link {
target: route.clone(), to: route.clone(),
"{route}" "{route}"
} }
} }
+7 -7
View File
@@ -42,7 +42,7 @@ pub struct GenericLinkProps<'a, R: Routable> {
pub rel: Option<&'a str>, pub rel: Option<&'a str>,
/// The navigation target. Roughly equivalent to the href attribute of an HTML anchor tag. /// The navigation target. Roughly equivalent to the href attribute of an HTML anchor tag.
#[props(into)] #[props(into)]
pub target: NavigationTarget<R>, pub to: NavigationTarget<R>,
} }
impl<R: Routable> Debug for GenericLinkProps<'_, R> { impl<R: Routable> Debug for GenericLinkProps<'_, R> {
@@ -56,7 +56,7 @@ impl<R: Routable> Debug for GenericLinkProps<'_, R> {
.field("onclick", &self.onclick.as_ref().map(|_| "onclick is set")) .field("onclick", &self.onclick.as_ref().map(|_| "onclick is set"))
.field("onclick_only", &self.onclick_only) .field("onclick_only", &self.onclick_only)
.field("rel", &self.rel) .field("rel", &self.rel)
.field("target", &self.target.to_string()) .field("to", &self.to.to_string())
.finish() .finish()
} }
} }
@@ -106,7 +106,7 @@ impl<R: Routable> Debug for GenericLinkProps<'_, R> {
/// id: "link_id", /// id: "link_id",
/// new_tab: true, /// new_tab: true,
/// rel: "link_rel", /// rel: "link_rel",
/// target: Route::Index {}, /// to: Route::Index {},
/// ///
/// "A fully configured link" /// "A fully configured link"
/// } /// }
@@ -132,7 +132,7 @@ pub fn GenericLink<'a, R: Routable + Clone>(cx: Scope<'a, GenericLinkProps<'a, R
onclick, onclick,
onclick_only, onclick_only,
rel, rel,
target, to,
} = cx.props; } = cx.props;
// hook up to router // hook up to router
@@ -150,7 +150,7 @@ pub fn GenericLink<'a, R: Routable + Clone>(cx: Scope<'a, GenericLinkProps<'a, R
let current_route = router.current(); let current_route = router.current();
let current_url = current_route.to_string(); let current_url = current_route.to_string();
let href = target.to_string(); let href = to.to_string();
let ac = active_class let ac = active_class
.and_then(|active_class| (href == current_url).then(|| format!(" {active_class}"))) .and_then(|active_class| (href == current_url).then(|| format!(" {active_class}")))
.unwrap_or_default(); .unwrap_or_default();
@@ -159,7 +159,7 @@ pub fn GenericLink<'a, R: Routable + Clone>(cx: Scope<'a, GenericLinkProps<'a, R
let class = format!("{}{ac}", class.unwrap_or_default()); let class = format!("{}{ac}", class.unwrap_or_default());
let tag_target = new_tab.then_some("_blank").unwrap_or_default(); let tag_target = new_tab.then_some("_blank").unwrap_or_default();
let is_external = matches!(target, NavigationTarget::External(_)); let is_external = matches!(to, NavigationTarget::External(_));
let is_router_nav = !is_external && !new_tab; let is_router_nav = !is_external && !new_tab;
let prevent_default = is_router_nav.then_some("onclick").unwrap_or_default(); let prevent_default = is_router_nav.then_some("onclick").unwrap_or_default();
let rel = rel let rel = rel
@@ -169,7 +169,7 @@ pub fn GenericLink<'a, R: Routable + Clone>(cx: Scope<'a, GenericLinkProps<'a, R
let do_default = onclick.is_none() || !onclick_only; let do_default = onclick.is_none() || !onclick_only;
let action = move |event| { let action = move |event| {
if do_default && is_router_nav { if do_default && is_router_nav {
router.push(target.clone()); router.push(to.clone());
} }
if let Some(handler) = onclick { if let Some(handler) = onclick {
+16 -5
View File
@@ -1,7 +1,6 @@
//! Types pertaining to navigation. //! Types pertaining to navigation.
use std::{ use std::{
convert::TryFrom,
fmt::{Debug, Display}, fmt::{Debug, Display},
str::FromStr, str::FromStr,
}; };
@@ -55,11 +54,23 @@ pub enum NavigationTarget<R: Routable> {
External(String), External(String),
} }
impl<R: Routable> TryFrom<&str> for NavigationTarget<R> { impl<R: Routable> From<&str> for NavigationTarget<R> {
type Error = NavigationTargetParseError<R>; fn from(value: &str) -> Self {
value
.parse()
.unwrap_or_else(|_| Self::External(value.to_string()))
}
}
fn try_from(value: &str) -> Result<Self, Self::Error> { impl<R: Routable> From<&String> for NavigationTarget<R> {
value.parse() fn from(value: &String) -> Self {
value.as_str().into()
}
}
impl<R: Routable> From<String> for NavigationTarget<R> {
fn from(value: String) -> Self {
value.as_str().into()
} }
} }
+9 -9
View File
@@ -59,7 +59,7 @@ fn href_internal() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
"Link" "Link"
} }
} }
@@ -97,7 +97,7 @@ fn href_external() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: NavigationTarget::External("https://dioxuslabs.com/".into()), to: NavigationTarget::External("https://dioxuslabs.com/".into()),
"Link" "Link"
} }
} }
@@ -135,7 +135,7 @@ fn with_class() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
class: "test_class", class: "test_class",
"Link" "Link"
} }
@@ -167,7 +167,7 @@ fn with_active_class_active() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Root {}, to: Route::Root {},
active_class: "active_class", active_class: "active_class",
class: "test_class", class: "test_class",
"Link" "Link"
@@ -207,7 +207,7 @@ fn with_active_class_inactive() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
active_class: "active_class", active_class: "active_class",
class: "test_class", class: "test_class",
"Link" "Link"
@@ -247,7 +247,7 @@ fn with_id() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
id: "test_id", id: "test_id",
"Link" "Link"
} }
@@ -286,7 +286,7 @@ fn with_new_tab() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
new_tab: true, new_tab: true,
"Link" "Link"
} }
@@ -318,7 +318,7 @@ fn with_new_tab_external() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: NavigationTarget::External("https://dioxuslabs.com/".into()), to: NavigationTarget::External("https://dioxuslabs.com/".into()),
new_tab: true, new_tab: true,
"Link" "Link"
} }
@@ -357,7 +357,7 @@ fn with_rel() {
fn Root(cx: Scope) -> Element { fn Root(cx: Scope) -> Element {
render! { render! {
Link { Link {
target: Route::Test {}, to: Route::Test {},
rel: "test_rel", rel: "test_rel",
"Link" "Link"
} }