create example

This commit is contained in:
Evan Almloff committed 2023-05-23 19:09:24 -05:00
1 parent d0d7e88a0d
commit 3678fa1d3f
6 files changed
+158 -79

No files matched your search

+12 -3
View File
@@ -13,7 +13,10 @@ pub struct RouterCfg<R: Routable> {
config: RefCell<Option<RouterConfiguration<R>>>,
}
impl<R: Routable> Default for RouterCfg<R> {
impl<R: Routable> Default for RouterCfg<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
config: RefCell::new(Some(RouterConfiguration::default())),
@@ -31,14 +34,20 @@ impl<R: Routable> From<RouterConfiguration<R>> for RouterCfg<R> {
/// The props for [`Router`].
#[derive(Props)]
pub struct RouterProps<R: Routable> {
pub struct RouterProps<R: Routable>
where
<R as FromStr>::Err: std::fmt::Display,
{
#[props(into)]
initial_url: Option<String>,
#[props(default, into)]
config: RouterCfg<R>,
}
impl<R: Routable> PartialEq for RouterProps<R> {
impl<R: Routable> PartialEq for RouterProps<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
fn eq(&self, _: &Self) -> bool {
// prevent the router from re-rendering when the initial url or config changes
true
+19 -19
View File
@@ -6,12 +6,15 @@ use super::HistoryProvider;
/// A [`HistoryProvider`] that stores all navigation information in memory.
pub struct MemoryHistory<R: Routable> {
current: Option<R>,
current: R,
history: Vec<R>,
future: Vec<R>,
}
impl<R: Routable> MemoryHistory<R> {
impl<R: Routable> MemoryHistory<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
/// Create a [`MemoryHistory`] starting at `path`.
///
/// ```rust
@@ -24,16 +27,19 @@ impl<R: Routable> MemoryHistory<R> {
let path = path.into();
Ok(Self {
current: Some(R::from_str(&path)?),
current: R::from_str(&path)?,
..Default::default()
})
}
}
impl<R: Routable> Default for MemoryHistory<R> {
impl<R: Routable> Default for MemoryHistory<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
current: None,
current: "/".parse().unwrap_or_else(|err| panic!("{}", err)),
history: Vec::new(),
future: Vec::new(),
}
@@ -42,7 +48,7 @@ impl<R: Routable> Default for MemoryHistory<R> {
impl<R: Routable> HistoryProvider<R> for MemoryHistory<R> {
fn current_route(&self) -> R {
self.current.clone().expect("current route is not set")
self.current.clone()
}
fn can_go_back(&self) -> bool {
@@ -51,10 +57,8 @@ impl<R: Routable> HistoryProvider<R> for MemoryHistory<R> {
fn go_back(&mut self) {
if let Some(last) = self.history.pop() {
let new = self.current.replace(last);
if let Some(new) = new {
self.future.push(new);
}
let old = std::mem::replace(&mut self.current, last);
self.future.push(old);
}
}
@@ -64,23 +68,19 @@ impl<R: Routable> HistoryProvider<R> for MemoryHistory<R> {
fn go_forward(&mut self) {
if let Some(next) = self.future.pop() {
let old = self.current.replace(next);
if let Some(old) = old {
self.history.push(old);
}
let old = std::mem::replace(&mut self.current, next);
self.history.push(old);
}
}
fn push(&mut self, new: R) {
if let Some(current) = self.current.take() {
self.history.push(current);
}
self.current = Some(new);
let old = std::mem::replace(&mut self.current, new);
self.history.push(old);
self.future.clear();
}
fn replace(&mut self, path: R) {
self.current = Some(path);
self.current = path;
}
}
+1
View File
@@ -46,6 +46,7 @@ pub mod hooks {
pub mod prelude {
pub use crate::components::*;
pub use crate::contexts::*;
pub use crate::history::*;
pub use crate::hooks::*;
pub use crate::routable::*;
pub use crate::router_cfg::RouterConfiguration;
+4 -1
View File
@@ -58,7 +58,10 @@ pub struct RouterConfiguration<R: Routable> {
pub on_update: Option<RoutingCallback<R>>,
}
impl<R: Routable + Clone> Default for RouterConfiguration<R> {
impl<R: Routable + Clone> Default for RouterConfiguration<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
failure_external_navigation: FailureExternalNavigation::<R>,