generated from mitchell/rust_template
71 lines
1.8 KiB
Rust
71 lines
1.8 KiB
Rust
use leptos::{Signal, View};
|
|
|
|
use super::{Control, ControlBuilder, ControlData};
|
|
use crate::{
|
|
form::{FormBuilder, FormData},
|
|
styles::FormStyle,
|
|
};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub struct TextInputData {
|
|
pub(crate) name: String,
|
|
pub(crate) placeholder: Option<String>,
|
|
pub(crate) label: Option<String>,
|
|
pub(crate) initial_text: String,
|
|
pub(crate) input_type: &'static str,
|
|
}
|
|
|
|
impl Default for TextInputData {
|
|
fn default() -> Self {
|
|
TextInputData {
|
|
name: String::new(),
|
|
placeholder: None,
|
|
label: None,
|
|
initial_text: String::new(),
|
|
input_type: "input",
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ControlData for TextInputData {
|
|
type ReturnType = String;
|
|
|
|
fn build_control<FD: FormData, FS: FormStyle>(
|
|
fs: &FS,
|
|
control: Control<FD, FS, Self>,
|
|
) -> (View, Signal<Self::ReturnType>) {
|
|
fs.text_input(control)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
|
pub fn text_input(
|
|
self,
|
|
builder: impl Fn(ControlBuilder<FD, FS, TextInputData>) -> Control<FD, FS, TextInputData>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormData, FS: FormStyle> ControlBuilder<FD, FS, TextInputData> {
|
|
pub fn placeholder(mut self, placeholder: impl ToString) -> Self {
|
|
self.data.placeholder = Some(placeholder.to_string());
|
|
self
|
|
}
|
|
|
|
pub fn label(mut self, label: impl ToString) -> Self {
|
|
self.data.label = Some(label.to_string());
|
|
self
|
|
}
|
|
|
|
pub fn initial_text(mut self, text: impl ToString) -> Self {
|
|
self.data.initial_text = text.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn password(mut self) -> Self {
|
|
self.data.input_type = "password";
|
|
self
|
|
}
|
|
}
|