rumtk_web/components/form/
props.rs1use rumtk_core::strings::{rumtk_format, CompactStringExt, RUMString, RUMStringConversions};
22
23type EventHandler<'a> = (&'a str, &'a str);
24type EventHandlers<'a> = Vec<EventHandler<'a>>;
25
26#[derive(Debug, Clone, Default)]
27pub struct InputProps<'a> {
28 pub id: Option<&'a str>,
29 pub name: Option<&'a str>,
30 pub typ: Option<&'a str>,
31 pub value: Option<&'a str>,
32 pub max: Option<&'a str>,
33 pub placeholder: Option<&'a str>,
34 pub pattern: Option<&'a str>,
35 pub accept: Option<&'a str>,
36 pub alt: Option<&'a str>,
37 pub aria_label: Option<&'a str>,
38 pub for_element: Option<&'a str>,
39 pub event_handlers: Option<EventHandlers<'a>>,
40 pub max_length: Option<usize>,
41 pub min_length: Option<usize>,
42 pub autocapitalize: bool,
43 pub autocomplete: bool,
44 pub autocorrect: bool,
45 pub autofocus: bool,
46 pub disabled: bool,
47 pub hidden: bool,
48 pub required: bool,
49 pub multiple: bool,
50}
51
52impl InputProps<'_> {
53 fn get_handler_string(&self, handlers: &EventHandlers) -> RUMString {
54 let mut handler_string = RUMString::default();
55
56 for (handler_name, handler_function) in handlers {
57 handler_string += &rumtk_format!(" {}={:?}", handler_name, handler_function);
58 }
59
60 handler_string.trim().to_rumstring()
61 }
62
63 pub fn to_rumstring(&self) -> RUMString {
64 let default_text = RUMString::default();
65 let mut options = vec![
66 match &self.id {
67 Some(id) => rumtk_format!("id={:?}", id),
68 None => default_text.clone(),
69 },
70 match &self.name {
71 Some(name) => rumtk_format!("name={:?}", name),
72 None => default_text.clone(),
73 },
74 match &self.for_element {
75 Some(name) => rumtk_format!("for={:?}", name),
76 None => default_text.clone(),
77 },
78 match &self.typ {
79 Some(typ) => rumtk_format!("type={:?}", typ),
80 None => default_text.clone(),
81 },
82 match &self.value {
83 Some(val) => rumtk_format!("value={:?}", val),
84 None => default_text.clone(),
85 },
86 match &self.max {
87 Some(val) => rumtk_format!("max={:?}", val),
88 None => default_text.clone(),
89 },
90 match &self.placeholder {
91 Some(placeholder) => rumtk_format!("placeholder={:?}", placeholder),
92 None => default_text.clone(),
93 },
94 match &self.pattern {
95 Some(pattern) => rumtk_format!("pattern={:?}", pattern),
96 None => default_text.clone(),
97 },
98 match &self.accept {
99 Some(accept) => rumtk_format!("accept={:?}", accept),
100 None => default_text.clone(),
101 },
102 match &self.alt {
103 Some(alt) => rumtk_format!("alt={:?}", alt),
104 None => default_text.clone(),
105 },
106 match &self.aria_label {
107 Some(aria_label) => rumtk_format!("aria-label={:?}", aria_label),
108 None => default_text.clone(),
109 },
110 match &self.event_handlers {
111 Some(handlers) => self.get_handler_string(handlers),
112 None => default_text.clone(),
113 },
114 match self.max_length {
115 Some(max_length) => rumtk_format!("maxlength={:?}", max_length),
116 None => default_text.clone(),
117 },
118 match self.min_length {
119 Some(min_length) => rumtk_format!("minlength={:?}", min_length),
120 None => default_text.clone(),
121 },
122 match self.autocapitalize {
123 true => rumtk_format!("autocapitalize"),
124 false => default_text.clone(),
125 },
126 match self.autocomplete {
127 true => rumtk_format!("autocomplete"),
128 false => default_text.clone(),
129 },
130 match self.autocorrect {
131 true => rumtk_format!("autocorrect"),
132 false => default_text.clone(),
133 },
134 match self.autofocus {
135 true => rumtk_format!("autofocus"),
136 false => default_text.clone(),
137 },
138 match self.disabled {
139 true => rumtk_format!("disabled"),
140 false => default_text.clone(),
141 },
142 match self.hidden {
143 true => rumtk_format!("hidden"),
144 false => default_text.clone(),
145 },
146 match self.required {
147 true => rumtk_format!("required"),
148 false => default_text.clone(),
149 },
150 ];
151 options
152 .into_iter()
153 .filter(|itm| !itm.is_empty())
154 .collect::<Vec<_>>()
155 .join_compact(" ")
156 }
157
158 pub fn to_string(&self) -> String {
159 self.to_rumstring().to_string()
160 }
161}