fix: listeners should not use hard indexing

This commit is contained in:
Jonathan Kelley committed 2023-01-14 00:19:37 -08:00
1 parent 270de7a0d2
commit 8d59c626ea
1 file changed
+7 -4
+7 -4
View File
@@ -349,7 +349,7 @@ impl VirtualDom {
/// If you have multiple events, you can call this method multiple times before calling "render_with_deadline" /// If you have multiple events, you can call this method multiple times before calling "render_with_deadline"
pub fn handle_event( pub fn handle_event(
&mut self, &mut self,
name: &str, mut name: &str,
data: Rc<dyn Any>, data: Rc<dyn Any>,
element: ElementId, element: ElementId,
bubbles: bool, bubbles: bool,
@@ -384,6 +384,11 @@ impl VirtualDom {
data, data,
}; };
// Remove the "on" prefix if it exists, TODO, we should remove this and settle on one
if name.starts_with("on") {
name = &name[2..];
}
// Loop through each dynamic attribute in this template before moving up to the template's parent. // Loop through each dynamic attribute in this template before moving up to the template's parent.
while let Some(el_ref) = parent_path { while let Some(el_ref) = parent_path {
// safety: we maintain references of all vnodes in the element slab // safety: we maintain references of all vnodes in the element slab
@@ -394,9 +399,7 @@ impl VirtualDom {
for (idx, attr) in template.dynamic_attrs.iter().enumerate() { for (idx, attr) in template.dynamic_attrs.iter().enumerate() {
let this_path = node_template.attr_paths[idx]; let this_path = node_template.attr_paths[idx];
// listeners are required to be prefixed with "on", but they come back to the virtualdom with that missing if attr.name == name && target_path.is_ascendant(&this_path) {
// we should fix this so that we look for "onclick" instead of "click"
if &attr.name[2..] == name && target_path.is_ascendant(&this_path) {
listeners.push(&attr.value); listeners.push(&attr.value);
// Break if the event doesn't bubble anyways // Break if the event doesn't bubble anyways