|
| 1 | +import { |
| 2 | + QTabWidget, |
| 3 | + QTabWidgetSignals, |
| 4 | + TabPosition, |
| 5 | + QIcon, |
| 6 | + NodeWidget |
| 7 | +} from "@nodegui/nodegui"; |
| 8 | +import { ViewProps, setViewProps } from "../View/RNView"; |
| 9 | +import { RNComponent } from "../config"; |
| 10 | +import { RNTabItem, setTabItemProps } from "../TabItem/RNTabItem"; |
| 11 | + |
| 12 | +export interface TabProps extends ViewProps<QTabWidgetSignals> { |
| 13 | + tabPosition?: TabPosition; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * @ignore |
| 18 | + */ |
| 19 | +export const setTabProps = ( |
| 20 | + widget: RNTab, |
| 21 | + newProps: TabProps, |
| 22 | + oldProps: TabProps |
| 23 | +) => { |
| 24 | + const setter: TabProps = { |
| 25 | + set tabPosition(value: TabPosition) { |
| 26 | + widget.setTabPosition(value); |
| 27 | + } |
| 28 | + }; |
| 29 | + Object.assign(setter, newProps); |
| 30 | + setViewProps(widget, newProps, oldProps); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * @ignore |
| 35 | + */ |
| 36 | +export class RNTab extends QTabWidget implements RNComponent { |
| 37 | + setProps(newProps: TabProps, oldProps: TabProps): void { |
| 38 | + setTabProps(this, newProps, oldProps); |
| 39 | + } |
| 40 | + |
| 41 | + appendInitialChild(tabItem: RNTabItem): void { |
| 42 | + if (!(tabItem instanceof RNTabItem)) { |
| 43 | + throw new Error("Children of tab should be of type TabItem"); |
| 44 | + } |
| 45 | + |
| 46 | + if (tabItem.actualTabWidget) { |
| 47 | + this.addTab(tabItem.actualTabWidget, new QIcon(), ""); |
| 48 | + tabItem.parentTab = this; |
| 49 | + setTabItemProps(tabItem, this, tabItem.initialProps, {}); |
| 50 | + } |
| 51 | + } |
| 52 | + appendChild(child: RNTabItem): void { |
| 53 | + this.appendInitialChild(child); |
| 54 | + } |
| 55 | + insertBefore(child: RNTabItem, beforeChild: RNTabItem): void { |
| 56 | + if (!(child instanceof RNTabItem)) { |
| 57 | + throw new Error("Children of tab should be of type TabItem"); |
| 58 | + } |
| 59 | + // uncomment below code after new release of nodegui containing insertTab |
| 60 | + // const index = this.indexOf(beforeChild.actualTabWidget as NodeWidget<any>); |
| 61 | + // this.insertTab(index, child.actualTabWidget, new QIcon(), ""); |
| 62 | + // child.parentTab = this; |
| 63 | + // setTabItemProps(child, this, child.initialProps, {}); |
| 64 | + } |
| 65 | + removeChild(child: RNTabItem): void { |
| 66 | + const childIndex = this.indexOf(child.actualTabWidget as NodeWidget<any>); |
| 67 | + this.removeTab(childIndex); |
| 68 | + } |
| 69 | + static tagName = "tabwidget"; |
| 70 | +} |
0 commit comments