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
use crate::{Button, Icon, Variant};
use yew::prelude::*;

use strum_macros::{Display, EnumIter, EnumString};

#[derive(Copy, Clone, Display, Debug, PartialEq, Eq, EnumIter, EnumString)]
pub enum Color {
    Grey,
    Blue,
    Green,
    Orange,
    Red,
    Purple,
    Cyan,
}

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

impl From<Color> for Classes {
    fn from(color: Color) -> Self {
        match color {
            Color::Grey => Classes::new(),
            Color::Blue => Classes::from("pf-m-blue"),
            Color::Green => Classes::from("pf-m-green"),
            Color::Orange => Classes::from("pf-m-orange"),
            Color::Red => Classes::from("pf-m-red"),
            Color::Purple => Classes::from("pf-m-purple"),
            Color::Cyan => Classes::from("pf-m-cyan"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct Props {
    #[prop_or_default]
    pub label: String,
    #[prop_or_default]
    pub color: Color,
    #[prop_or_default]
    pub outline: bool,
    #[prop_or_default]
    pub overflow: bool,
    #[prop_or_default]
    pub icon: Option<Icon>,
    #[prop_or_default]
    pub onclose: Option<Callback<()>>,
    #[prop_or_default]
    pub href: String,
}

pub struct Label {
    props: Props,
}

impl Component for Label {
    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-label");

        classes = classes.extend(Classes::from(self.props.color));

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

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

        let content = |content: Html| {
            if self.props.href.is_empty() {
                html! {<span class="pf-c-label__content">{content}</span>}
            } else {
                html! {<a class="pf-c-label__content" href=self.props.href.clone()>{content}</a>}
            }
        };

        return html! {
            <span class=classes>
                { content (
                    html!{
                        <>
                            { self.render_icon() }
                            { &self.props.label }
                        </>
                    }
                )}
                { self.render_close() }
            </span>
        };
    }
}

impl Label {
    fn render_icon(&self) -> Html {
        if let Some(icon) = &self.props.icon {
            html! {
                <span class="pf-c-label__icon">
                    { icon.as_html() }
                </span>
            }
        } else {
            html! {}
        }
    }

    fn render_close(&self) -> Html {
        if let Some(onclose) = &self.props.onclose {
            let onclose = onclose.reform(|_| {});
            return html! {
                <Button variant=Variant::Plain icon=Icon::Times onclick=onclose/>
            };
        } else {
            return html! {};
        }
    }
}