zino_dioxus/form/
checkbox.rs

1use crate::class::Class;
2use dioxus::prelude::*;
3
4/// The 2-state checkbox in its native format.
5pub fn Checkbox(props: CheckboxProps) -> Element {
6    rsx! {
7        label {
8            class: props.label_class,
9            input {
10                class: props.class,
11                r#type: "checkbox",
12                onchange: move |event| {
13                    if let Some(handler) = props.on_change.as_ref() {
14                        handler.call(event.value());
15                    }
16                },
17                ..props.attributes,
18            }
19            { props.children }
20        }
21    }
22}
23
24/// The [`Checkbox`] properties struct for the configuration of the component.
25#[derive(Clone, PartialEq, Props)]
26pub struct CheckboxProps {
27    /// The class attribute for the component.
28    #[props(into, default = "checkbox")]
29    pub class: Class,
30    /// A class to apply to the `label` element.
31    #[props(into, default = "checkbox")]
32    pub label_class: Class,
33    /// An event handler to be called when the checkbox's state is changed.
34    pub on_change: Option<EventHandler<String>>,
35    /// Spreading the props of the `input` element.
36    #[props(extends = input)]
37    attributes: Vec<Attribute>,
38    /// The children to render within the component.
39    children: Element,
40}