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
use crate::{GlobalClose, Icon, InputGroup, TextInput, TextInputIcon};
use std::rc::Rc;
use yew::prelude::*;

#[derive(Properties, Debug, Clone, PartialEq)]
pub struct Props {
    #[prop_or_default]
    pub selected: String,
    #[prop_or_default]
    pub onsearch: Callback<String>,
    #[prop_or_default]
    pub children: ChildrenWithProps<ContextSelectorItem>,
}

#[derive(Clone, Debug)]
pub enum Msg {
    Toggle,
    Close,
    Search(String),
}

pub struct ContextSelector {
    expanded: bool,
    global_close: GlobalClose,
}

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

    fn create(ctx: &Context<Self>) -> Self {
        let global_close =
            GlobalClose::new(NodeRef::default(), ctx.link().callback(|_| Msg::Close));
        Self {
            expanded: false,
            global_close,
        }
    }

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Self::Message::Toggle => {
                self.expanded = !self.expanded;
            }
            Self::Message::Close => {
                self.expanded = false;
            }
            Self::Message::Search(value) => {
                ctx.props().onsearch.emit(value);
                return false;
            }
        }
        true
    }

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

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

        return html! {
            <div
                class={classes}
                ref={self.global_close.clone()}
            >
                <button
                    class="pf-c-context-selector__toggle"
                    aria-expanded={self.expanded.to_string()}
                    type="button"
                    onclick={ctx.link().callback(|_|Msg::Toggle)}
                >
                    <span class="pf-c-context-selector__toggle-text">{&ctx.props().selected}</span>
                    <span class="pf-c-context-selector__toggle-icon">{Icon::CaretDown}</span>
                </button>
                <div class="pf-c-context-selector__menu"
                    hidden={!self.expanded}
                >
                    <div class="pf-c-context-selector__menu-search">
                        <InputGroup>
                            <TextInput
                                onchange={ctx.link().callback(|v|Msg::Search(v))}
                                icon={TextInputIcon::Search}
                                r#type="search"/>
                        </InputGroup>
                    </div>
                    <ul class="pf-c-context-selector__menu-list">
                        { for ctx.props().children.iter().map(|mut item|{
                            let mut props = Rc::make_mut(&mut item.props);
                            props.need_close = ctx.link().callback(|_|Msg::Close);
                            item
                        }) }
                    </ul>
                </div>
            </div>
        };
    }
}

// item

#[derive(Properties, Debug, Clone, PartialEq)]
pub struct ItemProps {
    pub label: String,
    #[prop_or_default]
    pub onclick: Callback<()>,
    #[prop_or_default]
    pub disabled: bool,
    #[prop_or_default]
    pub(crate) need_close: Callback<()>,
}

#[derive(Clone, Copy, Debug)]
pub enum ItemMsg {
    Clicked,
}

pub struct ContextSelectorItem {}

impl Component for ContextSelectorItem {
    type Message = ItemMsg;
    type Properties = ItemProps;

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

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Self::Message::Clicked => {
                ctx.props().onclick.emit(());
                ctx.props().need_close.emit(());
            }
        }
        true
    }

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

        return html! {
            <li>
                <button
                    class={classes}
                    disabled={ctx.props().disabled}
                    type="button"
                    onclick={ctx.link().callback(|_|ItemMsg::Clicked)}
                    >
                    { &ctx.props().label }
                </button>
            </li>
        };
    }
}