How to safely forward vnode.attrs to other vnodes? #3012
-
I'm trying to understand how should I work with Mithril Components, and I'm getting a bit confused. According to: https://mithril.js.org/components.html#don't-forward-vnodeattrs-itself-to-other-vnodes I should not forward I'm trying to create a simple button component to apply a common style to all the buttons in my application.
But when I use it like this:
Everything looks as expected, except the
Everything works including the What am I misunderstanding? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You at minimum have to forward var MyButton = {
view: function (vnode) {
return m('button.my-class', {title: vnode.attrs.tooltip || '', onclick: vnode.attrs.onclick}, vnode.children);
}
}; Event listeners are only called on the DOM vnode itself, not on outer component attributes as well. Or to put it another way, events only bubble through the DOM itself, not through Mithril. Worth mentioning that this is also the case with native custom elements unless |
Beta Was this translation helpful? Give feedback.
@mondolirondo You're looking for https://mithril.js.org/censor.html, which does exactly that. It also covers a couple bases you missed:
attrs.key
, so it won't muck with fragments. You probably don't want stuff like[m("a", m.censor(attrs)), m("input[type=number")]
to be seen as conditionally-keyed.delete
, instead building the object manually and just skipping those keys.