1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use yew::prelude::*;

#[derive(Clone)]
pub enum ButtonType {
    Primary,
    Secondary,
    Success,
    Info,
    Link,
    Warning,
    Danger,
    Standard,
}

#[derive(Clone)]
pub enum ButtonStyle {
    Regular,
    Light,
    Outline,
}

#[derive(Clone)]
pub enum Size {
    Small,
    Medium,
    Big,
}

pub struct Button {
    link: ComponentLink<Self>,
    props: ButtonProps,
}

struct ButtonProps {
    button_type: String,
    size: String,
    button_style: String,
    class_name: String,
    onsignal: Callback<()>,
    children: Children,
}

impl From<Props> for ButtonProps {
    fn from(props: Props) -> Self {
        ButtonProps {
            button_type: get_button_type(props.button_type),
            size: get_size(props.size),
            button_style: get_button_style(props.button_style),
            class_name: props.class_name,
            onsignal: props.onsignal,
            children: props.children,
        }
    }
}

#[derive(Clone, Properties)]
pub struct Props {
    pub button_type: ButtonType,
    pub class_name: String,
    pub size: Size,
    pub button_style: ButtonStyle,
    #[props(required)]
    pub onsignal: Callback<()>,
    pub children: Children,
}

pub enum Msg {
    Clicked,
}

pub fn get_button_type(button_type: ButtonType) -> String {
    match button_type {
        ButtonType::Primary => String::from("primary"),
        ButtonType::Secondary => String::from("secondary"),
        ButtonType::Info => String::from("info"),
        ButtonType::Link => String::from("link"),
        ButtonType::Success => String::from("success"),
        ButtonType::Warning => String::from("warning"),
        ButtonType::Danger => String::from("danger"),
        ButtonType::Standard => String::from("standard"),
    }
}

pub fn get_size(size: Size) -> String {
    match size {
        Size::Small => String::from("small"),
        Size::Medium => String::from("medium"),
        Size::Big => String::from("big"),
    }
}

pub fn get_button_style(button_style: ButtonStyle) -> String {
    match button_style {
        ButtonStyle::Regular => String::from("regular"),
        ButtonStyle::Light => String::from("light"),
        ButtonStyle::Outline => String::from("outline"),
    }
}

impl Default for Size {
    fn default() -> Self {
        Size::Medium
    }
}

impl Default for ButtonType {
    fn default() -> Self {
        ButtonType::Standard
    }
}

impl Default for ButtonStyle {
    fn default() -> Self {
        ButtonStyle::Regular
    }
}

impl Component for Button {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Button {
            link,
            props: ButtonProps::from(props),
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Clicked => {
                self.props.onsignal.emit(());
            }
        };

        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.props = ButtonProps::from(props);
        true
    }

    fn view(&self) -> Html {
        html! {
            <button
                onclick=self.link.callback(|_| Msg::Clicked)
                class=format!("button {} {} {} {}",
                    self.props.button_type.clone(),
                    self.props.size.clone(),
                    self.props.button_style.clone(),
                    self.props.class_name.clone())
            > { self.props.children.render() }
            </button>
        }
    }
}