common: add constructor & render method to Component

This commit is contained in:
David Sevilla Martin 2020-01-31 18:28:54 -05:00
parent 220a36c2e4
commit 66745916b3
No known key found for this signature in database
GPG Key ID: F764F1417E16B15F

View File

@ -11,7 +11,11 @@ export type ComponentProps = {
export default class Component<T extends ComponentProps = any> {
element: HTMLElement;
props = <T>{};
props: T;
constructor(props: T = <T>{}) {
this.props = props;
}
view(vnode) {
throw new Error('Component#view must be implemented by subclass');
@ -60,6 +64,10 @@ export default class Component<T extends ComponentProps = any> {
return selector ? $element.find(selector) : $element;
}
render() {
return m(this, this.props);
}
static component(props: ComponentProps | any = {}, children?: Mithril.Children) {
const componentProps: ComponentProps = Object.assign({}, props);