use std::marker::PhantomData;
use dioxus::prelude::*;
use gloo::events::EventListener;
use wasm_bindgen::prelude::*;
use web_sys::Node;
use crate::utils::StaticCallback;
#[wasm_bindgen(module = "/build/mwc-checkbox.js")]
extern "C" {
#[derive(Debug)]
#[wasm_bindgen(extends = Node)]
type Checkbox;
#[wasm_bindgen(getter, static_method_of = Checkbox)]
fn _dummy_loader() -> JsValue;
#[wasm_bindgen(method, setter)]
fn set_checked(this: &Checkbox, value: bool);
#[wasm_bindgen(method, getter)]
fn checked(this: &Checkbox) -> bool;
}
loader_hack!(Checkbox);
#[derive(Props)]
pub struct CheckboxProps<'a> {
#[props(default)]
pub checked: bool,
#[props(default)]
pub indeterminate: bool,
#[props(default)]
pub disabled: bool,
#[props(into)]
pub value: Option<String>,
#[props(default)]
pub reduced_touch_target: bool,
#[props(into)]
pub _onchange: Option<StaticCallback<bool>>,
_lifetime: Option<PhantomData<&'a ()>>,
#[props(into, default)]
pub style: String,
#[props(into, default)]
pub class: String,
#[props(into)]
pub slot: Option<String>,
#[props(default)]
pub dialog_initial_focus: bool,
}
fn render<'a>(cx: Scope<'a, CheckboxProps<'a>>) -> Element<'a> {
let id = crate::use_id(cx, "checkbox");
let change_listener = cx.use_hook(|| None);
if let Some(elem) = crate::get_elem_by_id(id) {
let target = elem.clone();
let cb = JsValue::from(elem).dyn_into::<Checkbox>().unwrap();
cb.set_checked(cx.props.checked);
if let Some(listener) = cx.props._onchange.clone() {
*change_listener = Some(EventListener::new(&target, "change", move |_| {
listener.call(cb.checked())
}));
}
}
render! {
mwc-checkbox {
id: id,
indeterminate: bool_attr!(cx.props.indeterminate),
disabled: cx.props.disabled,
value: optional_string_attr!(cx.props.value),
reducedTouchTarget: bool_attr!(cx.props.reduced_touch_target),
style: string_attr!(cx.props.style),
class: string_attr!(cx.props.class),
slot: optional_string_attr!(cx.props.slot),
dialogInitialFocus: bool_attr!(cx.props.dialog_initial_focus),
}
}
}
component!('a, MatCheckbox, CheckboxProps, render, Checkbox, "checkbox");