Merge branch 'master' into fullstack-binary-size-compile-times

This commit is contained in:
Evan Almloff committed 2024-01-08 13:18:05 -06:00
commit b180b501f7
310 files changed
+13994 -4978

No files matched your search

+27 -18
View File
@@ -55,9 +55,7 @@ fn main() {
#[component]
fn Root(cx: Scope) -> Element {
let history = LiveviewHistory::new(cx);
render! { Router::<Route> {
config: || RouterConfig::default().history(history),
} }
render! { Router::<Route> { config: || RouterConfig::default().history(history) } }
}
#[cfg(not(feature = "liveview"))]
@@ -84,12 +82,19 @@ 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}}"
}
Link {
to: 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+\".\""
}
p { "Footer" }
Link {
to: Route::Route3 { dynamic: String::new() },
to: Route::Route3 {
dynamic: String::new(),
},
"Home"
}
}
@@ -98,13 +103,13 @@ fn Route1(cx: Scope, user_id: usize, dynamic: usize, query: String, extra: Strin
#[component]
fn Route2(cx: Scope, user_id: usize) -> Element {
render! {
pre {
"Route2{{\n\tuser_id:{user_id}\n}}"
}
pre { "Route2{{\n\tuser_id:{user_id}\n}}" }
(0..*user_id).map(|i| rsx!{ p { "{i}" } }),
p { "Footer" }
Link {
to: Route::Route3 { dynamic: String::new() },
to: Route::Route3 {
dynamic: String::new(),
},
"Home"
}
}
@@ -112,7 +117,6 @@ fn Route2(cx: Scope, user_id: usize) -> Element {
#[component]
fn Route3(cx: Scope, dynamic: String) -> Element {
let navigator = use_navigator(cx);
let current_route = use_route(cx)?;
let current_route_str = use_ref(cx, String::new);
let parsed = Route::from_str(&current_route_str.read());
@@ -122,30 +126,35 @@ fn Route3(cx: Scope, dynamic: String) -> Element {
.flat_map(|seg| seg.flatten().into_iter())
.collect::<Vec<_>>();
let navigator = use_navigator(cx);
render! {
input {
oninput: move |evt| {
*current_route_str.write() = evt.value.clone();
*current_route_str.write() = evt.value();
},
value: "{current_route_str.read()}"
}
"dynamic: {dynamic}"
Link {
to: Route::Route2 { user_id: 8888 },
"hello world link"
}
Link { to: Route::Route2 { user_id: 8888 }, "hello world link" }
button {
disabled: !navigator.can_go_back(),
onclick: move |_| { navigator.go_back(); },
onclick: move |_| {
navigator.go_back();
},
"go back"
}
button {
disabled: !navigator.can_go_forward(),
onclick: move |_| { navigator.go_forward(); },
onclick: move |_| {
navigator.go_forward();
},
"go forward"
}
button {
onclick: move |_| { navigator.push("https://www.google.com"); },
onclick: move |_| {
navigator.push("https://www.google.com");
},
"google link"
}
p { "Site Map" }
+14
View File
@@ -1,5 +1,19 @@
use crate::prelude::{ExternalNavigationFailure, IntoRoutable, RouterContext};
/// Acquire the navigator without subscribing to updates.
///
/// Can be called anywhere in the application provided a Router has been initialized.
///
/// ## Panics
///
/// Panics if there is no router present.
pub fn navigator() -> Navigator {
Navigator(
dioxus::core::prelude::consume_context::<RouterContext>()
.expect("A router must be present to use navigator"),
)
}
/// A view into the navigation state of a router.
#[derive(Clone)]
pub struct Navigator(pub(crate) RouterContext);
+6 -1
View File
@@ -2,7 +2,7 @@ use dioxus::prelude::ScopeState;
use crate::{prelude::RouterContext, utils::use_router_internal::use_router_internal};
#[deprecated = "prefer the use_navigator or use_route functions"]
#[deprecated = "prefer the `router()` function or `use_route` functions"]
#[must_use]
/// A hook that provides access to information about the router.
pub fn use_router(cx: &ScopeState) -> &RouterContext {
@@ -10,3 +10,8 @@ pub fn use_router(cx: &ScopeState) -> &RouterContext {
.as_ref()
.expect("use_route must have access to a router")
}
/// Aquire the router without subscribing to updates.
pub fn router() -> RouterContext {
dioxus::core::prelude::consume_context().unwrap()
}
+2
View File
@@ -53,6 +53,8 @@ pub mod hooks {
pub use use_navigator::*;
}
pub use hooks::router;
/// A collection of useful items most applications might need.
pub mod prelude {
pub use crate::components::*;
+36 -1
View File
@@ -24,7 +24,7 @@ impl<E: Display> Display for RouteParseError<E> {
}
}
/// Something that can be created from a query string.
/// Something that can be created from an entire query string.
///
/// This trait needs to be implemented if you want to turn a query string into a struct.
///
@@ -40,6 +40,41 @@ impl<T: for<'a> From<&'a str>> FromQuery for T {
}
}
/// Something that can be created from a query argument.
///
/// This trait must be implemented for every type used within a query string in the router macro.
pub trait FromQueryArgument: Default {
/// The error that can occur when parsing a query argument.
type Err;
/// Create an instance of `Self` from a query string.
fn from_query_argument(argument: &str) -> Result<Self, Self::Err>;
}
impl<T: Default + FromStr> FromQueryArgument for T
where
<T as FromStr>::Err: Display,
{
type Err = <T as FromStr>::Err;
fn from_query_argument(argument: &str) -> Result<Self, Self::Err> {
let result = match urlencoding::decode(argument) {
Ok(argument) => T::from_str(&argument),
Err(err) => {
tracing::error!("Failed to decode url encoding: {}", err);
T::from_str(argument)
}
};
match result {
Ok(result) => Ok(result),
Err(err) => {
tracing::error!("Failed to parse query argument: {}", err);
Err(err)
}
}
}
}
/// Something that can be created from a route segment.
pub trait FromRouteSegment: Sized {
/// The error that can occur when parsing a route segment.