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