From babbda044b226cb9b96caf12f715ceb531209ff4 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Wed, 18 Mar 2020 16:51:38 -0400 Subject: [PATCH] common: move the component children prop logic to Component class instead of patchMithril Should make easier debugging if something doesn't work as well --- js/src/common/Component.ts | 20 ++++++++++++-------- js/src/common/utils/patchMithril.ts | 16 ++++------------ 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/js/src/common/Component.ts b/js/src/common/Component.ts index 08e6d3e76..21e4c2bd4 100644 --- a/js/src/common/Component.ts +++ b/js/src/common/Component.ts @@ -1,4 +1,4 @@ -import Mithril, { ClassComponent } from 'mithril'; +import Mithril, { ClassComponent, Vnode } from 'mithril'; export type ComponentProps = { children?: Mithril.Children; @@ -22,28 +22,28 @@ export default class Component implements ClassC } oninit(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); } oncreate(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); this.element = vnode.dom; } onbeforeupdate(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); } onupdate(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); } onbeforeremove(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); } onremove(vnode) { - this.setProps(vnode.attrs); + this.setProps(vnode); } /** @@ -78,9 +78,13 @@ export default class Component implements ClassC static initProps(props: ComponentProps = {}) {} - private setProps(props: T) { + private setProps(vnode: Vnode) { + const props = vnode.attrs || {}; + (this.constructor as typeof Component).initProps(props); + if (!props.children) props.children = vnode.children; + this.props = props; } } diff --git a/js/src/common/utils/patchMithril.ts b/js/src/common/utils/patchMithril.ts index afce5c777..8b78b67e4 100644 --- a/js/src/common/utils/patchMithril.ts +++ b/js/src/common/utils/patchMithril.ts @@ -10,18 +10,10 @@ export default () => { if (!arguments[1]) arguments[1] = {}; if (comp.prototype && comp.prototype instanceof Component) { - let children = args.slice(1); - if (children.length === 1 && Array.isArray(children[0])) { - children = children[0]; - } - - if (children) { - Object.defineProperty(arguments[1], 'children', { - writable: true, - }); - - arguments[1].children = (arguments[1].children || []).concat(children); - } + // allow writing to children attribute + Object.defineProperty(arguments[1], 'children', { + writable: true, + }); } const node = mo.apply(this, arguments);