Skip to content

Commit

Permalink
fix: prevent typescript error for conditional tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed May 25, 2022
1 parent c79f361 commit 749c27b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 2 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import AndroidSharedPullToRefresh from './AndroidSharedPullToRefresh'
import AnimatedHeader from './AnimatedHeader'
import CenteredEmptyList from './CenteredEmptyList'
import ConditionalTabs from './ConditionalTabs'
import Default from './Default'
import DefaultCustomLabels from './DefaultCustomLabels'
import DynamicTabs from './DynamicTabs'
Expand Down Expand Up @@ -49,6 +50,7 @@ const EXAMPLE_COMPONENTS: ExampleComponentType[] = [
Ref,
OnTabChange,
DynamicTabs,
ConditionalTabs,
MinHeaderHeight,
AnimatedHeader,
AndroidSharedPullToRefresh,
Expand Down
52 changes: 52 additions & 0 deletions example/src/ConditionalTabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState } from 'react'
import { View, Text, StyleSheet } from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler'

import { TabBarProps } from '../../src/types'
import ExampleComponent from './Shared/ExampleComponent'
import { ExampleComponentType } from './types'

const title = 'Conditional Tabs'

const HEADER_HEIGHT = 200

const NewHeader: React.FC<
TabBarProps & {
setHideArticleTab: React.Dispatch<React.SetStateAction<boolean>>
}
> = (props) => {
return (
<View pointerEvents="box-none" style={styles.headerView}>
<TouchableOpacity
onPress={() => props.setHideArticleTab((prev) => !prev)}
>
<Text>Toggle articles tab</Text>
</TouchableOpacity>
</View>
)
}

const DefaultExample: ExampleComponentType = () => {
const [hideArticleTab, setHideArticleTab] = useState(false)
return (
<ExampleComponent
renderHeader={(props) => (
<NewHeader setHideArticleTab={setHideArticleTab} {...props} />
)}
headerHeight={HEADER_HEIGHT}
hideArticleTab={hideArticleTab}
/>
)
}

DefaultExample.title = title

export default DefaultExample

const styles = StyleSheet.create({
headerView: {
height: HEADER_HEIGHT,
alignItems: 'center',
justifyContent: 'center',
},
})
9 changes: 6 additions & 3 deletions example/src/Shared/ExampleComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import SectionContacts from './SectionContacts'

type Props = {
emptyContacts?: boolean
hideArticleTab?: boolean
} & Partial<CollapsibleProps>

const Example = React.forwardRef<CollapsibleRef, Props>(
({ emptyContacts, ...props }, ref) => {
return (
<Tabs.Container ref={ref} headerHeight={HEADER_HEIGHT} {...props}>
<Tabs.Tab name="article" label="Article">
<Article />
</Tabs.Tab>
{props.hideArticleTab ? (
<Tabs.Tab name="article" label="Article">
<Article />
</Tabs.Tab>
) : null}
<Tabs.Tab name="albums" label="Albums">
<Albums />
</Tabs.Tab>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function useTabProps<T extends TabName>(
const tabOptions: TabsWithProps<T> = new Map()
if (children) {
Children.forEach(children, (element, index) => {
if (!element || !element.props) return
if (!element) return

if (element.type !== tabType)
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export type OnTabChangeCallback<T extends TabName = TabName> = (
data: IndexChangeEventData<T>
) => void

export type TabReactElement<
T extends TabName = TabName
> = React.ReactElement<TabProps<T> | null>
export type TabReactElement<T extends TabName = TabName> = React.ReactElement<
TabProps<T>
> | null

export type CollapsibleProps = {
initialTabName?: TabName
Expand Down

0 comments on commit 749c27b

Please sign in to comment.