generated from mitchell/rust_template
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use leptos::{Signal, View};
|
|
|
|
use super::{ControlBuilder, ControlData, ControlRenderData};
|
|
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
pub struct CheckboxData {
|
|
pub(crate) name: String,
|
|
pub(crate) label: Option<String>,
|
|
}
|
|
|
|
impl ControlData for CheckboxData {
|
|
type ReturnType = bool;
|
|
|
|
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.checkbox(control, value_getter, value_setter)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
|
|
pub fn checkbox<FDT: Clone + PartialEq + 'static>(
|
|
self,
|
|
builder: impl Fn(
|
|
ControlBuilder<FD, FS, CheckboxData, FDT>,
|
|
) -> ControlBuilder<FD, FS, CheckboxData, FDT>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle, FDT> ControlBuilder<FD, FS, CheckboxData, FDT> {
|
|
pub fn named(mut self, control_name: impl ToString) -> Self {
|
|
self.data.name = control_name.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn labeled(mut self, label: impl ToString) -> Self {
|
|
self.data.label = Some(label.to_string());
|
|
self
|
|
}
|
|
}
|