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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::borrow::BorrowMut;
use uuid::Uuid;

use crate::{DefaultModifiers, scale};
use crate::Renderable;
use crate::components::*;
use crate::node::{Node, NodeContainer};

#[derive(Debug, Clone)]
pub struct PickerOption {
    pub icon: Option<String>,
    pub label: String,
    pub value: String,
}

impl PickerOption {
    pub fn new(label: &str, value: &str) -> Self {
        PickerOption {
            icon: None,
            label: label.to_string(),
            value: value.to_string(),
        }
    }
    pub fn icon(&mut self, name: &str) -> Self {
        self.icon = Some(name.to_string());
        self.clone()
    }
}

#[derive(Debug, Clone)]
pub enum PickerStyle {
    Segmented,
    Dropdown,
    RadioGroup,
}

#[derive(Debug, Clone)]
pub struct Picker {
    node: Node,
    style: PickerStyle,
    label: Option<String>,
    name: String,
    value: String,
    options: Vec<PickerOption>,
    children: Vec<Box<dyn Renderable>>,
}

impl Picker {
    pub fn new(name: &str, value: &str, picker_style: PickerStyle) -> Self {
        Picker {
            node: Default::default(),
            style: picker_style,
            label: None,
            name: name.to_string(),
            value: value.to_string(),
            children: vec![],
            options: vec![],
        }
    }

    pub fn label(&mut self, label: &str) -> Self {
        self.label = Some(label.to_string());
        self.clone()
    }

    pub fn submit_on_change(&mut self, submit_on_change: bool) -> Self {
        if submit_on_change {
            self.set_attr("data-auto-submit", "data-auto-submit")
        } else {
            self.unset_attr("data-auto-submit")
        }
    }

    /// Make the button submit specified form
    /// ```rust
    ///View::new()
    ///    .append_child({
    ///        Form::new("formName", "/")
    ///    })
    ///    .append_child({
    ///        Button::new("Submit", ButtonStyle::Filled)
    ///            .attach_to_form("formName")
    ///        })
    /// ```
    pub fn attach_to_form(&mut self, form_name: &str) -> Self {
        self
            .set_attr("form", form_name)
    }


    pub fn append_child(&mut self, child: PickerOption) -> Self
    {
        self.options.push(child);
        self.clone()
    }
}


impl NodeContainer for Picker {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}

impl DefaultModifiers<Picker> for Picker {}

impl ChildContainer for Picker {
    fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> {
        return self.children.borrow_mut();
    }
}


impl Renderable for Picker {
    fn render(&self) -> Node {
        let mut picker = self.clone()
            .add_class("picker")
            .add_class(format!("picker--{:?}", self.style).to_lowercase().as_str());

        if let Some(label) = picker.label {
            let text = Text::new(label.as_str(), TextStyle::Label);
            picker.node.children.push(text.render());
        }
        let picker_id = Uuid::new_v4().to_hyphenated().to_string();
        match self.style {
            PickerStyle::Segmented => {
                picker.node.children.push({
                    let mut option_list = HStack::new(Alignment::Stretch)
                        .add_class("picker--segmented__option-list");
                    for option in picker.options {
                        let radio_id = format!("picker-segmented-{}-{}", picker_id, option.label);
                        option_list
                            .append_child({
                                let mut radio = View::new()
                                    .tag("input")
                                    .set_attr("type", "radio")
                                    .set_attr("name", self.name.as_str())
                                    .set_attr("value", option.value.as_str())
                                    .set_attr("id", radio_id.as_str())
                                    .add_class("picker--segmented__option-list__radio");
                                if picker.value.eq(option.value.as_str()) {
                                    radio.set_attr("checked", "checked");
                                }
                                radio
                            });
                        option_list.append_child({
                            let mut item = HStack::new(Alignment::Center)
                                .add_class("picker--segmented__option-list__option")
                                .tag("label")
                                .set_attr("for", radio_id.as_str());

                            if let Some(icon) = option.icon {
                                item.append_child({
                                    Icon::new(icon.as_str())
                                        .size(16)
                                        .margin_right(scale(2))
                                });
                            }
                            item.append_child({
                                Text::new(option.label.as_str(), TextStyle::Button)
                            });
                            if picker.value.eq(option.value.as_str()) {
                                item.add_class("selected");
                            }
                            item
                        });
                    }
                    option_list.render()
                })
            }
            PickerStyle::Dropdown => {
                picker.node.children.push({
                    let radio_id = format!("picker-dropdown-{}", self.name.as_str());
                    let mut select = View::new()
                        .tag("select")
                        .set_attr("name", self.name.as_str())
                        .set_attr("id", radio_id.as_str());

                    for option in picker.options {
                        select = select.append_child({
                            let mut option_element = View::new()
                                .tag("option")
                                .set_attr("value", &option.value);
                            if option.value.eq(&picker.value) {
                                option_element.set_attr("selected", "selected");
                            }
                            option_element.node.text = Some(option.label);
                            option_element
                        })
                    }
                    select.render()
                })
            }
            PickerStyle::RadioGroup => {
                picker.node.children.push({
                    let mut option_list = VStack::new(Alignment::Stretch)
                        .add_class("picker__option-list")
                        .gap(vec![scale(3)]);
                    for option in picker.options {
                        option_list.append_child({
                            let mut radio_row = HStack::new(Alignment::Center)
                                .gap(vec![scale(2)]);
                            let radio_id = format!("picker-radio-{}-{}", self.name.as_str(), option.label);
                            let mut radio_button = View::new()
                                .tag("input")
                                .set_attr("type", "radio")
                                .set_attr("name", self.name.as_str())
                                .set_attr("id", radio_id.as_str())
                                .set_attr("value", option.value.as_str());
                            if picker.value.eq(option.value.as_str()) {
                                radio_button.set_attr("checked", "checked");
                            }
                            radio_row.append_child(
                                radio_button
                            );
                            radio_row.append_child(
                                Text::new(option.label.as_str(), TextStyle::Body)
                                    .set_attr("for", radio_id.as_str())
                                    .tag("label")
                            );

                            radio_row
                        });
                    }
                    option_list.render()
                })
            }
        }

        picker.node
    }
}