Skip to content

Commit

Permalink
Fixes for tests errors
Browse files Browse the repository at this point in the history
  • Loading branch information
VladToby committed Jul 26, 2024
1 parent 7a3dd59 commit 7cb0a98
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/core/BaseForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ export class BaseForm extends Block {
}

protected onSubmit() {
const formData: Record<string, string> = {};
const _formData: Record<string, string> = {};
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] = <string>child.getValue();
if (!child.validate()) {
isValid = false;
}
}
});

if (isValid) {
this.onValid(formData);
this.onValid(_formData);
} else {
this.onInvalid();
}
}

protected onValid(formData: Record<string, string>) {
protected onValid(_formData: Record<string, string>) {
// This method should be overridden in child classes
}

Expand Down
20 changes: 12 additions & 8 deletions src/core/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/core/EventBus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Props} from "./Block";

export class EventBus {
listeners: Record<string, Function[]>;

constructor() {
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] = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/RegistrationComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="vite/client" />

declare module "*.hbs?raw" {
const _: string;
export default _;
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7cb0a98

Please sign in to comment.