mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-30 16:09:12 +00:00
add history buttons
This commit is contained in:
parent
0cc0cba482
commit
398043eba0
2 changed files with 83 additions and 0 deletions
80
packages/router/src/components/history_buttons.rs
Normal file
80
packages/router/src/components/history_buttons.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
use dioxus::prelude::*;
|
||||
use dioxus_router_core::RouterMessage;
|
||||
use log::error;
|
||||
|
||||
use crate::utils::use_router_internal::use_router_internal;
|
||||
|
||||
#[derive(Debug, Props)]
|
||||
pub struct HistoryButtonProps<'a> {
|
||||
pub children: Element<'a>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(&cx) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
let msg = "`GoBackButton` must have access to a parent router";
|
||||
error!("{msg}, will be inactive");
|
||||
#[cfg(debug_assertions)]
|
||||
panic!("{}", msg);
|
||||
#[cfg(not(debug_assertions))]
|
||||
anyhow::bail!("{msg}");
|
||||
}
|
||||
};
|
||||
let state = loop {
|
||||
if let Some(state) = router.state.try_read() {
|
||||
break state;
|
||||
}
|
||||
};
|
||||
let sender = router.sender.clone();
|
||||
|
||||
let disabled = !state.can_go_back;
|
||||
|
||||
render! {
|
||||
button {
|
||||
disabled: "{disabled}",
|
||||
prevent_default: "onclick",
|
||||
onclick: move |_| { let _ = sender.unbounded_send(RouterMessage::GoBack); },
|
||||
children
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn GoForwardButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(&cx) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
let msg = "`GoForwardButton` must have access to a parent router";
|
||||
error!("{msg}, will be inactive");
|
||||
#[cfg(debug_assertions)]
|
||||
panic!("{}", msg);
|
||||
#[cfg(not(debug_assertions))]
|
||||
anyhow::bail!("{msg}");
|
||||
}
|
||||
};
|
||||
let state = loop {
|
||||
if let Some(state) = router.state.try_read() {
|
||||
break state;
|
||||
}
|
||||
};
|
||||
let sender = router.sender.clone();
|
||||
|
||||
let disabled = !state.can_go_back;
|
||||
|
||||
render! {
|
||||
button {
|
||||
disabled: "{disabled}",
|
||||
prevent_default: "onclick",
|
||||
onclick: move |_| { let _ = sender.unbounded_send(RouterMessage::GoForward); },
|
||||
children
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
pub mod components {
|
||||
pub(crate) mod default_errors;
|
||||
|
||||
mod history_buttons;
|
||||
pub use history_buttons::*;
|
||||
|
||||
mod link;
|
||||
pub use link::*;
|
||||
|
||||
|
|
Loading…
Reference in a new issue