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

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

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

/// Context Selector component
///
/// > A **context selector** can be used in addition to global navigation when the data or resources you show in the interface need to change depending on the user's context.
///
/// See: <https://www.patternfly.org/components/menus/context-selector>
///
/// ## Properties
///
/// Defined by [`ContextSelectorProperties`].
#[deprecated(
    since = "5.0.0",
    note = "The ContentSelector component has been deprecated by PatternFly. \
    See https://pf5.patternfly.org/components/menus/context-selector for more information."
)]
pub struct ContextSelector {
    expanded: bool,
    global_close: GlobalClose,
}

#[allow(deprecated)]
impl Component for ContextSelector {
    type Message = ContextSelectorMsg;
    type Properties = ContextSelectorProperties;

    fn create(ctx: &Context<Self>) -> Self {
        let global_close = GlobalClose::new(
            NodeRef::default(),
            ctx.link().callback(|_| ContextSelectorMsg::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-v5-c-context-selector");

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

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

// item

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

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

/// An item of a [`ContextSelector`]
pub struct ContextSelectorItem {}

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

    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-v5-c-context-selector__menu-list-item");

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