use std::sync::Arc;
use super::form_field::form_field;
use crate::{app::ctx::Ctx, components::Control, node::Element, ui};
#[derive(Clone, Debug, PartialEq, crate::DevtoolsInspectable)]
pub struct FormCheckboxInputProps {
pub control: Control<bool>,
pub label: Option<Arc<str>>,
pub hint: Option<Arc<str>>,
}
impl FormCheckboxInputProps {
pub fn new(control: Control<bool>) -> Self {
Self {
control,
label: None,
hint: None,
}
}
pub fn label(mut self, label: impl Into<Arc<str>>) -> Self {
self.label = Some(label.into());
self
}
pub fn hint(mut self, hint: impl Into<Arc<str>>) -> Self {
self.hint = Some(hint.into());
self
}
}
pub struct FormCheckboxInput;
impl crate::app::component::Component for FormCheckboxInput {
type Props = FormCheckboxInputProps;
fn create(_ctx: &mut Ctx) -> Self {
Self
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let props = ctx.props::<Self::Props>().clone();
let control = ctx.form_control(&props.control);
let blur_control = control.clone();
let label = props.label.as_deref().unwrap_or("");
let checkbox = ui::Checkbox::new(label)
.label_hidden(label.is_empty())
.bind(control.value())
.name(control.name())
.on_leave(move || {
blur_control.mark_touched();
blur_control.validate();
});
form_field(&control, None, props.hint.as_deref(), checkbox)
}
}