yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications

Home Page:https://yew.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect help message when using a wrong method to unwrap an event.

wiseaidev opened this issue · comments

Current Behavior
Running the following snippet will throw an exception suggesting importing a missing module wasm_bindgen::cast::JsCast. However, the module cast under wasm_bindgen is private.

use yew::prelude::*;
use web_sys::{HtmlInputElement};

#[function_component(LoginForm)]
pub fn login_form<HtmlInputElement>() -> Html {
    let email_ref = NodeRef::default();
    let on_email_change = Callback::from(move |event: Event| {
        let target = event.target().unwrap();
        let value = target.unchecked_into::<HtmlInputElement>().value();
    });
      html! {
          <div>
              <form>
                  <div class="mb-3">
                      <input
                          class="form-control form-control-lg"
                          type="email"
                          placeholder="Email"
                          ref={email_ref}
                          oninput={on_email_change}
                          id="email-input"
                      />
                  </div>
              </form>
          </div>
    }
}

fn main() {
    yew::Renderer::<LoginForm<HtmlInputElement>>::new().render();
}

Output

error[E0599]: no method named `unchecked_into` found for struct `EventTarget` in the current scope
  --> src/main.rs:9:28
   |
9  |         let value = target.unchecked_into::<HtmlInputElement>().value();
   |                            ^^^^^^^^^^^^^^
   |
  ::: /home/mahmoud/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-0.2.84/src/cast.rs:78:8
   |
78 |     fn unchecked_into<T>(self) -> T
   |        -------------- the method is available for `EventTarget` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use wasm_bindgen::cast::JsCast;
   |
help: there is a method with a similar name
   |
9  |         let value = target.unchecked_into_f64::<HtmlInputElement>().value();
   |                            ~~~~~~~~~~~~~~~~~~

Expected Behavior
Regardless of whether the implementation of the event handler is correct or not, it should suggest:

use wasm_bindgen::JsCast

Instead of:

use wasm_bindgen::cast::JsCast;

Environment:

  • Yew version: 0.20.0
  • Rust version: rustc 1.68.2 (9eb3afe9e 2023-03-27)
  • Target, if relevant: wasm32-unknown-unknown
  • Build tool, if relevant: trunk
  • OS, if relevant: Ubuntu 22.04.2 LTS
  • Browser and version, if relevant: Any.

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later

I think this is actually a bug with the Rust compiler and not related to Yew.

For the following code:

use web_sys::window;
use web_sys::HtmlInputElement;

fn main() {
    let el = window()
        .and_then(|m| m.document())
        .and_then(|m| m.query_selector("#input").ok().flatten())
        .expect("failed to find input!");

    let _value = el.unchecked_into::<HtmlInputElement>().value();
}

Rust will emit the following errors:

error[E0599]: no method named `unchecked_into` found for struct `web_sys::Element` in the current scope
  --> src/main.rs:10:21
   |
10 |     let _value = el.unchecked_into::<HtmlInputElement>().value();
   |                     ^^^^^^^^^^^^^^
   |
  ::: /Users/futursolo/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-0.2.84/src/cast.rs:78:8
   |
78 |     fn unchecked_into<T>(self) -> T
   |        -------------- the method is available for `web_sys::Element` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use wasm_bindgen::cast::JsCast;
   |
help: there is a method with a similar name
   |
10 |     let _value = el.unchecked_into_f64::<HtmlInputElement>().value();
   |                     ~~~~~~~~~~~~~~~~~~

I am closing this issue as I do not think this is related to Yew.