-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTabContainer.ts
More file actions
93 lines (84 loc) · 3.06 KB
/
Copy pathTabContainer.ts
File metadata and controls
93 lines (84 loc) · 3.06 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
84
85
86
87
88
89
90
91
92
93
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
*
* Copyright © 2026 Extremely Heavy Industries Inc.
*/
import {div, hbox, placeholder, vbox} from '@xh/hoist/cmp/layout';
import {TabContainerModel, TabContainerProps} from '@xh/hoist/cmp/tab';
import {TabSwitcherProps} from '@xh/hoist/cmp/tab/Types';
import {getTestId} from '@xh/hoist/utils/js';
import {getLayoutProps} from '@xh/hoist/utils/react';
import {isEmpty, isNull, isObject} from 'lodash';
import '../Tabs.scss';
import {tabSwitcher} from '../TabSwitcher';
import {dynamicTabSwitcher} from '../dynamic/DynamicTabSwitcher';
import {tab} from './Tab';
/**
* Desktop implementation of TabContainer.
* @internal
*/
export function tabContainerImpl({
model,
childContainerProps,
className,
testId,
...props
}: TabContainerProps) {
const switcherProps = getSwitcherProps(props),
layoutProps = getLayoutProps(props),
vertical = ['left', 'right'].includes(switcherProps?.orientation),
container = vertical ? hbox : vbox;
// Default flex = 'auto' if no dimensions / flex specified.
if (layoutProps.width === null && layoutProps.height === null && layoutProps.flex === null) {
layoutProps.flex = 'auto';
}
return container({
...layoutProps,
className,
testId,
item: getChildren(model, switcherProps, testId, childContainerProps)
});
}
function getSwitcherProps(tabContainerProps: TabContainerProps): TabSwitcherProps {
const {switcher} = tabContainerProps;
if (isObject(switcher)) return switcher;
return switcher === false || isNull(switcher) ? null : {orientation: 'top'};
}
function getChildren(
model: TabContainerModel,
switcher: TabSwitcherProps,
testId: string,
childContainerProps: TabContainerProps['childContainerProps']
) {
const {tabs} = model;
if (isEmpty(tabs)) {
return div({className: 'xh-tab-wrapper', item: placeholder(model.emptyText)});
}
const {activeTabId, dynamicTabSwitcherModel} = model,
switcherBefore = ['left', 'top'].includes(switcher?.orientation),
switcherAfter = ['right', 'bottom'].includes(switcher?.orientation),
switcherImpl = dynamicTabSwitcherModel ? dynamicTabSwitcher : tabSwitcher,
switcherCmp = switcher
? switcherImpl({key: 'switcher', testId: getTestId(testId, 'switcher'), ...switcher})
: null;
return [
switcherBefore ? switcherCmp : null,
...tabs.map(tabModel => {
const tabId = tabModel.id,
style = activeTabId !== tabId ? hideStyle : undefined;
return div({
className: 'xh-tab-wrapper',
style,
key: tabId,
item: tab({
childContainerProps,
model: tabModel,
testId: getTestId(testId, tabId)
})
});
}),
switcherAfter ? switcherCmp : null
];
}
const hideStyle = {display: 'none'};