material_dioxus/
switch.rs1use std::marker::PhantomData;
2
3use dioxus::prelude::*;
4use gloo::events::EventListener;
5use wasm_bindgen::prelude::*;
6
7use crate::StaticCallback;
8
9#[wasm_bindgen(module = "/build/mwc-switch.js")]
10extern "C" {
11 #[derive(Debug)]
12 type Switch;
13
14 #[wasm_bindgen(getter, static_method_of = Switch)]
15 fn _dummy_loader() -> JsValue;
16}
17
18loader_hack!(Switch);
19
20#[derive(Props)]
27pub struct SwitchProps<'a> {
28 #[props(default)]
29 pub selected: bool,
30 #[props(default)]
31 pub disabled: bool,
32 #[props(into)]
33 pub name: Option<String>,
34 #[props(into)]
35 pub value: Option<String>,
36 #[props(into)]
37 pub _onclick: Option<StaticCallback<()>>,
40 _lifetime: Option<PhantomData<&'a ()>>,
41
42 #[props(into, default)]
43 pub style: String,
44 #[props(into, default)]
45 pub class: String,
46 #[props(into)]
47 pub slot: Option<String>,
48 #[props(default)]
49 pub dialog_initial_focus: bool,
50}
51
52fn render<'a>(cx: Scope<'a, SwitchProps<'a>>) -> Element<'a> {
53 let id = crate::use_id(cx, "switch");
54 let click_listener = cx.use_hook(|| None);
55 if let Some(elem) = crate::get_elem_by_id(id) {
56 let target = elem;
57 if let Some(listener) = cx.props._onclick.clone() {
58 *click_listener = Some(EventListener::new(&target, "click", move |_| {
59 listener.call(())
60 }));
61 }
62 }
63
64 render! {
65 mwc-switch {
66 id: id,
67
68 selected: bool_attr!(cx.props.selected),
69 disabled: bool_attr!(cx.props.disabled),
70 name: optional_string_attr!(cx.props.name),
71 value: optional_string_attr!(cx.props.value),
72
73 style: string_attr!(cx.props.style),
74 class: string_attr!(cx.props.class),
75 slot: optional_string_attr!(cx.props.slot),
76 dialogInitialFocus: bool_attr!(cx.props.dialog_initial_focus),
77 }
78 }
79}
80
81component!('a, MatSwitch, SwitchProps, render, Switch, "switch");