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

Allow to custom MaterialTabBar indicator #341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [Default Tab Bar](#default-tab-bar)
- [MaterialTabBar](#materialtabbar)
- [MaterialTabItem](#materialtabitem)
- [IndicatorComponent](#indicatorcomponent)
- [Known Issues](#known-issues)
- [Android FlatList Pull to Refresh](#android-flatlist-pull-to-refresh)
- [iOS FlatList StickyHeaderIndices](#ios-flatlist-stickyheaderindices)
Expand Down Expand Up @@ -352,6 +353,38 @@ Any additional props are passed to the pressable component.
|`style`|`StyleProp<ViewStyle>`|Either view styles or a function that receives a boolean reflecting whether the component is currently pressed and returns view styles.|


### IndicatorComponent
To custom `MaterialTabBar` indicator we can use `IndicatorComponent` as a prop of `MaterialTabBar`.
```tsx
const CustomTabbar = useCallback(
(props) => {
return (
<MaterialTabBar
{...props}
TabItemComponent={CustomTabItem}
IndicatorComponent={CustomTabIndicator}
/>
)
},
[]
)

return (
<Tabs.Container
renderTabBar={CustomTabbar}
>
...
</Tabs.Container>
)
```
#### Props

|name|type|description|
|:----:|:----:|:----:|
|`indexDecimal`|``SharedValue<number>``||
|`itemsLayout`|`ItemLayout[]`|ItemLayout = { width: number, x: number }|
|`style`|`StyleProp<ViewStyle>`||
|`fadeIn`|`boolean \| undefined`||

# Known Issues

Expand Down
3 changes: 2 additions & 1 deletion src/MaterialTabBar/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const MaterialTabBar = <T extends TabName = TabName>({
indicatorStyle,
index,
TabItemComponent = MaterialTabItem,
IndicatorComponent = Indicator,
getLabelText = (name) => String(name).toUpperCase(),
onTabPress,
style,
Expand Down Expand Up @@ -222,7 +223,7 @@ const MaterialTabBar = <T extends TabName = TabName>({
)
})}
{itemsLayout.length === nTabs && (
<Indicator
<IndicatorComponent
indexDecimal={indexDecimal}
itemsLayout={itemsLayout}
fadeIn={scrollEnabled}
Expand Down
6 changes: 5 additions & 1 deletion src/MaterialTabBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { MaterialTabBar, TABBAR_HEIGHT } from './TabBar'
export { MaterialTabItem } from './TabItem'
export type { MaterialTabBarProps, MaterialTabItemProps } from './types'
export type {
MaterialTabBarProps,
MaterialTabItemProps,
IndicatorProps,
} from './types'
4 changes: 4 additions & 0 deletions src/MaterialTabBar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export type MaterialTabBarProps<N extends TabName> = TabBarProps<N> & {
* React component to render as tab bar item
*/
TabItemComponent?: (props: MaterialTabItemProps<N>) => React.ReactElement
/**
* React component to render as indicator
*/
IndicatorComponent?: React.FC<IndicatorProps>
/**
* Function to compute the tab item label text
*/
Expand Down
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Container } from './Container'
import { FlashList } from './FlashList'
import { FlatList } from './FlatList'
import { Lazy } from './Lazy'
import { MaterialTabBarProps, MaterialTabItemProps } from './MaterialTabBar'
import {
MaterialTabBarProps,
MaterialTabItemProps,
IndicatorProps,
} from './MaterialTabBar'
import { ScrollView } from './ScrollView'
import { SectionList } from './SectionList'
import { Tab } from './Tab'
Expand All @@ -28,6 +32,7 @@ export type {
OnTabChangeCallback,
TabItemProps,
TabProps,
IndicatorProps,
}

export const Tabs = {
Expand Down