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, RUMString};
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<RUMString>,
29    pub name: Option<RUMString>,
30    pub typ: Option<RUMString>,
31    pub value: Option<RUMString>,
32    pub max: Option<RUMString>,
33    pub placeholder: Option<RUMString>,
34    pub pattern: Option<RUMString>,
35    pub accept: Option<RUMString>,
36    pub alt: Option<RUMString>,
37    pub aria_label: Option<RUMString>,
38    pub event_handlers: Option<EventHandlers<'a>>,
39    pub max_length: Option<usize>,
40    pub min_length: Option<usize>,
41    pub autocapitalize: bool,
42    pub autocomplete: bool,
43    pub autocorrect: bool,
44    pub autofocus: bool,
45    pub disabled: bool,
46    pub hidden: bool,
47    pub required: bool,
48}
49
50impl InputProps<'_> {
51    fn get_handler_string(&self, handlers: &EventHandlers) -> RUMString {
52        let mut handler_string = RUMString::default();
53
54        for (handler_name, handler_function) in handlers {
55            handler_string += &rumtk_format!(" {}={:?}", handler_name, handler_function);
56        }
57
58        handler_string
59    }
60
61    pub fn to_rumstring(&self) -> RUMString {
62        let default_text = RUMString::default();
63        rumtk_format!(
64            "{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}",
65            match &self.id {
66                Some(id) => rumtk_format!("id={:?}", id),
67                None => default_text.clone(),
68            },
69            match &self.name {
70                Some(name) => rumtk_format!("name={:?}", name),
71                None => default_text.clone(),
72            },
73            match &self.typ {
74                Some(typ) => rumtk_format!("type={:?}", typ),
75                None => default_text.clone(),
76            },
77            match &self.value {
78                Some(val) => rumtk_format!("value={:?}", val),
79                None => default_text.clone(),
80            },
81            match &self.max {
82                Some(val) => rumtk_format!("max={:?}", val),
83                None => default_text.clone(),
84            },
85            match &self.placeholder {
86                Some(placeholder) => rumtk_format!("placeholder={:?}", placeholder),
87                None => default_text.clone(),
88            },
89            match &self.pattern {
90                Some(pattern) => rumtk_format!("pattern={:?}", pattern),
91                None => default_text.clone(),
92            },
93            match &self.accept {
94                Some(accept) => rumtk_format!("accept={:?}", accept),
95                None => default_text.clone(),
96            },
97            match &self.alt {
98                Some(alt) => rumtk_format!("alt={:?}", alt),
99                None => default_text.clone(),
100            },
101            match &self.aria_label {
102                Some(aria_label) => rumtk_format!("aria-label={:?}", aria_label),
103                None => default_text.clone(),
104            },
105            match &self.event_handlers {
106                Some(handlers) => self.get_handler_string(handlers),
107                None => default_text.clone(),
108            },
109            match self.max_length {
110                Some(max_length) => rumtk_format!("maxlength={:?}", max_length),
111                None => default_text.clone(),
112            },
113            match self.min_length {
114                Some(min_length) => rumtk_format!("minlength={:?}", min_length),
115                None => default_text.clone(),
116            },
117            match self.autocapitalize {
118                true => rumtk_format!("autocapitalize"),
119                false => default_text.clone(),
120            },
121            match self.autocomplete {
122                true => rumtk_format!("autocomplete"),
123                false => default_text.clone(),
124            },
125            match self.autocorrect {
126                true => rumtk_format!("autocorrect"),
127                false => default_text.clone(),
128            },
129            match self.autofocus {
130                true => rumtk_format!("autofocus"),
131                false => default_text.clone(),
132            },
133            match self.disabled {
134                true => rumtk_format!("disabled"),
135                false => default_text.clone(),
136            },
137            match self.hidden {
138                true => rumtk_format!("hidden"),
139                false => default_text.clone(),
140            },
141            match self.required {
142                true => rumtk_format!("required"),
143                false => default_text.clone(),
144            },
145        )
146    }
147
148    pub fn to_string(&self) -> String {
149        self.to_rumstring().to_string()
150    }
151}