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.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23use rumtk_core::strings::{rumtk_format, RUMString};
24
25type EventHandler<'a> = (&'a str, &'a str);
26type EventHandlers<'a> = Vec<EventHandler<'a>>;
27
28#[derive(Debug, Clone, Default)]
29pub struct InputProps<'a> {
30    pub id: Option<RUMString>,
31    pub name: Option<RUMString>,
32    pub typ: Option<RUMString>,
33    pub value: Option<RUMString>,
34    pub max: Option<RUMString>,
35    pub placeholder: Option<RUMString>,
36    pub pattern: Option<RUMString>,
37    pub accept: Option<RUMString>,
38    pub alt: Option<RUMString>,
39    pub aria_label: Option<RUMString>,
40    pub event_handlers: Option<EventHandlers<'a>>,
41    pub max_length: Option<usize>,
42    pub min_length: Option<usize>,
43    pub autocapitalize: bool,
44    pub autocomplete: bool,
45    pub autocorrect: bool,
46    pub autofocus: bool,
47    pub disabled: bool,
48    pub hidden: bool,
49    pub required: 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
61    }
62
63    pub fn to_rumstring(&self) -> RUMString {
64        let default_text = RUMString::default();
65        rumtk_format!(
66            "{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}",
67            match &self.id {
68                Some(id) => rumtk_format!("id={:?}", id),
69                None => default_text.clone(),
70            },
71            match &self.name {
72                Some(name) => rumtk_format!("name={:?}", name),
73                None => default_text.clone(),
74            },
75            match &self.typ {
76                Some(typ) => rumtk_format!("type={:?}", typ),
77                None => default_text.clone(),
78            },
79            match &self.value {
80                Some(val) => rumtk_format!("value={:?}", val),
81                None => default_text.clone(),
82            },
83            match &self.max {
84                Some(val) => rumtk_format!("max={:?}", val),
85                None => default_text.clone(),
86            },
87            match &self.placeholder {
88                Some(placeholder) => rumtk_format!("placeholder={:?}", placeholder),
89                None => default_text.clone(),
90            },
91            match &self.pattern {
92                Some(pattern) => rumtk_format!("pattern={:?}", pattern),
93                None => default_text.clone(),
94            },
95            match &self.accept {
96                Some(accept) => rumtk_format!("accept={:?}", accept),
97                None => default_text.clone(),
98            },
99            match &self.alt {
100                Some(alt) => rumtk_format!("alt={:?}", alt),
101                None => default_text.clone(),
102            },
103            match &self.aria_label {
104                Some(aria_label) => rumtk_format!("aria-label={:?}", aria_label),
105                None => default_text.clone(),
106            },
107            match &self.event_handlers {
108                Some(handlers) => self.get_handler_string(handlers),
109                None => default_text.clone(),
110            },
111            match self.max_length {
112                Some(max_length) => rumtk_format!("maxlength={:?}", max_length),
113                None => default_text.clone(),
114            },
115            match self.min_length {
116                Some(min_length) => rumtk_format!("minlength={:?}", min_length),
117                None => default_text.clone(),
118            },
119            match self.autocapitalize {
120                true => rumtk_format!("autocapitalize"),
121                false => default_text.clone(),
122            },
123            match self.autocomplete {
124                true => rumtk_format!("autocomplete"),
125                false => default_text.clone(),
126            },
127            match self.autocorrect {
128                true => rumtk_format!("autocorrect"),
129                false => default_text.clone(),
130            },
131            match self.autofocus {
132                true => rumtk_format!("autofocus"),
133                false => default_text.clone(),
134            },
135            match self.disabled {
136                true => rumtk_format!("disabled"),
137                false => default_text.clone(),
138            },
139            match self.hidden {
140                true => rumtk_format!("hidden"),
141                false => default_text.clone(),
142            },
143            match self.required {
144                true => rumtk_format!("required"),
145                false => default_text.clone(),
146            },
147        )
148    }
149
150    pub fn to_string(&self) -> String {
151        self.to_rumstring().to_string()
152    }
153}