MapComplete/UI/Base/TabbedComponent.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-07-29 15:05:19 +02:00
import {UIElement} from "../UIElement";
import Translations from "../i18n/Translations";
import {UIEventSource} from "../../Logic/UIEventSource";
2021-06-10 01:36:20 +02:00
import Combine from "./Combine";
2020-07-29 15:05:19 +02:00
export class TabbedComponent extends UIElement {
2021-06-10 01:36:20 +02:00
private readonly header: UIElement;
2020-07-29 15:05:19 +02:00
private content: UIElement[] = [];
2020-09-02 11:37:34 +02:00
constructor(elements: { header: UIElement | string, content: UIElement | string }[], openedTab: (UIEventSource<number> | number) = 0) {
super(typeof (openedTab) === "number" ? new UIEventSource(openedTab) : (openedTab ?? new UIEventSource<number>(0)));
2020-07-29 15:05:19 +02:00
const self = this;
2021-06-10 01:36:20 +02:00
const tabs: UIElement[] = []
2020-07-29 15:05:19 +02:00
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
2021-06-10 01:36:20 +02:00
const header = Translations.W(element.header).onClick(() => self._source.setData(i))
2020-09-12 23:15:17 +02:00
const content = Translations.W(element.content)
this.content.push(content);
2020-07-31 04:58:58 +02:00
if (!this.content[i].IsEmpty()) {
2021-06-10 01:36:20 +02:00
const tab = header.SetClass("block tab-single-header")
tabs.push(tab)
2020-07-31 04:58:58 +02:00
}
2020-07-29 15:05:19 +02:00
}
2021-06-10 01:36:20 +02:00
this.header = new Combine(tabs).SetClass("block tabs-header-bar")
}
2020-07-29 15:05:19 +02:00
2021-06-10 01:36:20 +02:00
InnerRender(): UIElement {
2020-07-29 15:05:19 +02:00
const content = this.content[this._source.data];
2021-06-10 01:36:20 +02:00
return new Combine([
this.header,
content.SetClass("tab-content"),
])
}
2020-07-29 15:05:19 +02:00
}