Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): 修复 tabs 组件选中下划线在初始化时会从最左面移到目标位置的问题 #649

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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