feat: add prevent default attribute and upgrade router
This commit is contained in:
1 parent
cb2782b4bb
commit
427b126bc1
16 files changed
+220
-181
No files matched your search
@@ -37,10 +37,8 @@ pub struct LinkProps<'a> {
|
||||
attributes: Option<&'a [Attribute<'a>]>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
log::debug!("rendering link {:?}", cx.scope_id());
|
||||
let service = cx.consume_context::<RouterService>()?;
|
||||
let service = cx.consume_context::<RouterService>().unwrap();
|
||||
cx.render(rsx! {
|
||||
a {
|
||||
href: "{cx.props.to}",
|
||||
@@ -48,10 +46,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
id: format_args!("{}", cx.props.id.unwrap_or("")),
|
||||
|
||||
prevent_default: "onclick",
|
||||
onclick: move |_| {
|
||||
log::debug!("clicked");
|
||||
service.push_route(cx.props.to.clone());
|
||||
},
|
||||
onclick: move |_| service.push_route(cx.props.to.clone()),
|
||||
|
||||
&cx.props.children
|
||||
}
|
||||
|
||||
@@ -11,7 +11,11 @@ use crate::{RouteContext, RouterService};
|
||||
#[derive(Props)]
|
||||
pub struct RouteProps<'a> {
|
||||
to: &'a str,
|
||||
|
||||
children: Element<'a>,
|
||||
|
||||
#[props(default)]
|
||||
fallback: bool,
|
||||
}
|
||||
|
||||
pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
@@ -34,15 +38,18 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
});
|
||||
|
||||
// submit our rout
|
||||
router_root.register_total_route(route_context.total_route.clone(), cx.scope_id());
|
||||
router_root.register_total_route(
|
||||
route_context.total_route.clone(),
|
||||
cx.scope_id(),
|
||||
cx.props.fallback,
|
||||
);
|
||||
|
||||
Some(RouteInner {})
|
||||
});
|
||||
|
||||
log::debug!("rendering route {:?}", cx.scope_id());
|
||||
log::debug!("Checking route {}", cx.props.to);
|
||||
|
||||
if router_root.should_render(cx.scope_id()) {
|
||||
log::debug!("route {:?} produced nodes", cx.scope_id());
|
||||
cx.render(rsx!(&cx.props.children))
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -15,14 +15,13 @@ pub struct RouterProps<'a> {
|
||||
onchange: Option<&'a Fn(&'a str)>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
|
||||
let p = cx.use_hook(|_| {
|
||||
cx.use_hook(|_| {
|
||||
let update = cx.schedule_update_any();
|
||||
cx.provide_context(RouterService::new(update, cx.scope_id()))
|
||||
});
|
||||
|
||||
log::debug!("rendering router {:?}", cx.scope_id());
|
||||
|
||||
cx.render(rsx!(
|
||||
div { &cx.props.children }
|
||||
))
|
||||
|
||||
Reference in new issue
Block a user