Fix the router and stub out a number of crates to get compiling
This commit is contained in:
226 files changed
+1057
-1482
No files matched your search
@@ -4,9 +4,9 @@ use dioxus::prelude::*;
|
||||
|
||||
/// The default component to render when an external navigation fails.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn FailureExternalNavigation(cx: Scope) -> Element {
|
||||
pub fn FailureExternalNavigation() -> Element {
|
||||
#[allow(deprecated)]
|
||||
let router = use_router(cx);
|
||||
let router = use_router();
|
||||
|
||||
render! {
|
||||
h1 { "External Navigation Failure!" }
|
||||
|
||||
@@ -4,10 +4,10 @@ use tracing::error;
|
||||
use crate::utils::use_router_internal::use_router_internal;
|
||||
|
||||
/// The properties for a [`GoBackButton`] or a [`GoForwardButton`].
|
||||
#[derive(Debug, Props)]
|
||||
pub struct HistoryButtonProps<'a> {
|
||||
#[derive(Debug, Props, Clone, PartialEq)]
|
||||
pub struct HistoryButtonProps {
|
||||
/// The children to render within the generated HTML button tag.
|
||||
pub children: Element<'a>,
|
||||
pub children: Element,
|
||||
}
|
||||
|
||||
/// A button to go back through the navigation history. Similar to a browsers back button.
|
||||
@@ -31,14 +31,14 @@ pub struct HistoryButtonProps<'a> {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// GoBackButton {
|
||||
/// "go back"
|
||||
@@ -54,11 +54,14 @@ pub struct HistoryButtonProps<'a> {
|
||||
/// # );
|
||||
/// ```
|
||||
#[component]
|
||||
pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
pub fn GoBackButton(
|
||||
/// The props...
|
||||
props: HistoryButtonProps,
|
||||
) -> Element {
|
||||
let HistoryButtonProps { children } = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
@@ -103,14 +106,14 @@ pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// GoForwardButton {
|
||||
/// "go forward"
|
||||
@@ -126,11 +129,14 @@ pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
/// # );
|
||||
/// ```
|
||||
#[component]
|
||||
pub fn GoForwardButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
pub fn GoForwardButton(
|
||||
/// Props...
|
||||
props: HistoryButtonProps,
|
||||
) -> Element {
|
||||
let HistoryButtonProps { children } = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
|
||||
@@ -22,6 +22,16 @@ pub enum IntoRoutable {
|
||||
Route(Rc<dyn Any>),
|
||||
}
|
||||
|
||||
impl PartialEq for IntoRoutable {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(IntoRoutable::FromStr(a), IntoRoutable::FromStr(b)) => a == b,
|
||||
(IntoRoutable::Route(a), IntoRoutable::Route(b)) => Rc::ptr_eq(a, b),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Routable> From<R> for IntoRoutable {
|
||||
fn from(value: R) -> Self {
|
||||
IntoRoutable::Route(Rc::new(value) as Rc<dyn Any>)
|
||||
@@ -68,26 +78,26 @@ impl From<&Url> for IntoRoutable {
|
||||
}
|
||||
|
||||
/// The properties for a [`Link`].
|
||||
#[derive(Props)]
|
||||
pub struct LinkProps<'a> {
|
||||
#[derive(Props, Clone, PartialEq)]
|
||||
pub struct LinkProps {
|
||||
/// A class to apply to the generate HTML anchor tag if the `target` route is active.
|
||||
pub active_class: Option<&'a str>,
|
||||
pub active_class: Option<String>,
|
||||
/// The children to render within the generated HTML anchor tag.
|
||||
pub children: Element<'a>,
|
||||
pub children: Element,
|
||||
/// The class attribute for the generated HTML anchor tag.
|
||||
///
|
||||
/// If `active_class` is [`Some`] and the `target` route is active, `active_class` will be
|
||||
/// appended at the end of `class`.
|
||||
pub class: Option<&'a str>,
|
||||
pub class: Option<String>,
|
||||
/// The id attribute for the generated HTML anchor tag.
|
||||
pub id: Option<&'a str>,
|
||||
pub id: Option<String>,
|
||||
/// When [`true`], the `target` route will be opened in a new tab.
|
||||
///
|
||||
/// This does not change whether the [`Link`] is active or not.
|
||||
#[props(default)]
|
||||
pub new_tab: bool,
|
||||
/// The onclick event handler.
|
||||
pub onclick: Option<EventHandler<'a, MouseEvent>>,
|
||||
pub onclick: Option<EventHandler<MouseEvent>>,
|
||||
#[props(default)]
|
||||
/// Whether the default behavior should be executed if an `onclick` handler is provided.
|
||||
///
|
||||
@@ -98,14 +108,14 @@ pub struct LinkProps<'a> {
|
||||
pub onclick_only: bool,
|
||||
/// The rel attribute for the generated HTML anchor tag.
|
||||
///
|
||||
/// For external `target`s, this defaults to `noopener noreferrer`.
|
||||
pub rel: Option<&'a str>,
|
||||
/// For external `a`s, this defaults to `noopener noreferrer`.
|
||||
pub rel: Option<String>,
|
||||
/// The navigation target. Roughly equivalent to the href attribute of an HTML anchor tag.
|
||||
#[props(into)]
|
||||
pub to: IntoRoutable,
|
||||
}
|
||||
|
||||
impl Debug for LinkProps<'_> {
|
||||
impl Debug for LinkProps {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("LinkProps")
|
||||
.field("active_class", &self.active_class)
|
||||
@@ -150,14 +160,14 @@ impl Debug for LinkProps<'_> {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// render! {
|
||||
/// Link {
|
||||
@@ -182,7 +192,7 @@ impl Debug for LinkProps<'_> {
|
||||
/// # );
|
||||
/// ```
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
pub fn Link(props: LinkProps) -> Element {
|
||||
let LinkProps {
|
||||
active_class,
|
||||
children,
|
||||
@@ -194,10 +204,10 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
rel,
|
||||
to,
|
||||
..
|
||||
} = cx.props;
|
||||
} = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
@@ -210,7 +220,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
};
|
||||
|
||||
let current_url = router.current_route_string();
|
||||
let href = match to {
|
||||
let href = match &to {
|
||||
IntoRoutable::FromStr(url) => url.to_string(),
|
||||
IntoRoutable::Route(route) => router.any_route_to_string(&**route),
|
||||
};
|
||||
@@ -227,16 +237,17 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
let is_router_nav = !is_external && !new_tab;
|
||||
let prevent_default = is_router_nav.then_some("onclick").unwrap_or_default();
|
||||
let rel = rel
|
||||
.or_else(|| is_external.then_some("noopener noreferrer"))
|
||||
.or_else(|| is_external.then_some("noopener noreferrer".to_string()))
|
||||
.unwrap_or_default();
|
||||
|
||||
let do_default = onclick.is_none() || !onclick_only;
|
||||
|
||||
let action = move |event| {
|
||||
if do_default && is_router_nav {
|
||||
router.push_any(router.resolve_into_routable(to.clone()));
|
||||
}
|
||||
|
||||
if let Some(handler) = onclick {
|
||||
if let Some(handler) = onclick.clone() {
|
||||
handler.call(event);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,7 +30,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// div {
|
||||
/// "Index"
|
||||
@@ -39,7 +39,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Wrapper(cx: Scope) -> Element {
|
||||
/// fn Wrapper() -> Element {
|
||||
/// render! {
|
||||
/// h1 { "App" }
|
||||
/// Outlet::<Route> {} // The content of child routes will be rendered here
|
||||
@@ -47,7 +47,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Child(cx: Scope) -> Element {
|
||||
/// fn Child() -> Element {
|
||||
/// render! {
|
||||
/// p {
|
||||
/// "Child"
|
||||
@@ -56,7 +56,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// # #[component]
|
||||
/// # fn App(cx: Scope) -> Element {
|
||||
/// # fn App() -> Element {
|
||||
/// # render! {
|
||||
/// # Router::<Route> {
|
||||
/// # config: || RouterConfig::default().history(MemoryHistory::with_initial_path(Route::Child {}))
|
||||
@@ -68,6 +68,6 @@ use dioxus::prelude::*;
|
||||
/// # let _ = vdom.rebuild();
|
||||
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
|
||||
/// ```
|
||||
pub fn Outlet<R: Routable + Clone>(cx: Scope) -> Element {
|
||||
OutletContext::<R>::render(cx)
|
||||
pub fn Outlet<R: Routable + Clone>() -> Element {
|
||||
OutletContext::<R>::render()
|
||||
}
|
||||
@@ -61,6 +61,15 @@ where
|
||||
config: RouterConfigFactory<R>,
|
||||
}
|
||||
|
||||
impl<T: Routable> Clone for RouterProps<T>
|
||||
where
|
||||
<T as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
impl<R: Routable> Default for RouterProps<R>
|
||||
where
|
||||
@@ -111,26 +120,27 @@ where
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
/// A component that renders the current route.
|
||||
pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
|
||||
pub fn Router<R: Routable + Clone>(props: RouterProps<R>) -> Element
|
||||
where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
use crate::prelude::{outlet::OutletContext, RouterContext};
|
||||
|
||||
use_context_provider(cx, || {
|
||||
RouterContext::new(
|
||||
(cx.props
|
||||
.config
|
||||
.config
|
||||
.take()
|
||||
.expect("use_context_provider ran twice"))(),
|
||||
cx.schedule_update_any(),
|
||||
)
|
||||
});
|
||||
use_context_provider(cx, || OutletContext::<R> {
|
||||
current_level: 0,
|
||||
_marker: std::marker::PhantomData,
|
||||
});
|
||||
todo!();
|
||||
// use_context_provider(|| {
|
||||
// RouterContext::new(
|
||||
// (props
|
||||
// .config
|
||||
// .config
|
||||
// .take()
|
||||
// .expect("use_context_provider ran twice"))(),
|
||||
// schedule_update_any(),
|
||||
// )
|
||||
// });
|
||||
// use_context_provider(|| OutletContext::<R> {
|
||||
// current_level: 0,
|
||||
// _marker: std::marker::PhantomData,
|
||||
// });
|
||||
|
||||
render! {
|
||||
Outlet::<R> {}
|
||||
@@ -144,7 +154,7 @@ where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
R: serde::Serialize + serde::de::DeserializeOwned,
|
||||
{
|
||||
use_context_provider(cx, || {
|
||||
use_context_provider(|| {
|
||||
RouterContext::new(
|
||||
(cx.props
|
||||
.config
|
||||
@@ -154,7 +164,7 @@ where
|
||||
cx.schedule_update_any(),
|
||||
)
|
||||
});
|
||||
use_context_provider(cx, || OutletContext::<R> {
|
||||
use_context_provider(|| OutletContext::<R> {
|
||||
current_level: 0,
|
||||
_marker: std::marker::PhantomData,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user