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
//! `NativeSelect`: the browser's own `<select>` with Lumen styling.
// cspell:words optgroup
use dioxus::prelude::*;
use crate::class::compose;
use crate::{ControlSize, SelectOption};
/// Props for [`NativeSelect`].
#[derive(Props, Clone, PartialEq)]
pub struct NativeSelectProps {
/// Generated `<option>` elements, rendered after `children`.
#[props(default)]
pub options: Vec<SelectOption>,
/// Adds a first, disabled `<option value="">` with this text. It starts selected when neither
/// `value` nor `default_value` is set.
#[props(into)]
pub placeholder: Option<String>,
/// Controlled value: the option with this value is marked `selected`.
#[props(into)]
pub value: Option<String>,
/// Initial value when uncontrolled.
#[props(into)]
pub default_value: Option<String>,
/// Control height.
#[props(default)]
pub visual_size: ControlSize,
/// Fires on every change of the selected option.
pub oninput: Option<EventHandler<FormEvent>>,
/// Fires when the selected option changes; `event.value()` is the new value.
pub onchange: Option<EventHandler<FormEvent>>,
/// Extra classes appended after the Lumen classes.
#[props(into)]
pub class: Option<String>,
/// Any other `<select>` or global attribute, such as `id`, `name`, `disabled`, or `required`.
#[props(extends = GlobalAttributes, extends = select)]
pub attributes: Vec<Attribute>,
/// Extra `<option>` or `<optgroup>` elements, rendered after the placeholder. Mark their
/// selected state with `selected` yourself.
pub children: Element,
}
/// A styled native `<select>`. Use it when the platform picker is a requirement; otherwise prefer
/// [`Select`](crate::Select), which also works on Dioxus Native.
#[component]
pub fn NativeSelect(props: NativeSelectProps) -> Element {
let placeholder = props.placeholder.clone().filter(|text| !text.is_empty());
let initial = props
.default_value
.clone()
.or_else(|| placeholder.as_ref().map(|_| String::new()));
let mut current = use_signal(|| initial);
let selected = props.value.clone().or_else(|| current.cloned());
let is_selected = |value: &str| selected.as_deref() == Some(value);
let size = match props.visual_size {
ControlSize::Default => None,
ControlSize::Sm => Some("ui-select--sm"),
ControlSize::Lg => Some("ui-select--lg"),
};
let class = compose([Some("ui-select"), size, props.class.as_deref()]);
let (oninput, onchange) = (props.oninput, props.onchange);
rsx! {
select {
class,
oninput: move |event| {
if let Some(handler) = oninput {
handler.call(event);
}
},
onchange: move |event: FormEvent| {
current.set(Some(event.value()));
if let Some(handler) = onchange {
handler.call(event);
}
},
..props.attributes,
if let Some(text) = placeholder {
option { disabled: true, selected: is_selected(""), value: "", "{text}" }
}
{props.children}
for choice in props.options.iter() {
option {
disabled: choice.disabled,
selected: is_selected(&choice.value),
value: choice.value.clone(),
"{choice.label}"
}
}
}
}
}