@wandelbots/wandelbots-js-react-components 2.40.0 → 2.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/TabBar.d.ts +2 -0
- package/dist/components/TabBar.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +84 -73
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/TabBar.tsx +35 -8
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SxProps } from "@mui/material"
|
|
2
2
|
import { Box, Tab, Tabs } from "@mui/material"
|
|
3
3
|
import { observer } from "mobx-react-lite"
|
|
4
|
-
import { useState } from "react"
|
|
4
|
+
import { useEffect, useState } from "react"
|
|
5
5
|
import { externalizeComponent } from "../externalizeComponent"
|
|
6
6
|
|
|
7
7
|
export interface TabItem {
|
|
@@ -18,6 +18,8 @@ export interface TabItem {
|
|
|
18
18
|
export interface TabBarProps {
|
|
19
19
|
/** Array of tab items to display */
|
|
20
20
|
items: TabItem[]
|
|
21
|
+
/** Controlled active tab index */
|
|
22
|
+
activeTab?: number
|
|
21
23
|
/** Default active tab index */
|
|
22
24
|
defaultActiveTab?: number
|
|
23
25
|
/** Callback when tab changes */
|
|
@@ -57,14 +59,39 @@ function TabPanel(props: TabPanelProps) {
|
|
|
57
59
|
*/
|
|
58
60
|
export const TabBar = externalizeComponent(
|
|
59
61
|
observer((props: TabBarProps) => {
|
|
60
|
-
const {
|
|
61
|
-
|
|
62
|
+
const {
|
|
63
|
+
items,
|
|
64
|
+
activeTab,
|
|
65
|
+
defaultActiveTab = 0,
|
|
66
|
+
onTabChange,
|
|
67
|
+
sx,
|
|
68
|
+
ref,
|
|
69
|
+
} = props
|
|
70
|
+
const isControlled = activeTab !== undefined
|
|
71
|
+
const [uncontrolledActiveTab, setUncontrolledActiveTab] =
|
|
72
|
+
useState(defaultActiveTab)
|
|
73
|
+
|
|
74
|
+
// Keep uncontrolled state in range when items change
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (isControlled) return
|
|
77
|
+
if (items.length === 0) return
|
|
78
|
+
if (
|
|
79
|
+
uncontrolledActiveTab < 0 ||
|
|
80
|
+
uncontrolledActiveTab > items.length - 1
|
|
81
|
+
) {
|
|
82
|
+
setUncontrolledActiveTab(0)
|
|
83
|
+
}
|
|
84
|
+
}, [items.length, isControlled, uncontrolledActiveTab])
|
|
85
|
+
|
|
86
|
+
const currentValue = isControlled ? activeTab! : uncontrolledActiveTab
|
|
62
87
|
|
|
63
88
|
const handleTabChange = (
|
|
64
89
|
_event: React.SyntheticEvent,
|
|
65
90
|
newValue: number,
|
|
66
91
|
) => {
|
|
67
|
-
|
|
92
|
+
if (!isControlled) {
|
|
93
|
+
setUncontrolledActiveTab(newValue)
|
|
94
|
+
}
|
|
68
95
|
onTabChange?.(newValue)
|
|
69
96
|
}
|
|
70
97
|
|
|
@@ -76,7 +103,7 @@ export const TabBar = externalizeComponent(
|
|
|
76
103
|
{/* Tabs */}
|
|
77
104
|
<Box sx={{ px: 0, py: 0 }}>
|
|
78
105
|
<Tabs
|
|
79
|
-
value={
|
|
106
|
+
value={currentValue}
|
|
80
107
|
onChange={handleTabChange}
|
|
81
108
|
sx={{
|
|
82
109
|
minHeight: "32px",
|
|
@@ -104,11 +131,11 @@ export const TabBar = externalizeComponent(
|
|
|
104
131
|
backgroundColor: (theme) =>
|
|
105
132
|
theme.palette.backgroundPaperElevation?.[11] || "#32344B",
|
|
106
133
|
color: "text.primary",
|
|
107
|
-
opacity:
|
|
134
|
+
opacity: currentValue === index ? 1 : 0.38,
|
|
108
135
|
fontSize: "13px",
|
|
109
136
|
transition: "all 0.2s ease-in-out",
|
|
110
137
|
"&:hover": {
|
|
111
|
-
opacity:
|
|
138
|
+
opacity: currentValue === index ? 1 : 0.6,
|
|
112
139
|
},
|
|
113
140
|
"&.Mui-selected": {
|
|
114
141
|
opacity: 1,
|
|
@@ -134,7 +161,7 @@ export const TabBar = externalizeComponent(
|
|
|
134
161
|
{/* Tab Content */}
|
|
135
162
|
<Box sx={{ flex: 1, overflow: "auto" }}>
|
|
136
163
|
{items.map((item, index) => (
|
|
137
|
-
<TabPanel key={item.id} value={
|
|
164
|
+
<TabPanel key={item.id} value={currentValue} index={index}>
|
|
138
165
|
{item.content}
|
|
139
166
|
</TabPanel>
|
|
140
167
|
))}
|