lumen_dioxus/inputs/
select_option.rs1#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
22pub struct SelectOption {
23 pub value: String,
25 pub label: String,
27 pub disabled: bool,
29}
30
31impl SelectOption {
32 pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
34 Self {
35 value: value.into(),
36 label: label.into(),
37 disabled: false,
38 }
39 }
40
41 #[must_use]
43 pub fn disabled(mut self, disabled: bool) -> Self {
44 self.disabled = disabled;
45 self
46 }
47}
48
49impl From<&str> for SelectOption {
50 fn from(value: &str) -> Self {
51 Self::new(value, value)
52 }
53}
54
55impl From<String> for SelectOption {
56 fn from(value: String) -> Self {
57 Self {
58 label: value.clone(),
59 value,
60 disabled: false,
61 }
62 }
63}
64
65impl From<(&str, &str)> for SelectOption {
66 fn from((value, label): (&str, &str)) -> Self {
67 Self::new(value, label)
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::SelectOption;
74
75 #[test]
76 fn strings_become_options_with_matching_label_and_value() {
77 let option = SelectOption::from("Draft");
78
79 assert_eq!(option, SelectOption::new("Draft", "Draft"));
80 assert_eq!(
81 SelectOption::from(String::from("Live")),
82 SelectOption::new("Live", "Live")
83 );
84 assert_eq!(
85 SelectOption::from(("a", "Alpha")).disabled(true),
86 SelectOption {
87 value: "a".into(),
88 label: "Alpha".into(),
89 disabled: true
90 }
91 );
92 }
93}