use crate::element::{Element, HtmlElementConfig};
use super::field::{create_input, create_labeled_input, AsHtmlConfig, InputFieldConfig};
pub struct TextInputConfig {
pub max_length: Option<usize>,
pub min_length: Option<usize>,
pub list: Option<String>,
pub place_holder: Option<String>,
pub pattern: Option<String>,
pub size: Option<u64>,
pub required: bool,
pub read_only: bool,
pub spell_check: bool,
}
pub fn create_label_text_field(
html_configs: InputFieldConfig,
input_configs: TextInputConfig,
value: Option<String>,
) -> Element {
create_labeled_input(html_configs, "text".to_string(), input_configs, value)
}
pub fn create_text_field(
html_configs: InputFieldConfig,
input_configs: TextInputConfig,
value: Option<String>,
) -> Element {
create_input(html_configs, "text".to_string(), input_configs, value)
}
pub fn create_label_search_field(
html_configs: InputFieldConfig,
input_configs: TextInputConfig,
value: Option<String>,
) -> Element {
create_labeled_input(html_configs, "search".to_string(), input_configs, value)
}
pub fn create_search_field(
html_configs: InputFieldConfig,
input_configs: TextInputConfig,
value: Option<String>,
) -> Element {
create_input(html_configs, "search".to_string(), input_configs, value)
}
impl TextInputConfig {
pub fn new() -> Self {
Self::default()
}
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_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_place_holder(mut self, place_holder: String) -> Self {
self.place_holder = Some(place_holder);
self
}
pub fn without_place_holder(mut self) -> Self {
self.place_holder = 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_size(mut self, size: u64) -> Self {
self.size = Some(size);
self
}
pub fn without_size(mut self) -> Self {
self.size = None;
self
}
pub fn set_required(mut self) -> Self {
self.required = true;
self
}
pub fn set_non_required(mut self) -> Self {
self.required = false;
self
}
pub fn set_read_only(mut self) -> Self {
self.read_only = true;
self
}
pub fn set_non_read_only(mut self) -> Self {
self.read_only = false;
self
}
pub fn set_spell_check(mut self) -> Self {
self.spell_check = true;
self
}
pub fn set_non_spell_check(mut self) -> Self {
self.spell_check = false;
self
}
}
impl AsHtmlConfig for TextInputConfig {
fn set_html_configs(&self, mut configs: HtmlElementConfig) -> HtmlElementConfig {
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(list) = &self.list {
configs = configs.set_attribute("list".to_string(), Some(list.clone()));
}
if let Some(place_holder) = &self.place_holder {
configs = configs.set_attribute("placeholder".to_string(), Some(place_holder.clone()));
}
if let Some(pattern) = &self.pattern {
configs = configs.set_attribute("pattern".to_string(), Some(pattern.clone()));
}
if let Some(size) = self.size {
configs = configs.set_attribute("size".to_string(), Some(size.to_string()));
}
if self.required {
configs = configs.set_attribute("required".to_string(), None);
}
if self.read_only {
configs = configs.set_attribute("readonly".to_string(), None);
}
if self.spell_check {
configs = configs.set_attribute("spellcheck".to_string(), None);
}
configs
}
}
impl Default for TextInputConfig {
fn default() -> Self {
Self {
max_length: None,
min_length: None,
list: None,
place_holder: None,
pattern: None,
size: None,
required: false,
read_only: false,
spell_check: false,
}
}
}