Skip to main content

yew_tincture/components/
button.rs

1use yew::{classes, html, Callback, Component, Context, Html, Properties};
2
3pub struct Button {}
4
5pub enum Msg {
6    Clicked,
7}
8
9#[derive(PartialEq, Properties)]
10pub struct Props {
11    #[prop_or_default]
12    pub title: String,
13    pub onsignal: Callback<()>,
14    #[prop_or_default]
15    pub disabled: bool,
16    #[prop_or_default]
17    pub is_active: bool,
18}
19
20impl Component for Button {
21    type Message = Msg;
22    type Properties = Props;
23
24    fn create(_ctx: &Context<Self>) -> Self {
25        Self {}
26    }
27
28    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
29        match msg {
30            Msg::Clicked => {
31                ctx.props().onsignal.emit(());
32            }
33        }
34        false
35    }
36
37    fn view(&self, ctx: &Context<Self>) -> Html {
38        let active_class = if ctx.props().is_active {
39            "btn-active"
40        } else {
41            "btn-inactive"
42        };
43        html! {
44            <button
45                class={classes!("btn",active_class)}
46                onclick={ctx.link().callback(|_| Msg::Clicked)}
47                disabled={ctx.props().disabled}>{ &ctx.props().title }
48            </button>
49        }
50    }
51}