1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use gloo::events::EventListener;
use wasm_bindgen::prelude::*;
use web_sys::Node;
use yew::prelude::*;
#[wasm_bindgen(module = "/build/mwc-switch.js")]
extern "C" {
#[derive(Debug)]
#[wasm_bindgen(extends = Node)]
type Switch;
#[wasm_bindgen(getter, static_method_of = Switch)]
fn _dummy_loader() -> JsValue;
#[wasm_bindgen(method, getter)]
fn checked(this: &Switch) -> bool;
#[wasm_bindgen(method, setter)]
fn set_checked(this: &Switch, value: bool);
}
loader_hack!(Switch);
pub struct MatSwitch {
node_ref: NodeRef,
change_listener: Option<EventListener>,
}
#[derive(Debug, Properties, PartialEq, Clone)]
pub struct SwitchProps {
#[prop_or_default]
pub checked: bool,
#[prop_or_default]
pub disabled: bool,
#[prop_or_default]
pub onchange: Callback<bool>,
}
impl Component for MatSwitch {
type Message = ();
type Properties = SwitchProps;
fn create(_: &Context<Self>) -> Self {
Switch::ensure_loaded();
Self {
node_ref: NodeRef::default(),
change_listener: None,
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
html! {
<mwc-switch
disabled={props.disabled}
ref={self.node_ref.clone()}
></mwc-switch>
}
}
fn rendered(&mut self, ctx: &Context<Self>, _first_render: bool) {
let props = ctx.props();
let element = self.node_ref.cast::<Switch>().unwrap();
element.set_checked(props.checked);
if self.change_listener.is_none() {
let callback = props.onchange.clone();
self.change_listener =
Some(EventListener::new(&element.clone(), "change", move |_| {
callback.emit(element.checked());
}));
}
}
}