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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use uuid::Uuid;
use crate::Renderable;
use crate::components::icons::{IconPack, Lucide};
use crate::components::*;
use crate::node::Node;
use crate::{DefaultModifiers, scale};
#[derive(Debug, Clone)]
pub struct PickerOption {
pub icon: Option<Box<dyn IconPack>>,
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(),
}
}
/// Set picker's icon
pub fn icon<T>(&mut self, icon: T) -> &mut Self
where
T: 'static + IconPack,
{
self.icon = Some(Box::new(icon));
self
}
}
#[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,
pub options: Vec<PickerOption>,
is_disabled: bool,
auto_submit: bool,
required: bool,
pub form: Option<String>,
}
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(),
options: vec![],
is_disabled: false,
auto_submit: false,
required: false,
form: None,
}
}
pub fn label(&mut self, label: &str) -> &mut Self {
self.label = Some(label.to_string());
self
}
pub fn required(&mut self) -> &mut Self {
self.required = true;
self
}
pub fn submit_on_change(&mut self, submit_on_change: bool) -> &mut Self {
self.auto_submit = submit_on_change;
self
}
pub fn multiple(&mut self) -> &mut Self {
self
}
/// 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) -> &mut Self {
self.form = Some(form_name.to_string());
self
}
pub fn disabled(&mut self, is_disabled: bool) -> &mut Self {
self.is_disabled = is_disabled;
self
}
pub fn append_child(&mut self, child: PickerOption) -> &mut Self {
self.options.push(child);
self
}
}
impl std::ops::Deref for Picker {
type Target = Node;
fn deref(&self) -> &Self::Target {
&self.node
}
}
impl std::ops::DerefMut for Picker {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.node
}
}
impl DefaultModifiers for Picker {}
impl Renderable for Picker {
fn render(mut self) -> Node {
let picker_style = format!("picker--{:?}", self.style).to_lowercase();
self.add_class("picker").add_class(&picker_style);
if let Some(label) = &self.label {
self.node
.children
.push(Text::new(label.as_str(), TextStyle::Label).render());
}
let picker_id = Uuid::new_v4().to_string();
match self.style {
PickerStyle::Segmented => {
if self.is_disabled {
self.add_class("picker--segmented--disabled");
}
self.node.children.push({
let mut option_list = HStack::new(Alignment::Stretch);
option_list.add_class("picker--segmented__option-list");
for option in self.options {
let radio_id = format!("picker-segmented-{}-{}", picker_id, option.label);
option_list.append_child({
let mut radio = View::new();
radio
.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 let Some(form_id) = &self.form {
radio.set_attr("form", form_id);
}
if self.value.eq(option.value.as_str()) {
radio.set_attr("checked", "checked");
}
if self.required {
radio.set_attr("required", "required");
}
if self.auto_submit {
radio.set_attr("data-auto-submit", "data-auto-submit");
}
if self.is_disabled {
radio.set_attr("disabled", "disabled");
}
radio
});
option_list.append_child({
let mut item = HStack::new(Alignment::Center);
item.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({
let mut icon = Icon::new(icon);
icon.size(16).margin_right(scale(2));
icon
});
}
item.append_child({
Text::new(option.label.as_str(), TextStyle::Button)
});
if self.value.eq(option.value.as_str()) {
item.add_class("selected");
}
item
});
}
option_list.render()
})
}
PickerStyle::Dropdown => {
let dropdown_id = format!("picker-dropdown-{}", picker_id);
self.node.children.push({
let mut view = View::new();
view.add_class("picker--dropdown__input")
.set_attr("tabindex", "0")
.append_child({
let mut input = Field::new(self.name.as_str(), FieldType::Hidden);
input.add_class("picker--dropdown__input__field")
.value(&self.value);
if let Some(form_id) = &self.form{
input.set_attr("form", form_id.as_str());
}
if self.auto_submit {
input.set_attr("data-auto-submit", "data-auto-submit");
}
if self.required {
input.set_attr("required", "required");
}
input
})
.append_child({
let mut text = Text::new("", TextStyle::Body);
text.add_class("picker--dropdown__input__value-display")
.flex_grow(1);
text
})
.popover({
let mut popover = Popover::new();
popover.placement(Placement::BottomStart)
.add_class("picker--dropdown__dropdown")
.hide_arrow()
.append_child(
VStack::new(Alignment::Stretch)
.gap(vec![scale(3)])
.append_child({
let mut field = Field::new("", FieldType::Search);
field.add_class("picker--dropdown__dropdown__search-bar")
.placeholder("Recherche");
field
})
.append_child({
let mut option_list = VStack::new(Alignment::Stretch);
option_list .add_class("picker--dropdown__dropdown__option-list");
for option in self.options {
let radio_id = format!("{}-{}", dropdown_id, option.value);
option_list.append_child(
HStack::new(Alignment::Center)
.gap(vec![scale(3)])
.add_class("text--label")
.add_class("picker--dropdown__dropdown__option-list__option")
.tag("label")
.set_attr("for", radio_id.as_str())
.append_child({
let mut radio = View::new();
radio.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());
if self.value.eq(&option.value) {
radio.set_attr("checked", "checked");
}
radio
})
.append_child({
let mut text = Text::new(&option.label, TextStyle::Label);
text.flex_grow(1)
.no_wrap(true);
text
})
.append_child({
let mut icon = Icon::new(Lucide::Check);
icon.size(scale(4));
icon
})
);
}
option_list
})
);
popover
});
view
.render()
});
}
PickerStyle::RadioGroup => self.node.children.push({
let mut option_list = VStack::new(Alignment::Stretch);
option_list
.add_class("picker__option-list")
.gap(vec![scale(3)]);
for option in self.options {
option_list.append_child({
let mut radio_row = HStack::new(Alignment::Center);
radio_row.gap(vec![scale(2)]);
let radio_id =
format!("picker-radio-{}-{}", self.name.as_str(), option.value);
let mut radio_button = View::new();
radio_button
.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 let Some(form_id) = &self.form {
radio_button.set_attr("form", form_id.as_str());
}
if self.is_disabled {
radio_button.set_attr("disabled", "disabled");
}
if self.value.eq(option.value.as_str()) {
radio_button.set_attr("checked", "checked");
}
if self.required {
radio_button.set_attr("required", "required");
}
if self.auto_submit {
radio_button.set_attr("data-auto-submit", "data-auto-submit");
}
radio_row.append_child(radio_button);
radio_row.append_child({
let mut text = Text::new(option.label.as_str(), TextStyle::Body);
text.set_attr("for", radio_id.as_str()).tag("label");
text
});
radio_row
});
}
option_list.render()
}),
}
self.node
}
}