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 possibility to cancel bubbling #2172

Merged
merged 4 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions packages/yew/src/virtual_dom/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl Registry {

run_handler(&target);

if unsafe { BUBBLE_EVENTS } {
if unsafe { BUBBLE_EVENTS } && !event.cancel_bubble() {
let mut el = target;
loop {
el = match el.parent_element() {
Expand All @@ -523,7 +523,7 @@ mod tests {
use std::marker::PhantomData;

use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use web_sys::{Event, EventInit};
use web_sys::{Event, EventInit, MouseEvent};
wasm_bindgen_test_configure!(run_in_browser);

use crate::{html, html::TargetCast, AppHandle, Component, Context, Html};
Expand Down Expand Up @@ -794,6 +794,40 @@ mod tests {
assert_count(&el, 4);
}

#[test]
fn cancel_bubbling() {
struct CancelBubbling;

impl Mixin for CancelBubbling {
fn view<C>(ctx: &Context<C>, state: &State) -> Html
where
C: Component<Message = Message>,
{
html! {
<div onclick={ctx.link().callback(|_| Message::Action)}>
<a onclick={ctx.link().callback(|mouse_event: MouseEvent| {
let event: Event = mouse_event.dyn_into().unwrap();
event.stop_propagation();
Message::Action
})}>
{state.action}
</a>
</div>
}
}
}

let (_, el) = init::<CancelBubbling>("a");

assert_count(&el, 0);

el.click();
assert_count(&el, 1);

el.click();
assert_count(&el, 2);
}

fn test_input_listener<E>(make_event: impl Fn() -> E)
where
E: JsCast + std::fmt::Debug,
Expand Down Expand Up @@ -858,7 +892,7 @@ mod tests {
test_input_listener(|| {
web_sys::InputEvent::new_with_event_init_dict(
"input",
&web_sys::InputEventInit::new().bubbles(true),
web_sys::InputEventInit::new().bubbles(true),
)
.unwrap()
})
Expand All @@ -869,7 +903,7 @@ mod tests {
test_input_listener(|| {
web_sys::Event::new_with_event_init_dict(
"change",
&web_sys::EventInit::new().bubbles(true),
web_sys::EventInit::new().bubbles(true),
)
.unwrap()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ mod benchmarks {
let static_ = Attributes::Static(&[]);
let dynamic = Attributes::Dynamic {
keys: &[],
values: vec![],
values: Box::new([]),
};
let map = Attributes::IndexMap(Default::default());

Expand Down Expand Up @@ -776,7 +776,7 @@ mod benchmarks {
fn make_dynamic(values: Vec<AttrValue>) -> Attributes {
Attributes::Dynamic {
keys: sample_keys(),
values: values.into_iter().map(|v| Some(v)).collect(),
values: values.into_iter().map(Some).collect(),
}
}

Expand Down