-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathTab.tsx
More file actions
executable file
·85 lines (73 loc) · 2.67 KB
/
Tab.tsx
File metadata and controls
executable file
·85 lines (73 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as React from "react";
import { Fragment } from "react";
import { Actions } from "../model/Actions";
import { TabNode } from "../model/TabNode";
import { TabSetNode } from "../model/TabSetNode";
import { CLASSES } from "../Types";
import { ILayoutCallbacks } from "./Layout";
import { ErrorBoundary } from "./ErrorBoundary";
import { I18nLabel } from "../I18nLabel";
import { BorderNode } from "../model/BorderNode";
import { hideElement } from "./Utils";
/** @internal */
export interface ITabProps {
layout: ILayoutCallbacks;
selected: boolean;
node: TabNode;
factory: (node: TabNode) => React.ReactNode;
path: string;
}
/** @internal */
export const Tab = (props: ITabProps) => {
const { layout, selected, node, factory, path } = props;
const [renderComponent, setRenderComponent] = React.useState<boolean>(!props.node.isEnableRenderOnDemand() || props.selected);
React.useLayoutEffect(() => {
if (!renderComponent && selected) {
// load on demand
// console.log("load on demand: " + node.getName());
setRenderComponent(true);
}
});
const onMouseDown = () => {
const parent = node.getParent() as TabSetNode;
if (parent.getType() === TabSetNode.TYPE) {
if (!parent.isActive()) {
layout.doAction(Actions.setActiveTabset(parent.getId()));
}
}
};
const cm = layout.getClassName;
const useVisibility = node.getModel().isUseVisibility();
const parentNode = node.getParent() as TabSetNode | BorderNode;
const style: Record<string, any> = node._styleWithPosition();
if (!selected) {
hideElement(style, useVisibility);
}
if (parentNode instanceof TabSetNode) {
if (node.getModel().getMaximizedTabset() !== undefined && !parentNode.isMaximized()) {
hideElement(style, useVisibility);
}
}
let child;
if (renderComponent) {
child = factory(node);
}
let className = cm(CLASSES.FLEXLAYOUT__TAB);
if (node.getClassName()) className += " " + node.getClassName()
if (parentNode instanceof BorderNode) {
className += " " + cm(CLASSES.FLEXLAYOUT__TAB_BORDER);
className += " " + cm(CLASSES.FLEXLAYOUT__TAB_BORDER_ + parentNode.getLocation().getName());
}
return (
<div
className={className}
data-layout-path={path}
onMouseDown={onMouseDown}
onTouchStart={onMouseDown}
style={style}>
<ErrorBoundary message={props.layout.i18nName(I18nLabel.Error_rendering_component)}>
<Fragment>{child}</Fragment>
</ErrorBoundary>
</div>
);
};