use crate::element::{Element, HtmlElement, HtmlElementConfig};
pub struct InputFieldConfig {
pub name: String,
pub id: String,
pub label: String,
pub label_config: HtmlElementConfig,
pub field_config: HtmlElementConfig,
}
pub struct NoConfigs;
impl InputFieldConfig {
pub fn new(name: String, id: String, label: String) -> Self {
Self {
name,
label,
label_config: HtmlElementConfig::new_empty(),
field_config: HtmlElementConfig::new_empty(),
id,
}
}
pub fn set_html_configs(&self, configs: HtmlElementConfig) -> HtmlElementConfig {
configs
.set_attribute("name".to_string(), Some(self.name.clone()))
.set_attribute("id".to_string(), Some(self.id.clone()))
}
}
impl NoConfigs {
pub fn new() -> Self {
Self
}
}
impl AsHtmlConfig for NoConfigs {
fn set_html_configs(&self, configs: HtmlElementConfig) -> HtmlElementConfig {
configs
}
}
pub trait AsHtmlConfig {
fn set_html_configs(&self, configs: HtmlElementConfig) -> HtmlElementConfig;
}
pub fn create_labeled_input<T>(
html_configs: InputFieldConfig,
input_type: String,
input_configs: T,
value: Option<String>,
) -> Element
where
T: AsHtmlConfig,
{
let label = Element::Element(HtmlElement::new(
crate::tags::TagType::Label,
html_configs
.label_config
.clone()
.set_attribute("for".to_string(), Some(html_configs.id.clone())),
)) + Element::Text(html_configs.label.clone());
let mut cfg = input_configs.set_html_configs(
html_configs
.set_html_configs(html_configs.field_config.clone())
.set_attribute("type".to_string(), Some(input_type)),
);
if value.is_some() {
cfg = cfg.set_attribute("value".to_string(), value);
}
Element::Element(HtmlElement::new(
crate::tags::TagType::Div,
HtmlElementConfig::new_empty(),
)) + label
+ Element::Element(HtmlElement::new(crate::tags::TagType::Input, cfg))
}
pub fn create_input<T>(
html_configs: InputFieldConfig,
input_type: String,
input_configs: T,
value: Option<String>,
) -> Element
where
T: AsHtmlConfig,
{
let mut cfg = input_configs.set_html_configs(
html_configs
.set_html_configs(html_configs.field_config.clone())
.set_attribute("type".to_string(), Some(input_type)),
);
if value.is_some() {
cfg = cfg.set_attribute("value".to_string(), value);
}
Element::Element(HtmlElement::new(crate::tags::TagType::Input, cfg))
}