1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use web_sys::{HtmlElement, HtmlInputElement};
use yew::NodeRef;

/// Focus an HTML input element.
///
/// The ref must point to an [`HtmlElement`], if it does not, the function does nothing.
pub fn focus(node_ref: &NodeRef) {
    if let Some(input) = node_ref.cast::<HtmlElement>() {
        let _ = input.focus();
    }
}

/// Retrieve the value of an [`HtmlInputElement`].
///
/// The ref must point to an [`HtmlInputElement`], if it does not, the function will return [`None`].
pub fn value(node_ref: &NodeRef) -> Option<String> {
    node_ref
        .cast::<HtmlInputElement>()
        .map(|input| input.value())
}