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
//! `SelectOption`: one choice shared by the option-list components.
/// One choice in a [`Select`](crate::Select), [`NativeSelect`](crate::NativeSelect),
/// [`ListBox`](crate::ListBox), [`Segmented`](crate::Segmented), or [`Mentions`](crate::Mentions)
/// list, like lumen-react's `SelectOption`.
///
/// Build options from plain strings (the label is also the value) or with [`SelectOption::new`]:
///
/// ```
/// use lumen_dioxus::SelectOption;
///
/// let options: Vec<SelectOption> = vec![
/// "Draft".into(),
/// SelectOption::new("published", "Published"),
/// SelectOption::new("archived", "Archived").disabled(true),
/// ];
///
/// assert_eq!(options[0].value, "Draft");
/// assert!(options[2].disabled);
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct SelectOption {
/// The value the option submits and reports.
pub value: String,
/// The visible label.
pub label: String,
/// Whether the option is unavailable.
pub disabled: bool,
}
impl SelectOption {
/// An enabled option with a separate value and label.
pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
Self {
value: value.into(),
label: label.into(),
disabled: false,
}
}
/// Returns the option with its disabled state set.
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl From<&str> for SelectOption {
fn from(value: &str) -> Self {
Self::new(value, value)
}
}
impl From<String> for SelectOption {
fn from(value: String) -> Self {
Self {
label: value.clone(),
value,
disabled: false,
}
}
}
impl From<(&str, &str)> for SelectOption {
fn from((value, label): (&str, &str)) -> Self {
Self::new(value, label)
}
}
#[cfg(test)]
mod tests {
use super::SelectOption;
#[test]
fn strings_become_options_with_matching_label_and_value() {
let option = SelectOption::from("Draft");
assert_eq!(option, SelectOption::new("Draft", "Draft"));
assert_eq!(
SelectOption::from(String::from("Live")),
SelectOption::new("Live", "Live")
);
assert_eq!(
SelectOption::from(("a", "Alpha")).disabled(true),
SelectOption {
value: "a".into(),
label: "Alpha".into(),
disabled: true
}
);
}
}