Skip to content

Commit

Permalink
fix(tabs): 修复 tabs 组件选中下划线在初始化时会从最左面移到目标位置的问题 (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowings-zy authored May 31, 2023
1 parent dfab33f commit f4c8318
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/react-vant/src/components/tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ const Tabs = forwardRef<TabsInstance, TabsProps>((props, ref) => {

// 下划线偏移量
const [lineTranslateLeft, setLineTranslateLeft] = useState<number>(0)
const [showLine, setShowLine] = useState<boolean>(false)
useUpdateEffect(() => {
const hidden = isHidden(root.current)
const title = titleRefs?.[index]
if (!title || hidden || props.type !== 'line') {
return
}
setShowLine(true)
setLineTranslateLeft(title.offsetLeft + title.offsetWidth / 2)
}, [root.current, titleRefs, props.type, index])

Expand All @@ -123,6 +125,7 @@ const Tabs = forwardRef<TabsInstance, TabsProps>((props, ref) => {
width: addUnit(lineWidth),
backgroundColor: color,
transitionDuration: `${immediateRef.current ? 0 : props.duration}ms`,
display: showLine ? 'inherit' : 'none',
} as React.CSSProperties

if (lineTranslateLeft) {
Expand All @@ -141,6 +144,7 @@ const Tabs = forwardRef<TabsInstance, TabsProps>((props, ref) => {
props.lineWidth,
lineTranslateLeft,
immediateRef.current,
showLine,
])

const getAvailableTab = (targetIndex: number) => {
Expand Down

0 comments on commit f4c8318

Please sign in to comment.