import {UIElement} from "../UIElement"; import {UIEventSource} from "../UIEventSource"; import {UIInputElement} from "./UIInputElement"; export class TextField extends UIInputElement { private value: UIEventSource; private mappedValue: UIEventSource; /** * Pings and has the value data */ public enterPressed = new UIEventSource(undefined); private _placeholder: UIEventSource; private _pretext: string; private _fromString: (string: string) => T; constructor(options: { placeholder?: UIEventSource, toString: (t: T) => string, fromString: (string: string) => T, pretext?: string, value?: UIEventSource }) { super(options?.placeholder); this.value = new UIEventSource(""); this.mappedValue = options?.value ?? new UIEventSource(undefined); this.value.addCallback((str) => this.mappedValue.setData(options.fromString(str))); this.mappedValue.addCallback((t) => this.value.setData(options.toString(t))); this._placeholder = options?.placeholder ?? new UIEventSource(""); this._pretext = options?.pretext ?? ""; const self = this; this.mappedValue.addCallback((t) => { if (t === undefined && t === null) { return; } const field = document.getElementById('text-' + this.id); if (field === undefined && field === null) { return; } field.value = options.toString(t); }) } GetValue(): UIEventSource { return this.mappedValue; } protected InnerRender(): string { return this._pretext + "
" + "" + "
"; } InnerUpdate(htmlElement: HTMLElement) { const field = document.getElementById('text-' + this.id); const self = this; field.oninput = () => { // @ts-ignore self.value.setData(field.value); }; field.addEventListener("keyup", function (event) { if (event.key === "Enter") { // @ts-ignore self.enterPressed.setData(field.value); } }); } Clear() { const field = document.getElementById('text-' + this.id); if (field !== undefined) { // @ts-ignore field.value = ""; } } }