diff --git a/src/core/BaseForm.ts b/src/core/BaseForm.ts index f6721b8..15667ed 100644 --- a/src/core/BaseForm.ts +++ b/src/core/BaseForm.ts @@ -15,13 +15,13 @@ export class BaseForm extends Block { } protected onSubmit() { - const formData: Record = {}; + const _formData: Record = {}; let isValid: boolean = true; Object.values(this.children).forEach((child: Block | Element) => { if (child instanceof Input) { - const { name } = child.props; - formData[name] = child.getValue(); + const name = child.props.name as string; + _formData[name] = child.getValue(); if (!child.validate()) { isValid = false; } @@ -29,13 +29,13 @@ export class BaseForm extends Block { }); if (isValid) { - this.onValid(formData); + this.onValid(_formData); } else { this.onInvalid(); } } - protected onValid(formData: Record) { + protected onValid(_formData: Record) { // This method should be overridden in child classes } diff --git a/src/core/Block.ts b/src/core/Block.ts index 997244e..35bfa49 100644 --- a/src/core/Block.ts +++ b/src/core/Block.ts @@ -20,7 +20,7 @@ class Block { children: Children; eventBus: () => EventBus; - protected constructor(propsAndChildren: Props | Children) { + constructor(propsAndChildren: Props | Children) { const eventBus = new EventBus(); const { props, children} = this._getChildrenAndProps(propsAndChildren); @@ -39,7 +39,7 @@ class Block { const children: Children = {}; Object.entries(propsAndChildren).forEach(([key, value]) => { - if (value instanceof Block) { + if (value instanceof Block || value instanceof Element) { children[key] = value; } else { props[key] = value; @@ -141,8 +141,8 @@ class Block { } private _compile(): DocumentFragment { - const template: string = this.render(); - const fragment: HTMLElement = this._createDocumentElement('template'); + const template = this.render(); + const fragment = document.createElement('template'); const context = { ...this.props, @@ -151,15 +151,19 @@ class Block { fragment.innerHTML = Handlebars.compile(template)(context); - Object.entries(this.children).forEach(([id, component]) => { + Object.entries(this.children).forEach(([id, child]) => { const stub = fragment.content.querySelector(`[data-id="${id}"]`); if (!stub) { return; } - const content = component.getContent(); - if (content) { - stub.replaceWith(content); + if (child instanceof Block) { + const content = child.getContent(); + if (content) { + stub.replaceWith(content); + } + } else if (child instanceof Element) { + stub.replaceWith(child); } }); diff --git a/src/core/EventBus.ts b/src/core/EventBus.ts index fa6a0b8..375cffd 100644 --- a/src/core/EventBus.ts +++ b/src/core/EventBus.ts @@ -1,3 +1,5 @@ +import {Props} from "./Block"; + export class EventBus { listeners: Record; @@ -5,7 +7,7 @@ export class EventBus { this.listeners = {}; } - on(event: string, callback: () => void): void { + on(event: string, callback: OmitThisParameter<(oldProps: Props, newProps: Props) => void>): void { if (!this.listeners[event]) { this.listeners[event] = []; } diff --git a/src/core/RegistrationComponent.ts b/src/core/RegistrationComponent.ts index ee3e5a6..5bbbadd 100644 --- a/src/core/RegistrationComponent.ts +++ b/src/core/RegistrationComponent.ts @@ -4,7 +4,7 @@ import Block from "./Block"; export function registerComponent(name: string, Component: typeof Block) { Handlebars.registerHelper(name, function(this: any, { hash, data }) { const component: Block = new Component(hash); - const id = `${name}-${component.id}`; + const id: string = `${name}-${component.id}`; if (hash.ref) { (data.root.__refs = data.root.__refs || {})[hash.ref] = component; diff --git a/src/index.d.ts b/src/index.d.ts index 682af27..5a4238d 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,3 +1,5 @@ +/// + declare module "*.hbs?raw" { const _: string; export default _; diff --git a/src/main.ts b/src/main.ts index 679d29b..75c0de5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,7 +36,7 @@ registerImports(components); registerImports(pages); const navigator = (pageName: string) => { - const Page = pages[pageName]; + const Page: any = pages[pageName]; if (Page) { const app = document.getElementById('app'); if (app) {