This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
leptos_form_tool/src/controls/text_input.rs

76 lines
2.2 KiB
Rust

use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData};
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
use leptos::{Signal, View};
#[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<FS: FormStyle>(
fs: &FS,
control: ControlRenderData<FS, Self>,
value_getter: Signal<Self::ReturnType>,
value_setter: Box<dyn Fn(Self::ReturnType)>,
validation_state: Signal<Result<(), String>>,
) -> View {
fs.text_input(control, value_getter, value_setter, validation_state)
}
}
impl ValidatedControlData for TextInputData {}
impl<FD: FormToolData> FormBuilder<FD> {
pub fn text_input<FDT: Clone + PartialEq + 'static>(
self,
builder: impl BuilderFn<ControlBuilder<FD, TextInputData, FDT>, FD::Context>,
) -> Self {
self.new_control(builder)
}
}
impl<FD: FormToolData, FDT> ControlBuilder<FD, TextInputData, FDT> {
pub fn named(mut self, control_name: impl ToString) -> Self {
self.data.name = control_name.to_string();
self
}
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
}
}