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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::{Action, Button, Icon, Variant};

use yew::prelude::*;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
    Default,
    Info,
    Success,
    Warning,
    Danger,
}

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

impl Type {
    pub fn as_classes(&self) -> Vec<&str> {
        match self {
            Type::Default => vec![],
            Type::Info => vec!["pf-m-info"],
            Type::Success => vec!["pf-m-success"],
            Type::Warning => vec!["pf-m-warning"],
            Type::Danger => vec!["pf-m-danger"],
        }
    }

    pub fn aria_label(&self) -> &'static str {
        match self {
            Type::Default => "Default alert",
            Type::Info => "Information alert",
            Type::Success => "Success alert",
            Type::Warning => "Warning alert",
            Type::Danger => "Danger alert",
        }
    }

    pub fn icon(&self) -> Icon {
        match self {
            Type::Default => Icon::Bell,
            Type::Info => Icon::InfoCircle,
            Type::Success => Icon::CheckCircle,
            Type::Warning => Icon::ExclamationTriangle,
            Type::Danger => Icon::ExclamationCircle,
        }
    }
}

#[derive(Clone, PartialEq, Properties)]
pub struct Props {
    #[prop_or_default]
    pub id: String,
    #[prop_or_default]
    pub r#type: Type,
    pub title: String,
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub inline: bool,
    #[prop_or_default]
    pub truncate: bool,
    #[prop_or_default]
    pub actions: Vec<Action>,
    #[prop_or_default]
    pub onclose: Option<Callback<()>>,
}

pub struct Alert {
    props: Props,
}

impl Component for Alert {
    type Message = ();
    type Properties = Props;

    fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
        Self { props }
    }

    fn update(&mut self, _msg: Self::Message) -> ShouldRender {
        true
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-alert");

        classes = classes.extend(self.props.r#type.as_classes());

        if self.props.inline {
            classes.push("pf-m-inline");
        }

        if self.props.truncate {
            classes.push("pf-m-truncate");
        }

        let t = self.props.r#type;

        let actions = if self.props.actions.is_empty() {
            html! {}
        } else {
            html! {
                <div class="pf-c-alert__action-group">
                    {for self.props.actions.iter().map(|action|{
                        html!{
                            <Button variant=Variant::InlineLink label=&action.label onclick=action.callback.reform(|_|())/>
                        }
                    })}
                </div>
            }
        };

        return html! {
            <div id=self.props.id class=classes aria_label=t.aria_label()>
                <div class="pf-c-alert__icon">{ t.icon() }</div>
                <div class="pf-c-alert__title">
                    <strong>
                        <span class="pf-screen-reader">{t.aria_label()}{":"}</span>
                        { &self.props.title }
                    </strong>
                </div>

                {
                    if let Some(onclose) = self.props.onclose.as_ref() {
                        html!{
                            <div class="pf-c-alert__action">
                                <Button variant=Variant::Plain icon=Icon::Times onclick=onclose.clone().reform(|_|())/>
                            </div>
                        }
                    } else {
                        html!{}
                    }
                }

                {
                    if self.props.children.len() > 0 {
                        html!{
                            <div class="pf-c-alert__description">
                                { for self.props.children.iter() }
                            </div>
                        }
                    } else {
                        html!{}
                    }
                }

                { actions }

            </div>
        };
    }
}

// alert group

#[derive(Clone, PartialEq, Properties)]
pub struct GroupProps {
    #[prop_or_default]
    pub children: ChildrenWithProps<Alert>,
    #[prop_or_default]
    pub toast: bool,
}

pub struct AlertGroup {
    props: GroupProps,
}

impl Component for AlertGroup {
    type Message = ();
    type Properties = GroupProps;

    fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
        Self { props }
    }

    fn update(&mut self, _msg: Self::Message) -> ShouldRender {
        true
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-alert-group");

        if self.props.toast {
            classes.push("pf-m-toast");
        }

        return html! {
            <ul class=classes>
                { for self.props.children.iter().map(|child|html_nested!{
                    <li class="pf-c-alert-group__item">
                        { child }
                    </li>
                })}
            </ul>
        };
    }
}