Skip to content

Commit 0e955ee

Browse files
fix: add validation tabs
1 parent 82bc457 commit 0e955ee

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

src/components/Tabs/TabGroup.tsx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,44 +120,46 @@ function TabGroup({
120120
},
121121
[orientation, variant, refContainer, dashRect]
122122
)
123-
123+
console.log(refContainer.current?.children)
124124
const onClick = useCallback(
125-
(e: MouseEvent<HTMLButtonElement>) => {
126-
const nodes = Array.from(
127-
refContainer.current?.children ?? []
128-
) as HTMLElement[]
129-
const newValue = nodes.indexOf(e.currentTarget as unknown as HTMLElement) // 👈 en vez de e.target
125+
(event: MouseEvent<HTMLButtonElement>) => {
126+
const nodes = Array.prototype.slice.call(refContainer.current?.children)
127+
const newValue = nodes.indexOf(event.target) ?? 0
130128
handleChangeIndicator(newValue)
131-
onChange?.(newValue)
129+
onChange && onChange(newValue)
130+
131+
const tabElement = nodes[
132+
newValue !== 0 ? newValue + 1 : newValue
133+
] as HTMLElement
134+
135+
if (tabElement)
136+
tabElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
132137
},
133-
[handleChangeIndicator, onChange]
138+
[refContainer, handleChangeIndicator, onChange]
134139
)
135140

136141
const childrenWithProps = useCallback((): ReactNode => {
137-
const visibles = React.Children.toArray(children).filter(
138-
(children): children is React.ReactElement =>
139-
React.isValidElement(children) && !children.props?.hidden
140-
)
141-
142-
return visibles.map((child, visibleIndex) =>
143-
React.cloneElement(child, {
144-
index: visibleIndex,
145-
'data-tab-index': visibleIndex,
146-
value,
147-
onClick,
148-
textColor,
149-
textDisabledColor,
150-
variant,
151-
disabledText,
152-
tabWidth,
153-
tabMinWidth,
154-
className: childClassName,
155-
isVertical: orientation === 'vertical',
156-
...child.props
157-
})
158-
)
142+
return React.Children.map(children, (child, index): ReactNode => {
143+
if (React.isValidElement(child)) {
144+
return React.cloneElement(child as React.ReactElement, {
145+
index,
146+
value,
147+
onClick,
148+
textColor,
149+
textDisabledColor,
150+
variant,
151+
disabledText,
152+
tabWidth,
153+
tabMinWidth,
154+
className: childClassName,
155+
isVertical: orientation === 'vertical',
156+
...child.props
157+
})
158+
}
159+
if ((child as React.ReactElement)?.props?.hidden) return null
160+
return child
161+
})
159162
}, [
160-
children,
161163
value,
162164
onClick,
163165
disabledText,
@@ -167,7 +169,7 @@ function TabGroup({
167169
variant,
168170
tabWidth,
169171
tabMinWidth,
170-
orientation
172+
children
171173
])
172174

173175
useEffect(() => {

src/components/TopPage/TopPage.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) DD360 and its affiliates.
33
*/
44

5-
import React, { useCallback } from 'react'
5+
import React, { useCallback, useMemo } from 'react'
66
import { composeClasses } from 'lib/classes'
77
import Breadcrumbs from 'components/Breadcrumbs'
88
import type { BreadcrumbsProps } from 'components/Breadcrumbs'
@@ -109,6 +109,10 @@ const TopPage = ({
109109
return 'text-blue-600'
110110
}, [actionIcon])
111111

112+
const updateTabs = useMemo(() => {
113+
return tabs?.items.filter((tab) => !tab.hidden)
114+
}, [tabs])
115+
112116
return (
113117
<div className={classNameHeader ?? 'sticky top-0 bg-white z-40 px-5'}>
114118
{(optionsBreadcrumbs || lastUpdate) && (
@@ -203,20 +207,17 @@ const TopPage = ({
203207
)}
204208
</Flex>
205209
<div className="-mx-5 mt-1">
206-
{tabs ? (
207-
<TabGroup value={tabs.value} onChange={tabs.setValue}>
208-
{tabs.items.map((tab, index) => {
209-
if (tab.hidden) return null
210-
return (
211-
<Tab
212-
key={index}
213-
label={tab.label}
214-
disabled={tab.disabled}
215-
hidden={tab.hidden}
216-
toolTipProps={tab.toolTipProps}
217-
/>
218-
)
219-
})}
210+
{updateTabs ? (
211+
<TabGroup value={tabs?.value} onChange={tabs?.setValue}>
212+
{updateTabs?.map((tab, index) => (
213+
<Tab
214+
key={index}
215+
label={tab.label}
216+
disabled={tab.disabled}
217+
hidden={tab.hidden}
218+
toolTipProps={tab.toolTipProps}
219+
/>
220+
))}
220221
</TabGroup>
221222
) : (
222223
<Divider className="mt-3" light />

0 commit comments

Comments
 (0)