From eab842d18ac707f5fdad35b6339df01e0191f323 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Wed, 9 Sep 2020 22:17:46 +0200 Subject: [PATCH] Add multi-checkbox component --- UI/Input/Checkboxes.ts | 113 ++++++++++++++++++++++++++++++++++++++++ UI/Input/RadioButton.ts | 6 +-- test.ts | 24 ++++++++- 3 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 UI/Input/Checkboxes.ts diff --git a/UI/Input/Checkboxes.ts b/UI/Input/Checkboxes.ts new file mode 100644 index 000000000..2e5c27472 --- /dev/null +++ b/UI/Input/Checkboxes.ts @@ -0,0 +1,113 @@ +import {InputElement} from "./InputElement"; +import {UIEventSource} from "../../Logic/UIEventSource"; +import {Utils} from "../../Utils"; + +/** + * Supports multi-input + */ +export class CheckBoxes extends InputElement { + IsSelected: UIEventSource = new UIEventSource(false); + + private readonly _selectedElementIndex: UIEventSource + = new UIEventSource(null); + + private readonly value: UIEventSource; + private readonly _elements: InputElement[] + private readonly _selectFirstAsDefault: boolean; + + + constructor(elements: InputElement[], + selectFirstAsDefault = true) { + super(undefined); + this._elements = Utils.NoNull(elements); + this._selectFirstAsDefault = selectFirstAsDefault; + + this.value = new UIEventSource([]) + this.ListenTo(this.value); + + } + + IsValid(ts: T[]): boolean { + if (ts === undefined) { + return false; + } + for (const t of ts) { + let matchFound = false; + for (const element of this._elements) { + if (element.IsValid(t)) { + matchFound = true; + break + } + } + if (!matchFound) { + return false; + } + } + return true; + } + + GetValue(): UIEventSource { + return this.value; + } + + + private IdFor(i) { + return 'checkmark-' + this.id + '-' + i; + } + + InnerRender(): string { + + let body = ""; + for (let i = 0; i < this._elements.length; i++) { + let el = this._elements[i]; + const htmlElement = + `
`; + body += htmlElement; + + } + + return `
${body}
`; + } + + protected InnerUpdate(htmlElement: HTMLElement) { + super.InnerUpdate(htmlElement); + const self = this; + + for (let i = 0; i < this._elements.length; i++) { + const el = document.getElementById(this.IdFor(i)); + const inputEl = this._elements[i]; + { + + const v = inputEl.GetValue().data; + const index = self.value.data.indexOf(v); + if(index >= 0){ + // @ts-ignore + el.checked = true; + } + } + + + el.onchange = e => { + const v = inputEl.GetValue().data; + const index = self.value.data.indexOf(v); + // @ts-ignore + if (el.checked) { + if (index < 0) { + self.value.data.push(v); + self.value.ping(); + } + } else { + if (index >= 0) { + self.value.data.splice(index, 1); + self.value.ping(); + } + } + } + + } + + + } + + +} \ No newline at end of file diff --git a/UI/Input/RadioButton.ts b/UI/Input/RadioButton.ts index b120eff7d..415760f78 100644 --- a/UI/Input/RadioButton.ts +++ b/UI/Input/RadioButton.ts @@ -16,7 +16,6 @@ export class RadioButton extends InputElement { constructor(elements: InputElement[], selectFirstAsDefault = true) { super(undefined); - console.log("Created new radiobutton with values ", elements) this._elements = Utils.NoNull(elements); this._selectFirstAsDefault = selectFirstAsDefault; const self = this; @@ -66,15 +65,14 @@ export class RadioButton extends InputElement { InnerRender(): string { let body = ""; - let i = 0; - for (const el of this._elements) { + for (let i = 0; i < this._elements.length; i++){ + const el = this._elements[i]; const htmlElement = '' + '' + '
'; body += htmlElement; - i++; } return "
" + body + "
"; diff --git a/test.ts b/test.ts index fef2492a4..8d34719d9 100644 --- a/test.ts +++ b/test.ts @@ -1,4 +1,26 @@ import BikeCafes from "./Customizations/Layers/BikeCafes"; +import {CheckBoxes} from "./UI/Input/Checkboxes"; +import {FixedInputElement} from "./UI/Input/FixedInputElement"; +import {VariableUiElement} from "./UI/Base/VariableUIElement"; -console.log(JSON.stringify(new BikeCafes())) \ No newline at end of file +const cb = new CheckBoxes( + [ new FixedInputElement("One", 1), + new FixedInputElement("Two",2), + new FixedInputElement("Thee",3)] +) + +cb.AttachTo("maindiv"); +new VariableUiElement(cb.GetValue().map(ts => ts?.join(", "))).AttachTo("extradiv") + +window.setTimeout(() => { + cb.GetValue().setData([2,3]); +}, 2500) + +window.setTimeout(() => { + cb.GetValue().setData([2]); +}, 3000) + +window.setTimeout(() => { + cb.GetValue().setData([1, 2]); +}, 3500) \ No newline at end of file