mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 17:57:04 +08:00
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:
parent
66b839d241
commit
babbda044b
|
@ -1,4 +1,4 @@
|
||||||
import Mithril, { ClassComponent } from 'mithril';
|
import Mithril, { ClassComponent, Vnode } from 'mithril';
|
||||||
|
|
||||||
export type ComponentProps = {
|
export type ComponentProps = {
|
||||||
children?: Mithril.Children;
|
children?: Mithril.Children;
|
||||||
|
@ -22,28 +22,28 @@ export default class Component<T extends ComponentProps = any> implements ClassC
|
||||||
}
|
}
|
||||||
|
|
||||||
oninit(vnode) {
|
oninit(vnode) {
|
||||||
this.setProps(vnode.attrs);
|
this.setProps(vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
oncreate(vnode) {
|
oncreate(vnode) {
|
||||||
this.setProps(vnode.attrs);
|
this.setProps(vnode);
|
||||||
this.element = vnode.dom;
|
this.element = vnode.dom;
|
||||||
}
|
}
|
||||||
|
|
||||||
onbeforeupdate(vnode) {
|
onbeforeupdate(vnode) {
|
||||||
this.setProps(vnode.attrs);
|
this.setProps(vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
onupdate(vnode) {
|
onupdate(vnode) {
|
||||||
this.setProps(vnode.attrs);
|
this.setProps(vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
onbeforeremove(vnode) {
|
onbeforeremove(vnode) {
|
||||||
this.setProps(vnode.attrs);
|
this.setProps(vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
onremove(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 = {}) {}
|
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);
|
(this.constructor as typeof Component).initProps(props);
|
||||||
|
|
||||||
|
if (!props.children) props.children = vnode.children;
|
||||||
|
|
||||||
this.props = props;
|
this.props = props;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,18 +10,10 @@ export default () => {
|
||||||
if (!arguments[1]) arguments[1] = {};
|
if (!arguments[1]) arguments[1] = {};
|
||||||
|
|
||||||
if (comp.prototype && comp.prototype instanceof Component) {
|
if (comp.prototype && comp.prototype instanceof Component) {
|
||||||
let children = args.slice(1);
|
// allow writing to children attribute
|
||||||
if (children.length === 1 && Array.isArray(children[0])) {
|
|
||||||
children = children[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (children) {
|
|
||||||
Object.defineProperty(arguments[1], 'children', {
|
Object.defineProperty(arguments[1], 'children', {
|
||||||
writable: true,
|
writable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
arguments[1].children = (arguments[1].children || []).concat(children);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = mo.apply(this, arguments);
|
const node = mo.apply(this, arguments);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user