forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Dispatchers, which act like bridges, but don't require a callback to create; updated router example * cargo fmt * improve comment * Another approach * add newtype around dispatcher bridges * added debug impl, run cargo fmt * fix example * make button on routing example start on loading variant * revert singleton_id changes * actually revert singleton_id changes * slabs own option<callback> * cargo fmt * remove dead lines * address bad doc comment * fix router example * fix handler id initialization in local agent * add appropriate error message when id is not associated with callback * remove misleading comment * use a type alias to the shared output slab
- Loading branch information
1 parent
2dd60cb
commit 39d0079
Showing
4 changed files
with
225 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//! A component wrapping a <button/> tag that changes the route. | ||
use crate::router::Route; | ||
use crate::router::Router; | ||
use yew::agent::Dispatched; | ||
use yew::prelude::*; | ||
|
||
use crate::router::Request; | ||
use yew::agent::Dispatcher; | ||
|
||
/// Changes the route when clicked. | ||
pub struct RouterButton { | ||
router: Dispatcher<Router<()>>, | ||
props: Props, | ||
} | ||
|
||
pub enum Msg { | ||
Clicked, | ||
} | ||
|
||
/// Properties for Routing Components | ||
#[derive(Properties, Default, Clone, Debug, PartialEq)] | ||
pub struct Props { | ||
/// The route that will be set when the component is clicked. | ||
pub path: String, | ||
/// The text to display. | ||
pub text: String, | ||
/// Disable the component. | ||
pub disabled: bool, | ||
/// Classes to be added to component. | ||
pub classes: String, | ||
} | ||
|
||
impl Component for RouterButton { | ||
type Message = Msg; | ||
type Properties = Props; | ||
|
||
fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self { | ||
let router = Router::dispatcher(); | ||
|
||
RouterButton { router, props } | ||
} | ||
|
||
fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
match msg { | ||
Msg::Clicked => { | ||
let route = Route { | ||
path_segments: self | ||
.props | ||
.path | ||
.split("/") | ||
.skip(1) | ||
.map(|s| s.to_string()) | ||
.collect::<Vec<_>>(), | ||
query: None, | ||
state: (), | ||
fragment: None, | ||
}; | ||
self.router.send(Request::ChangeRoute(route)); | ||
false | ||
} | ||
} | ||
} | ||
fn change(&mut self, props: Self::Properties) -> ShouldRender { | ||
self.props = props; | ||
true | ||
} | ||
} | ||
|
||
impl Renderable<RouterButton> for RouterButton { | ||
fn view(&self) -> Html<RouterButton> { | ||
html! { | ||
<button | ||
class=self.props.classes.clone(), | ||
onclick=|_| Msg::Clicked, | ||
disabled=self.props.disabled, | ||
> | ||
{&self.props.text} | ||
</button> | ||
} | ||
} | ||
} |
Oops, something went wrong.