Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
GeekLearnMathWeapp
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
GeekLearnMathWeapp
Commits
b9057fa7
Commit
b9057fa7
authored
Mar 30, 2023
by
m
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: tabs 组件增加动画
parent
b5b7e712
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
16 deletions
+29
-16
index.module.scss
src/components/Tabs/index.module.scss
+11
-7
index.tsx
src/components/Tabs/index.tsx
+18
-9
No files found.
src/components/Tabs/index.module.scss
View file @
b9057fa7
...
...
@@ -26,16 +26,20 @@
}
.tabPanes
{
display
:
flex
;
flex-direction
:
column
;
padding
:
12px
16px
;
margin-top
:
16px
;
}
position
:
relative
;
overflow
:
hidden
;
min-height
:
400px
;
/* 根据需要设置最小高度 */
.tabPane
{
display
:
none
;
.tabPane
{
position
:
absolute
;
top
:
0
;
left
:
0
;
width
:
100%
;
transition
:
transform
0
.3s
ease-in-out
;
&
.active
{
display
:
block
;
}
}
}
src/components/Tabs/index.tsx
View file @
b9057fa7
...
...
@@ -6,25 +6,26 @@ import styles from './index.module.scss'
export
interface
TabItem
{
label
:
string
|
ReactNode
;
children
:
ReactNode
;
disabled
?:
boolean
;
hidden
?:
boolean
;
keepAlive
?:
boolean
;
children
:
ReactNode
;
}
interface
TabsProps
{
items
:
TabItem
[];
initialIndex
?:
number
;
tabBarStyle
?:
CSSProperties
;
tabBarGutter
?:
number
;
tabBarStyle
?:
CSSProperties
;
tabPanesStyle
?:
CSSProperties
;
onChange
?:
(
index
:
number
)
=>
void
;
}
const
Tabs
:
React
.
FC
<
TabsProps
>
=
({
items
,
initialIndex
=
0
,
tabBarStyle
=
{},
tabBarGutter
=
16
,
tabBarStyle
,
tabPanesStyle
,
onChange
,
})
=>
{
const
[
activeIndex
,
setActiveIndex
]
=
useState
(
initialIndex
)
...
...
@@ -36,12 +37,15 @@ const Tabs: React.FC<TabsProps> = ({
}
},
[
items
,
activeIndex
])
const
handleClick
=
useCallback
((
index
:
number
)
=>
{
const
handleClick
=
useCallback
(
(
index
:
number
)
=>
{
if
(
!
items
[
index
].
disabled
&&
index
!==
activeIndex
)
{
setActiveIndex
(
index
)
onChange
?.(
index
)
}
},
[
items
,
activeIndex
,
onChange
])
},
[
items
,
activeIndex
,
onChange
]
)
const
renderTabs
=
()
=>
{
return
items
.
map
((
tab
,
index
)
=>
{
...
...
@@ -67,7 +71,7 @@ const Tabs: React.FC<TabsProps> = ({
const
renderTabPanes
=
()
=>
{
return
items
.
map
((
tab
,
index
)
=>
{
if
(
tab
.
hidden
&&
!
tab
.
keepAlive
)
return
null
if
(
tab
.
hidden
)
return
null
const
isActive
=
activeIndex
===
index
const
isHidden
=
tab
.
hidden
||
!
isActive
...
...
@@ -76,8 +80,13 @@ const Tabs: React.FC<TabsProps> = ({
[
styles
.
active
]:
isActive
,
})
const
tabPaneStyle
=
{
transform
:
`translateX(
${(
index
-
activeIndex
)
*
100
}
%)`
,
transition
:
'transform 0.3s ease-in-out'
,
}
return
(
<
View
key=
{
index
}
className=
{
cls
}
hidden=
{
isHidden
}
>
<
View
key=
{
index
}
className=
{
cls
}
hidden=
{
isHidden
}
style=
{
tabPaneStyle
}
>
{
tab
.
children
}
</
View
>
)
...
...
@@ -89,7 +98,7 @@ const Tabs: React.FC<TabsProps> = ({
<
View
className=
{
styles
.
tabBar
}
style=
{
tabBarStyle
}
>
{
renderTabs
()
}
</
View
>
<
View
className=
{
styles
.
tabPanes
}
>
<
View
className=
{
styles
.
tabPanes
}
style=
{
tabPanesStyle
}
>
{
renderTabPanes
()
}
</
View
>
</
View
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment