hyaenidae/toolkit/src/text_input.rs

77 lines
1.7 KiB
Rust
Raw Normal View History

2020-12-16 02:40:41 +00:00
use crate::{Input, InputKind};
#[derive(Debug, Default)]
pub struct TextInput {
pub(crate) input: Input,
pub(crate) title: Option<String>,
pub(crate) classes: Vec<String>,
pub(crate) error: Option<String>,
2021-01-09 04:32:02 +00:00
dark: bool,
2020-12-16 02:40:41 +00:00
}
impl TextInput {
pub fn new(name: &str) -> Self {
TextInput {
input: Input::new(name, InputKind::Text),
..Default::default()
}
}
pub fn password(&mut self) -> &mut Self {
self.input.kind(InputKind::Password);
self
}
pub fn textarea(&mut self) -> &mut Self {
self.input.kind(InputKind::TextArea);
self
}
pub fn title(&mut self, title: &str) -> &mut Self {
self.title = Some(title.to_owned());
self
}
pub fn value(&mut self, value: &str) -> &mut Self {
self.input.value(value);
self
}
pub fn placeholder(&mut self, placeholder: &str) -> &mut Self {
self.input.placeholder(placeholder);
self
}
pub fn classes(&mut self, classes: &[&str]) -> &mut Self {
self.classes = classes.into_iter().map(|s| s.to_string()).collect();
self
}
pub fn value_opt(&mut self, value: Option<String>) -> &mut Self {
self.input.value_opt(value);
self
}
pub fn error_opt(&mut self, error: Option<String>) -> &mut Self {
self.error = error;
self
}
2021-01-09 04:32:02 +00:00
pub fn dark(&mut self, dark: bool) -> &mut Self {
self.dark = dark;
self
}
2020-12-16 02:40:41 +00:00
pub(crate) fn class_string(&self) -> String {
2021-01-09 04:32:02 +00:00
let mut classes = self.classes.clone();
classes.push("toolkit-input".to_owned());
if self.dark {
classes.push("toolkit-dark".to_owned());
}
classes.join(" ")
2020-12-16 02:40:41 +00:00
}
}