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

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct Props {
    #[prop_or_default]
    pub text: String,
    #[prop_or_default]
    pub badge: Option<String>,
    #[prop_or_default]
    pub overflow: bool,
    #[prop_or_default]
    pub draggable: bool,
    #[prop_or_default]
    pub onclose: Option<Callback<()>>,
    #[prop_or_default]
    pub icon: Option<Icon>,
}

pub struct Chip {}

impl Component for Chip {
    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-chip");

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

        // this is only used in the chip group component
        if ctx.props().overflow {
            classes.push("pf-m-overflow");
        }

        let body = html! {
            <>
                { self.render_icon(ctx) }
                <span class="pf-c-chip__text">{ctx.props().text.clone()}</span>
                { self.render_badge(ctx) }
                { self.render_close(ctx) }
            </>
        };

        if ctx.props().overflow {
            html! {<button class={classes}>{body}</button>}
        } else {
            html! {<div class={classes}>{body}</div>}
        }
    }
}

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

    fn render_badge(&self, ctx: &Context<Self>) -> Html {
        if let Some(badge) = &ctx.props().badge {
            return html! {
                <span class="pf-c-badge pf-m-read">{badge}</span>
            };
        } else {
            return 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! {};
        }
    }
}