mapcomplete/UI/Base/TabbedComponent.ts

42 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-07-29 15:05:19 +02:00
import Translations from "../i18n/Translations";
import {UIEventSource} from "../../Logic/UIEventSource";
2021-06-10 01:36:20 +02:00
import Combine from "./Combine";
2021-06-12 02:58:32 +02:00
import BaseUIElement from "../BaseUIElement";
import {VariableUiElement} from "./VariableUIElement";
2020-07-29 15:05:19 +02:00
2021-06-12 02:58:32 +02:00
export class TabbedComponent extends Combine {
2020-07-29 15:05:19 +02:00
2021-06-12 02:58:32 +02:00
constructor(elements: { header: BaseUIElement | string, content: BaseUIElement | string }[], openedTab: (UIEventSource<number> | number) = 0) {
2021-06-10 01:36:20 +02:00
2021-06-12 02:58:32 +02:00
const openedTabSrc = typeof (openedTab) === "number" ? new UIEventSource(openedTab) : (openedTab ?? new UIEventSource<number>(0))
const tabs: BaseUIElement[] = []
const contentElements: BaseUIElement[] = [];
2020-07-29 15:05:19 +02:00
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
2021-06-12 02:58:32 +02:00
const header = Translations.W(element.header).onClick(() => openedTabSrc.setData(i))
2021-06-14 19:21:33 +02:00
openedTabSrc.addCallbackAndRun(selected => {
if(selected === i){
header.SetClass("tab-active")
header.RemoveClass("tab-non-active")
}else{
header.SetClass("tab-non-active")
header.RemoveClass("tab-active")
}
})
2020-09-12 23:15:17 +02:00
const content = Translations.W(element.content)
2021-06-14 19:21:33 +02:00
content.SetClass("relative p-4 w-full inline-block")
2021-06-12 02:58:32 +02:00
contentElements.push(content);
const tab = header.SetClass("block tab-single-header")
tabs.push(tab)
2020-07-29 15:05:19 +02:00
}
2021-06-12 02:58:32 +02:00
const header = new Combine(tabs).SetClass("block tabs-header-bar")
const actualContent = new VariableUiElement(
openedTabSrc.map(i => contentElements[i])
)
super([header, actualContent])
2020-07-29 15:05:19 +02:00
}
2020-07-29 15:05:19 +02:00
}