Skip to main content

rumtk_web/components/form/
props.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use 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}