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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::{bool_to_option, to_option_string};
use gloo::events::EventListener;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{CustomEvent, Element};
use yew::prelude::*;
#[wasm_bindgen(module = "/build/mwc-slider.js")]
extern "C" {
#[derive(Debug)]
type Slider;
#[wasm_bindgen(getter, static_method_of = Slider)]
fn _dummy_loader() -> JsValue;
}
loader_hack!(Slider);
pub struct MatSlider {
node_ref: NodeRef,
input_listener: Option<EventListener>,
change_listener: Option<EventListener>,
}
#[derive(Debug, Properties, PartialEq, Clone)]
pub struct SliderProps {
#[prop_or(0)]
pub value: u32,
#[prop_or(0)]
pub min: u32,
#[prop_or(100)]
pub max: u32,
#[prop_or(1)]
pub step: u32,
#[prop_or(false)]
pub pin: bool,
#[prop_or(false)]
pub markers: bool,
#[prop_or_default]
pub oninput: Callback<CustomEvent>,
#[prop_or_default]
pub onchange: Callback<CustomEvent>,
}
impl Component for MatSlider {
type Message = ();
type Properties = SliderProps;
fn create(_: &Context<Self>) -> Self {
Slider::ensure_loaded();
Self {
node_ref: NodeRef::default(),
input_listener: None,
change_listener: None,
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
html! {
<mwc-slider
value={to_option_string(props.value)}
min={to_option_string(props.min)}
max={to_option_string(props.max)}
step={to_option_string(props.step)}
pin={bool_to_option(props.pin)}
markers={bool_to_option(props.markers)}
ref={self.node_ref.clone()}
></mwc-slider>
}
}
fn rendered(&mut self, ctx: &Context<Self>, _first_render: bool) {
let props = ctx.props();
let element = self.node_ref.cast::<Element>().unwrap();
if self.input_listener.is_none() {
let oninput = props.oninput.clone();
self.input_listener = Some(EventListener::new(&element, "input", move |event| {
oninput.emit(JsValue::from(event).unchecked_into::<CustomEvent>())
}));
};
if self.change_listener.is_none() {
let onchange = props.onchange.clone();
self.change_listener = Some(EventListener::new(&element, "change", move |event| {
onchange.emit(JsValue::from(event).unchecked_into::<CustomEvent>())
}));
}
}
}