-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabs.tsx
More file actions
88 lines (82 loc) · 2.32 KB
/
Tabs.tsx
File metadata and controls
88 lines (82 loc) · 2.32 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
/*
* Copyright (c) 2019-2026 by Brockmann Consult Development team
* Permissions are hereby granted under the terms of the MIT License:
* https://opensource.org/licenses/MIT.
*/
import MuiBox from "@mui/material/Box";
import MuiTabs from "@mui/material/Tabs";
import MuiTab from "@mui/material/Tab";
import type { ComponentProps, ComponentState } from "@/index";
import type { SyntheticEvent } from "react";
import { Box } from "@/plugins/mui/Box";
import { Icon } from "./Icon";
import { isString } from "@/utils/isString";
import { isComponentState } from "@/types/state/component";
interface TabState {
type: "Tab";
label?: string;
icon?: string;
disabled?: boolean;
children?: ComponentProps[];
}
interface TabsState extends ComponentState {
value?: number;
children?: (string | TabState)[];
}
interface TabsProps extends ComponentProps, TabsState {}
export function Tabs({
type,
id,
value,
children: tabItems,
disabled,
style,
onChange,
}: TabsProps) {
const handleChange = (_event: SyntheticEvent, value: number) => {
if (id) {
onChange({
componentType: type,
id: id,
property: "value",
value: value,
});
}
};
return (
<MuiBox sx={{ width: "100%" }}>
<MuiBox sx={{ borderBottom: 1, borderColor: "divider" }}>
<MuiTabs id={id} style={style} value={value} onChange={handleChange}>
{tabItems?.map((tab, index) => {
const tabState = isComponentState(tab)
? (tab as TabState)
: undefined;
return (
<MuiTab
key={index}
label={tabState ? tabState.label : isString(tab) ? tab : ""}
icon={
tabState && tabState.icon && <Icon iconName={tabState.icon} />
}
disabled={disabled || (tabState && tabState.disabled)}
/>
);
})}
</MuiTabs>
</MuiBox>
{tabItems?.map((tab, index) => {
const tabState = isComponentState(tab) ? (tab as TabState) : undefined;
return (
value === index && (
<Box
key={index}
type={type}
onChange={onChange}
children={tabState?.children ?? undefined}
/>
)
);
})}
</MuiBox>
);
}