web_tools/optimistic/
html_input_element.rs

1use web_sys::wasm_bindgen::JsCast;
2
3/// Support working with [`web_sys::HtmlInputElement`]
4pub trait OptimisticHtmlInputElement {
5    /// Get the [`web_sys::HtmlFormElement`] of an input.
6    fn form(&self) -> Option<web_sys::HtmlFormElement>;
7
8    /// Get the [`web_sys::HtmlInputElement::checked`] state of an input.
9    fn checked(&self) -> bool;
10}
11
12impl OptimisticHtmlInputElement for web_sys::Node {
13    fn form(&self) -> Option<web_sys::HtmlFormElement> {
14        self.dyn_ref::<web_sys::HtmlInputElement>()
15            .and_then(|input| input.form())
16    }
17
18    fn checked(&self) -> bool {
19        self.dyn_ref::<web_sys::HtmlInputElement>()
20            .map(|input| input.checked())
21            .unwrap_or_default()
22    }
23}
24
25#[cfg(feature = "yew")]
26impl OptimisticHtmlInputElement for yew::prelude::NodeRef {
27    fn form(&self) -> Option<web_sys::HtmlFormElement> {
28        self.cast::<web_sys::HtmlInputElement>()
29            .and_then(|input| input.form())
30    }
31
32    fn checked(&self) -> bool {
33        self.cast::<web_sys::HtmlInputElement>()
34            .map(|input| input.checked())
35            .unwrap_or_default()
36    }
37}