Skip to content

Commit f7e0729

Browse files
committed
v3.0.6
1 parent 2a8f995 commit f7e0729

File tree

5 files changed

+45
-40
lines changed

5 files changed

+45
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mui-tabs",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"description": "Material UI tabs for React and React Native projects.",
55
"author": "Bilal Eren",
66
"license": "MIT",

src/native/TabView/PagerView.tsx

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type PagerViewProps<T extends Route> = PagerProps & {
2626
render: (children: React.ReactNode) => React.ReactNode
2727
// Callback to call when switching the tab
2828
// The tab switch animation is performed even if the index in state is unchanged
29-
jumpTo: (key: string, animated?: boolean) => void
29+
jumpTo: (key: T['key'], animated?: boolean) => void
3030
}) => React.ReactElement
3131
}
3232

@@ -46,53 +46,51 @@ const PagerView = <T extends Route>(props: PagerViewProps<T>) => {
4646
const { index } = state
4747

4848
const [initialPage] = React.useState(index)
49+
const position = useSharedValue(index)
4950
const pagerRef = React.useRef<RNPagerView>(null)
5051
const indexRef = React.useRef<number>(index)
5152
const stateRef = React.useRef<TabViewState<T>>(state)
5253

53-
const position = useSharedValue(index)
54-
5554
React.useEffect(() => {
5655
stateRef.current = state
5756
})
5857

58+
const setPage = React.useCallback(
59+
(page: number, animated = setPageAnimationEnabled) => {
60+
if (animated) {
61+
pagerRef.current?.setPage(page)
62+
} else {
63+
pagerRef.current?.setPageWithoutAnimation(page)
64+
position.value = page
65+
}
66+
},
67+
[position, setPageAnimationEnabled]
68+
)
69+
5970
const jumpTo = React.useCallback(
60-
(key: string, animated = setPageAnimationEnabled) => {
71+
(key: T['key'], animated?: boolean) => {
6172
const routes = stateRef.current.routes
6273
const nextIndex = routes.findIndex((route) => key === route.key)
6374

6475
if (nextIndex === -1) {
6576
if (__DEV__) {
6677
const keys = routes.map((route) => route.key)
78+
const message = [
79+
`Route matching key [${key}] not found.`,
80+
`You can provide one of the following keys: ${keys.join(', ')}.`
81+
].join('\n')
6782

68-
console.error(
69-
'[mui-tabs]',
70-
[
71-
`Route matching key [${key}] not found.`,
72-
`You can provide one of the following keys: ${keys.join(', ')}.`
73-
].join('\n')
74-
)
83+
console.error('[mui-tabs]', message)
7584
}
7685

7786
return
7887
}
7988

80-
if (animated) {
81-
pagerRef.current?.setPage(nextIndex)
82-
} else {
83-
pagerRef.current?.setPageWithoutAnimation(nextIndex)
84-
position.value = nextIndex
85-
}
89+
setPage(nextIndex, animated)
8690
},
87-
[position, setPageAnimationEnabled]
91+
[setPage]
8892
)
8993

90-
React.useEffect(() => {
91-
if (keyboardDismissMode === 'auto' && indexRef.current !== index) {
92-
Keyboard.dismiss()
93-
}
94-
}, [index, keyboardDismissMode])
95-
9694
const pageScrollHandler = usePageScrollHandler({
9795
onPageScroll: (event) => {
9896
'worklet'
@@ -102,7 +100,6 @@ const PagerView = <T extends Route>(props: PagerViewProps<T>) => {
102100

103101
const handlePageSelected = React.useCallback(
104102
(event: PagerViewOnPageSelectedEvent) => {
105-
'worklet'
106103
const { position: nextIndex } = event.nativeEvent
107104

108105
if (indexRef.current !== nextIndex) {
@@ -128,6 +125,16 @@ const PagerView = <T extends Route>(props: PagerViewProps<T>) => {
128125
[onSwipeEnd, onSwipeStart]
129126
)
130127

128+
React.useEffect(() => {
129+
if (keyboardDismissMode === 'auto') {
130+
Keyboard.dismiss()
131+
}
132+
133+
if (indexRef.current !== index) {
134+
setPage(index)
135+
}
136+
}, [index, setPage, keyboardDismissMode])
137+
131138
return children({
132139
position,
133140
jumpTo,

src/native/TabView/TabBar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
} from '../types'
1010

1111
export type TabBarProps<T extends Route> = Omit<
12-
TabsProps<string>,
12+
TabsProps<T['key']>,
1313
| 'tabs'
1414
| 'value'
1515
| 'onChange'
@@ -28,7 +28,7 @@ const TabBar = <T extends Route>(props: TabBarProps<T>) => {
2828

2929
const { index, routes } = state
3030

31-
const tabs: TabItem<string>[] = React.useMemo(
31+
const tabs: TabItem<T['key']>[] = React.useMemo(
3232
() =>
3333
routes.map(({ key, ...other }) => ({
3434
value: key,
@@ -43,7 +43,7 @@ const TabBar = <T extends Route>(props: TabBarProps<T>) => {
4343
)
4444

4545
return (
46-
<Tabs<string>
46+
<Tabs<T['key']>
4747
{...other}
4848
tabs={tabs}
4949
value={tabs[index]?.value}

src/native/Tabs/Tabs.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,13 @@ const Tabs = <Value extends TabValue = TabValue>(props: TabsProps<Value>) => {
278278

279279
if (__DEV__ && tabIndex === -1) {
280280
const values = tabs.map((tab) => tab.value).join(', ')
281+
const message = [
282+
'The `value` provided to the Tabs component is invalid.',
283+
`None of the Tabs match with "${value}" value.`,
284+
`You can provide one of the following values: ${values}.`
285+
].join('\n')
281286

282-
console.warn(
283-
'[mui-tabs]',
284-
[
285-
'The `value` provided to the Tabs component is invalid.',
286-
`None of the Tabs match with "${value}" value.`,
287-
`You can provide one of the following values: ${values}.`
288-
].join('\n')
289-
)
287+
console.warn('[mui-tabs]', message)
290288
}
291289

292290
const getTabWidth = React.useCallback(

src/native/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ export type RenderTabBar<T extends Route = Route> = (
9494

9595
export type RenderTabsIndicator = (props: TabsIndicatorProps) => React.ReactNode
9696

97-
export interface Route {
97+
export interface Route<T extends string = string> {
9898
/**
9999
* You can provide your own route key.
100100
*/
101-
key: string
101+
key: T
102102

103103
/**
104104
* The tab label.
@@ -146,7 +146,7 @@ export interface Layout {
146146
export interface SceneProps<T extends Route = Route> {
147147
route: T
148148
index: number
149-
jumpTo: (key: string, animated?: boolean) => void
149+
jumpTo: (key: T['key'], animated?: boolean) => void
150150
focused: boolean
151151
position: SharedValue<number>
152152
}

0 commit comments

Comments
 (0)