common: move the component children prop logic to Component class instead of patchMithril

Should make easier debugging if something doesn't work as well
This commit is contained in:
David Sevilla Martin 2020-03-18 16:51:38 -04:00
parent 66b839d241
commit babbda044b
No known key found for this signature in database
GPG Key ID: F764F1417E16B15F
2 changed files with 16 additions and 20 deletions

View File

@ -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<T extends ComponentProps = any> 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<T extends ComponentProps = any> implements ClassC
static initProps(props: ComponentProps = {}) {}
private setProps(props: T) {
private setProps(vnode: Vnode<T, this>) {
const props = vnode.attrs || {};
(this.constructor as typeof Component).initProps(props);
if (!props.children) props.children = vnode.children;
this.props = props;
}
}

View File

@ -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) {
// allow writing to children attribute
Object.defineProperty(arguments[1], 'children', {
writable: true,
});
arguments[1].children = (arguments[1].children || []).concat(children);
}
}
const node = mo.apply(this, arguments);