use std::marker::PhantomData;
use dioxus::prelude::*;
use gloo::events::EventListener;
use rand::distributions::{Alphanumeric, DistString};
use wasm_bindgen::prelude::*;
use web_sys::Node;
use crate::utils::StaticCallback;
#[wasm_bindgen(module = "/build/mwc-radio.js")]
extern "C" {
#[derive(Debug)]
#[wasm_bindgen(extends = Node)]
type Radio;
#[wasm_bindgen(getter, static_method_of = Radio)]
fn _dummy_loader() -> JsValue;
#[wasm_bindgen(method, getter)]
fn checked(this: &Radio) -> bool;
#[wasm_bindgen(method, setter)]
fn set_checked(this: &Radio, value: bool);
}
loader_hack!(Radio);
#[derive(Props)]
pub struct RadioProps<'a> {
#[props(default)]
pub checked: bool,
#[props(default)]
pub disabled: bool,
#[props(into)]
pub name: Option<String>,
#[props(into)]
pub value: Option<String>,
#[props(default)]
pub global: bool,
#[props(default)]
pub reduced_touch_target: bool,
#[props(into)]
pub _onchange: Option<StaticCallback<bool>>,
_lifetime: Option<PhantomData<&'a ()>>,
}
fn render<'a>(cx: Scope<'a, RadioProps<'a>>) -> Element<'a> {
let id = cx
.use_hook(|| {
let mut id = String::from("radio-");
Alphanumeric.append_string(&mut rand::thread_rng(), &mut id, 11);
cx.needs_update();
id
})
.as_str();
let change_listener = cx.use_hook(|| None);
if let Some(elem) = web_sys::window()
.unwrap()
.document()
.unwrap()
.get_element_by_id(id)
{
let target = elem.clone();
let radio = JsValue::from(elem).dyn_into::<Radio>().unwrap();
radio.set_checked(cx.props.checked);
if let Some(listener) = cx.props._onchange.clone() {
*change_listener = Some(EventListener::new(&target, "change", move |_| {
listener.call(radio.checked())
}));
}
}
render! {
mwc-radio {
id: id,
"disabled": bool_attr!(cx.props.disabled),
"name": optional_string_attr!(cx.props.name),
"value": optional_string_attr!(cx.props.value),
"global": bool_attr!(cx.props.global),
"reducedTouchTarget": bool_attr!(cx.props.reduced_touch_target),
}
}
}
component!('a, MatRadio, RadioProps, render, Radio, "radio");