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
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 {}

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

    fn create(_: &Context<Self>) -> Self {
        Self {}
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let mut classes = Classes::from("pf-c-label");

        classes.extend(Classes::from(ctx.props().color));

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

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

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

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

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

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