querySelector/querySelectorAll on Widgets

This commit is contained in:
Tony Garnock-Jones 2024-04-17 13:24:48 +02:00
parent bf9d10813e
commit 9d8e7f5ccd
1 changed files with 16 additions and 0 deletions

View File

@ -98,6 +98,22 @@ export class Widget implements EventTarget {
return this;
}
querySelector(selector: string): Widget | null;
querySelector<T extends Widget>(selector: string, ctor: { new(e: Element): T }): T | null;
querySelector<T extends Widget>(selector: string, ctor?: { new(e: Element): T }): Widget | null {
const e = this._nodeAsParent?.querySelector(selector);
return e ? new (ctor ?? Widget)(e) : null;
}
querySelectorAll(selector: string): Widget[];
querySelectorAll<T extends Widget>(selector: string, ctor: { new(e: Element): T }): T[];
querySelectorAll<T extends Widget>(selector: string, ctor?: { new(e: Element): T }): Widget[] {
const es = this._nodeAsParent?.querySelectorAll(selector);
const ws: Widget[] = [];
if (es) es.forEach(e => ws.push(new (ctor ?? Widget)(e)));
return ws;
}
on(type: string, callback: EventListenerOrEventListenerObject): this {
this.addEventListener(type, callback);
return this;