use crate::element::{Element, HtmlElementConfig};
use super::field::{create_input, create_labeled_input, AsHtmlConfig, InputFieldConfig};
pub struct EmailInputConfig {
pub list: Option<String>,
pub max_length: Option<usize>,
pub min_length: Option<usize>,
pub multiple: Option<bool>,
pub pattern: Option<String>,
pub placeholder: Option<String>,
pub readonly: Option<bool>,
pub size: Option<usize>,
}
pub fn create_labeled_email_input(
html_configs: InputFieldConfig,
input_configs: EmailInputConfig,
value: Option<String>,
) -> Element {
create_labeled_input(html_configs, "email".to_string(), input_configs, value)
}
pub fn create_email_input(
html_configs: InputFieldConfig,
input_configs: EmailInputConfig,
value: Option<String>,
) -> Element {
create_input(html_configs, "email".to_string(), input_configs, value)
}
impl EmailInputConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_list(mut self, list: String) -> Self {
self.list = Some(list);
self
}
pub fn without_list(mut self) -> Self {
self.list = None;
self
}
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = Some(max_length);
self
}
pub fn without_max_length(mut self) -> Self {
self.max_length = None;
self
}
pub fn with_min_length(mut self, min_length: usize) -> Self {
self.min_length = Some(min_length);
self
}
pub fn without_min_length(mut self) -> Self {
self.min_length = None;
self
}
pub fn with_multiple(mut self, state: bool) -> Self {
self.multiple = Some(state);
self
}
pub fn without_multiple(mut self) -> Self {
self.multiple = None;
self
}
pub fn with_pattern(mut self, pattern: String) -> Self {
self.pattern = Some(pattern);
self
}
pub fn without_pattern(mut self) -> Self {
self.pattern = None;
self
}
pub fn with_placeholder(mut self, placeholder: String) -> Self {
self.placeholder = Some(placeholder);
self
}
pub fn without_placeholder(mut self) -> Self {
self.placeholder = None;
self
}
pub fn with_readonly(mut self, state: bool) -> Self {
self.readonly = Some(state);
self
}
pub fn without_readonly(mut self) -> Self {
self.readonly = None;
self
}
pub fn with_size(mut self, size: usize) -> Self {
self.size = Some(size);
self
}
pub fn without_size(mut self) -> Self {
self.size = None;
self
}
}
impl AsHtmlConfig for EmailInputConfig {
fn set_html_configs(&self, mut configs: HtmlElementConfig) -> HtmlElementConfig {
if self.list.is_some() {
configs = configs.set_attribute("list".to_string(), self.list.clone());
}
if let Some(max_length) = self.max_length {
configs = configs.set_attribute("maxlength".to_string(), Some(max_length.to_string()));
}
if let Some(min_length) = self.min_length {
configs = configs.set_attribute("minlength".to_string(), Some(min_length.to_string()));
}
if let Some(size) = self.size {
configs = configs.set_attribute("size".to_string(), Some(size.to_string()));
}
if let Some(pattern) = &self.pattern {
configs = configs.set_attribute("pattern".to_string(), Some(pattern.clone()));
}
if let Some(placeholder) = &self.placeholder {
configs = configs.set_attribute("placeholder".to_string(), Some(placeholder.clone()));
}
if let Some(multiple) = self.multiple {
if multiple {
configs = configs.set_attribute("multiple".to_string(), None);
}
}
if let Some(readonly) = self.readonly {
if readonly {
configs = configs.set_attribute("readonly".to_string(), None);
}
}
configs
}
}
impl Default for EmailInputConfig {
fn default() -> Self {
Self {
list: None,
max_length: None,
min_length: None,
multiple: None,
pattern: None,
placeholder: None,
readonly: None,
size: None,
}
}
}