Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pending event listener on the VTag #2300

Merged
merged 3 commits into from Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/yew/src/virtual_dom/vtag.rs
Expand Up @@ -8,6 +8,7 @@ use std::borrow::Cow;
use std::cmp::PartialEq;
use std::hint::unreachable_unchecked;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::rc::Rc;
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -430,8 +431,22 @@ impl VTag {
.insert(key, value.into_prop_value());
}

/// Add event listener on the [VTag]'s [Element].
/// Returns `true` if the listener has been added, `false` otherwise.
pub fn add_listener(&mut self, listener: Rc<dyn Listener>) -> bool {
if let Listeners::Pending(listeners) = &mut self.listeners {
let mut listeners = mem::take(listeners).into_vec();
listeners.push(Some(listener));

self.set_listeners(listeners.into_boxed_slice());
true
} else {
false
voidpumpkin marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Set event listeners on the [VTag]'s [Element]
pub fn set_listener(&mut self, listeners: Box<[Option<Rc<dyn Listener>>]>) {
pub fn set_listeners(&mut self, listeners: Box<[Option<Rc<dyn Listener>>]>) {
self.listeners = Listeners::Pending(listeners);
}

Expand Down