Przeglądaj źródła

fix: tests pass

Jonathan Kelley 3 lat temu
rodzic
commit
c7627f0b93

+ 3 - 2
examples/router.rs

@@ -31,7 +31,7 @@ fn app(cx: Scope) -> Element {
 }
 
 fn BlogPost(cx: Scope) -> Element {
-    let post = dioxus::router::use_route(&cx).last_segment();
+    let post = dioxus::router::use_route(&cx).last_segment()?;
 
     cx.render(rsx! {
         div {
@@ -47,7 +47,8 @@ struct Query {
 }
 
 fn User(cx: Scope) -> Element {
-    let post = dioxus::router::use_route(&cx).last_segment();
+    let post = dioxus::router::use_route(&cx).last_segment()?;
+
     let query = dioxus::router::use_route(&cx)
         .query::<Query>()
         .unwrap_or(Query { bold: false });

+ 7 - 5
packages/router/Cargo.toml

@@ -35,12 +35,13 @@ gloo-events = { version = "0.1.1", optional = true }
 log = "0.4.14"
 thiserror = "1.0.30"
 futures-util = "0.3.21"
-
+serde = { version = "1", optional = true }
+serde_urlencoded = {version = "0.7.1", optional = true}
 
 [features]
-default = ["web"]
+default = ["query"]
 web = ["web-sys", "gloo-events", "js-sys", "wasm-bindgen"]
-hash = []
+query = ["serde"]
 
 [dev-dependencies]
 console_error_panic_hook = "0.1.7"
@@ -50,10 +51,11 @@ wasm-logger = "0.2.0"
 wasm-bindgen-test = "0.3"
 gloo-utils = "0.1.2"
 dioxus-ssr = { path = "../ssr"}
+
 dioxus-router = { path = ".", default-features = false }
 
-# [target.wasm32-unknown-unknown.dev-dependencies]
-# dioxus-router = { path = ".", features = ["web"] }
+[target.wasm32-unknown-unknown.dev-dependencies]
+dioxus-router = { path = ".", features = ["web"] }
 
 
 

+ 1 - 1
packages/router/README.md

@@ -51,7 +51,7 @@
 
 Dioxus Router is a first-party Router for all your Dioxus Apps. It provides a React-Router style interface that works anywhere: across the browser, SSR, and natively.
 
-```rust
+```rust, ignore
 fn app() {
     cx.render(rsx! {
         Router {

+ 2 - 2
packages/router/src/components/link.rs

@@ -11,7 +11,7 @@ use dioxus_html as dioxus_elements;
 pub struct LinkProps<'a> {
     /// The route to link to. This can be a relative path, or a full URL.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// // Absolute path
     /// Link { to: "/home", "Go Home" }
     ///
@@ -64,7 +64,7 @@ pub struct LinkProps<'a> {
 ///
 /// # Examples
 ///
-/// ```rust
+/// ```rust, ignore
 /// fn Header(cx: Scope) -> Element {
 ///     cx.render(rsx!{
 ///         Link { to: "/home", "Go Home" }

+ 2 - 2
packages/router/src/components/redirect.rs

@@ -9,7 +9,7 @@ use crate::use_router;
 pub struct RedirectProps<'a> {
     /// The route to link to. This can be a relative path, or a full URL.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// // Absolute path
     /// Redirect { from: "", to: "/home" }
     ///
@@ -20,7 +20,7 @@ pub struct RedirectProps<'a> {
 
     /// The route to link from. This can be a relative path, or a full URL.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// // Absolute path
     /// Redirect { from: "", to: "/home" }
     ///

+ 1 - 1
packages/router/src/components/route.rs

@@ -22,7 +22,7 @@ pub struct RouteProps<'a> {
 ///
 /// # Example
 ///
-///```rust
+///```rust, ignore
 /// rsx!(
 ///     Router {
 ///         Route { to: "/home", Home {} }

+ 10 - 1
packages/router/src/hooks/use_route.rs

@@ -48,7 +48,16 @@ impl UseRoute {
     /// Get the first query parameter given the parameter name.
     ///
     /// If you need to get more than one parameter, use [`query_pairs`] on the [`Url`] instead.
-    pub fn query(&self, param: &str) -> Option<Cow<str>> {
+    #[cfg(feature = "query")]
+    pub fn query<T: serde::de::DeserializeOwned>(&self) -> Option<T> {
+        let query = self.url().query()?;
+        serde_urlencoded::from_str(query.strip_prefix('?').unwrap_or("")).ok()
+    }
+
+    /// Get the first query parameter given the parameter name.
+    ///
+    /// If you need to get more than one parameter, use [`query_pairs`] on the [`Url`] instead.
+    pub fn query_param(&self, param: &str) -> Option<Cow<str>> {
         self.route
             .url
             .query_pairs()

+ 2 - 2
packages/router/src/routecontext.rs

@@ -8,7 +8,7 @@ pub struct RouteContext {
     ///
     ///
     /// It follows this pattern:
-    /// ```
+    /// ```ignore
     /// "name/:id"
     /// ```
     pub declared_route: String,
@@ -17,7 +17,7 @@ pub struct RouteContext {
     ///
     ///
     /// It follows this pattern:
-    /// ```
+    /// ```ignore
     /// "/level0/level1/:id"
     /// ```
     pub total_route: String,

+ 3 - 1
packages/router/src/service.rs

@@ -29,7 +29,7 @@ use url::Url;
 ///
 /// # Example
 ///
-/// ```rust
+/// ```rust, ignore
 /// let router = Router::new();
 /// router.push_route("/home/custom");
 /// cx.provide_context(router);
@@ -280,6 +280,8 @@ mod hash {
                 serialized_state: None,
             }
         }
+
+        fn replace(&self, _route: &ParsedRoute) {}
     }
 }
 

+ 2 - 1
packages/router/tests/ssr_router.rs

@@ -12,13 +12,14 @@ fn generates_without_error() {
 
     let out = dioxus_ssr::render_vdom(&app);
 
-    assert_eq!(out, "<nav>navbar</nav><h1>Home</h1>");
+    assert_eq!(out, "<nav>navbar</nav>default<!--placeholder-->");
 }
 
 fn app(cx: Scope) -> Element {
     cx.render(rsx! {
         Router {
             nav { "navbar" }
+            Route { to: "/", "default" }
             Route { to: "/home", Home {} }
         }
     })