generated from mitchell/rust_template
45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData};
|
|
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
|
|
use leptos::{Signal, View};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
pub struct CheckboxData {
|
|
pub name: String,
|
|
pub 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> FormBuilder<FD> {
|
|
pub fn checkbox<FDT: Clone + PartialEq + 'static>(
|
|
self,
|
|
builder: impl BuilderFn<ControlBuilder<FD, CheckboxData, FDT>, FD::Context>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FDT> ControlBuilder<FD, 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
|
|
}
|
|
}
|