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
use std::borrow::BorrowMut;
use uuid::Uuid;
use crate::{DefaultModifiers, scale};
use crate::components::*;
use crate::components::icons::{IconPack, Lucide};
use crate::node::{Node, NodeContainer};
use crate::Renderable;
#[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) -> Self
where
T: 'static + IconPack {
self.icon = Some(Box::new(icon));
self.clone()
}
}
#[derive(Debug, Clone)]
pub enum PickerStyle {
Segmented,
Dropdown,
RadioGroup,
}
#[derive(Debug, Clone)]
pub struct Picker {
node: Node,
children: Vec<Box<dyn Renderable>>,
style: PickerStyle,
label: Option<String>,
name: String,
value: String,
options: Vec<PickerOption>,
is_disabled: bool,
auto_submit: bool,
}
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![],
is_disabled: false,
auto_submit: false,
}
}
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 {
self.auto_submit = submit_on_change;
self.clone()
}
pub fn multiple(&mut self) -> Self {
self.clone()
}
/// 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 disabled(&mut self, is_disabled: bool) -> Self {
self.is_disabled = is_disabled;
self.clone()
}
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_string();
match self.style {
PickerStyle::Segmented => {
if picker.is_disabled {
picker.add_class("picker--segmented--disabled");
}
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");
}
if self.auto_submit {
radio.set_attr("data-auto-submit", "data-auto-submit");
}
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)
.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 => {
let dropdown_id = format!("picker-dropdown-{}", self.name.as_str());
picker.node.children.push({
View::new()
.add_class("picker--dropdown__input")
.set_attr("tabindex", "0")
.append_child({
let mut input = Field::new(self.name.as_str(), FieldType::Hidden)
.add_class("picker--dropdown__input__field")
.value(&picker.value);
if self.auto_submit {
input.set_attr("data-auto-submit", "data-auto-submit");
}
input
})
.append_child({
Text::new("", TextStyle::Body)
.add_class("picker--dropdown__input__value-display")
.flex_grow(1)
})
.popover({
Popover::new()
.placement(Placement::BottomStart)
.add_class("picker--dropdown__dropdown")
.hide_arrow()
.append_child({
VStack::new(Alignment::Stretch)
.gap(vec![scale(3)])
.append_child({
Field::new("", FieldType::Search)
.add_class("picker--dropdown__dropdown__search-bar")
.placeholder("Recherche")
})
.append_child({
let mut option_list = VStack::new(Alignment::Stretch)
.add_class("picker--dropdown__dropdown__option-list");
for option in picker.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()
.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 picker.value.eq(&option.value) {
radio.set_attr("checked", "checked");
}
radio
})
.append_child({
Text::new(&option.label, TextStyle::Label)
.flex_grow(1)
.no_wrap(true)
})
.append_child({
Icon::new(Lucide::Check)
.size(scale(4))
})
});
}
option_list
})
})
})
.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.is_disabled {
radio_button.set_attr("disabled", "disabled");
}
if picker.value.eq(option.value.as_str()) {
radio_button.set_attr("checked", "checked");
}
if self.auto_submit {
radio_button.set_attr("data-auto-submit", "data-auto-submit");
}
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
}
}