Skip to content

Commit 82bc457

Browse files
fix: validation was added
1 parent 3fc5a04 commit 82bc457

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

src/components/Tabs/TabGroup.tsx

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -122,44 +122,42 @@ function TabGroup({
122122
)
123123

124124
const onClick = useCallback(
125-
(event: MouseEvent<HTMLButtonElement>) => {
126-
const nodes = Array.prototype.slice.call(refContainer.current?.children)
127-
const newValue = nodes.indexOf(event.target) ?? 0
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
128130
handleChangeIndicator(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' })
131+
onChange?.(newValue)
137132
},
138-
[refContainer, handleChangeIndicator, onChange]
133+
[handleChangeIndicator, onChange]
139134
)
140135

141136
const childrenWithProps = useCallback((): ReactNode => {
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-
})
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+
)
162159
}, [
160+
children,
163161
value,
164162
onClick,
165163
disabledText,
@@ -169,7 +167,7 @@ function TabGroup({
169167
variant,
170168
tabWidth,
171169
tabMinWidth,
172-
children
170+
orientation
173171
])
174172

175173
useEffect(() => {

0 commit comments

Comments
 (0)