This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,558 additions
and
930 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "@navigation-ex/bottom-tabs", | ||
"version": "0.0.1", | ||
"main": "src/index", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@navigation-ex/core": "^0.0.1", | ||
"@navigation-ex/routers": "^0.0.1", | ||
"react-native-safe-area-view": "^0.14.6" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^16.8.24", | ||
"@types/react-native": "^0.60.2", | ||
"react": "16.8.3", | ||
"react-native": "^0.59.8" | ||
}, | ||
"peerDependencies": { | ||
"react": "*", | ||
"react-native": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 46 additions & 174 deletions
220
packages/bottom-tabs/src/navigators/createBottomTabNavigator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,181 +1,53 @@ | ||
import * as React from 'react'; | ||
import { | ||
View, | ||
StyleSheet, | ||
AccessibilityRole, | ||
AccessibilityState, | ||
} from 'react-native'; | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
import { ScreenContainer } from 'react-native-screens'; | ||
|
||
import createTabNavigator, { | ||
NavigationViewProps, | ||
} from '../utils/createTabNavigator'; | ||
import BottomTabBar from '../views/BottomTabBar'; | ||
import ResourceSavingScene from '../views/ResourceSavingScene'; | ||
useNavigationBuilder, | ||
createNavigator, | ||
DefaultNavigatorOptions, | ||
} from '@navigation-ex/core'; | ||
import { | ||
TabRouter, | ||
TabRouterOptions, | ||
TabNavigationState, | ||
} from '@navigation-ex/routers'; | ||
import BottomTabView from '../views/BottomTabView'; | ||
import { | ||
NavigationProp, | ||
Route, | ||
SceneDescriptor, | ||
NavigationBottomTabOptions, | ||
BottomTabBarOptions, | ||
BottomTabNavigationConfig, | ||
BottomTabNavigationOptions, | ||
} from '../types'; | ||
|
||
type Props = NavigationViewProps & { | ||
getAccessibilityRole: (props: { | ||
route: Route; | ||
}) => AccessibilityRole | undefined; | ||
getAccessibilityStates: (props: { | ||
route: Route; | ||
focused: boolean; | ||
}) => AccessibilityState[]; | ||
lazy?: boolean; | ||
tabBarComponent?: React.ComponentType<any>; | ||
tabBarOptions?: BottomTabBarOptions; | ||
navigation: NavigationProp; | ||
descriptors: { [key: string]: SceneDescriptor<NavigationBottomTabOptions> }; | ||
screenProps?: unknown; | ||
}; | ||
|
||
type State = { | ||
loaded: number[]; | ||
}; | ||
|
||
class TabNavigationView extends React.PureComponent<Props, State> { | ||
static defaultProps = { | ||
lazy: true, | ||
getAccessibilityRole: (): AccessibilityRole => 'button', | ||
getAccessibilityStates: ({ | ||
focused, | ||
}: { | ||
focused: boolean; | ||
}): AccessibilityState[] => (focused ? ['selected'] : []), | ||
}; | ||
|
||
static getDerivedStateFromProps(nextProps: Props, prevState: State) { | ||
const { index } = nextProps.navigation.state; | ||
|
||
return { | ||
// Set the current tab to be loaded if it was not loaded before | ||
loaded: prevState.loaded.includes(index) | ||
? prevState.loaded | ||
: [...prevState.loaded, index], | ||
}; | ||
} | ||
|
||
state = { | ||
loaded: [this.props.navigation.state.index], | ||
}; | ||
|
||
_getButtonComponent = ({ route }: { route: Route }) => { | ||
const { descriptors } = this.props; | ||
const descriptor = descriptors[route.key]; | ||
const options = descriptor.options; | ||
|
||
if (options.tabBarButtonComponent) { | ||
return options.tabBarButtonComponent; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
_renderTabBar = () => { | ||
const { | ||
tabBarComponent: TabBarComponent = BottomTabBar, | ||
tabBarOptions, | ||
navigation, | ||
screenProps, | ||
getLabelText, | ||
getAccessibilityLabel, | ||
getAccessibilityRole, | ||
getAccessibilityStates, | ||
getTestID, | ||
renderIcon, | ||
onTabPress, | ||
onTabLongPress, | ||
} = this.props; | ||
|
||
const { descriptors } = this.props; | ||
const { state } = this.props.navigation; | ||
const route = state.routes[state.index]; | ||
const descriptor = descriptors[route.key]; | ||
const options = descriptor.options; | ||
|
||
if (options.tabBarVisible === false) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<TabBarComponent | ||
{...tabBarOptions} | ||
jumpTo={this._jumpTo} | ||
navigation={navigation} | ||
screenProps={screenProps} | ||
onTabPress={onTabPress} | ||
onTabLongPress={onTabLongPress} | ||
getLabelText={getLabelText} | ||
getButtonComponent={this._getButtonComponent} | ||
getAccessibilityLabel={getAccessibilityLabel} | ||
getAccessibilityRole={getAccessibilityRole} | ||
getAccessibilityStates={getAccessibilityStates} | ||
getTestID={getTestID} | ||
renderIcon={renderIcon} | ||
/> | ||
); | ||
}; | ||
|
||
_jumpTo = (key: string) => { | ||
const { navigation, onIndexChange } = this.props; | ||
|
||
const index = navigation.state.routes.findIndex(route => route.key === key); | ||
|
||
onIndexChange(index); | ||
}; | ||
|
||
render() { | ||
const { navigation, renderScene, lazy } = this.props; | ||
const { routes } = navigation.state; | ||
const { loaded } = this.state; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<ScreenContainer style={styles.pages}> | ||
{routes.map((route, index) => { | ||
if (lazy && !loaded.includes(index)) { | ||
// Don't render a screen if we've never navigated to it | ||
return null; | ||
} | ||
|
||
const isFocused = navigation.state.index === index; | ||
|
||
return ( | ||
<ResourceSavingScene | ||
key={route.key} | ||
style={StyleSheet.absoluteFill} | ||
isVisible={isFocused} | ||
> | ||
{renderScene({ route })} | ||
</ResourceSavingScene> | ||
); | ||
})} | ||
</ScreenContainer> | ||
{this._renderTabBar()} | ||
</View> | ||
); | ||
} | ||
type Props = DefaultNavigatorOptions<BottomTabNavigationOptions> & | ||
TabRouterOptions & | ||
BottomTabNavigationConfig; | ||
|
||
function BottomTabNavigator({ | ||
initialRouteName, | ||
backBehavior, | ||
children, | ||
screenOptions, | ||
...rest | ||
}: Props) { | ||
const { state, descriptors, navigation } = useNavigationBuilder< | ||
TabNavigationState, | ||
BottomTabNavigationOptions, | ||
TabRouterOptions | ||
>(TabRouter, { | ||
initialRouteName, | ||
backBehavior, | ||
children, | ||
screenOptions, | ||
}); | ||
|
||
return ( | ||
<BottomTabView | ||
{...rest} | ||
state={state} | ||
navigation={navigation} | ||
descriptors={descriptors} | ||
/> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
overflow: 'hidden', | ||
}, | ||
pages: { | ||
flex: 1, | ||
}, | ||
}); | ||
|
||
export default createTabNavigator<NavigationBottomTabOptions, Props>( | ||
TabNavigationView | ||
); | ||
export default createNavigator< | ||
BottomTabNavigationOptions, | ||
typeof BottomTabNavigator | ||
>(BottomTabNavigator); |
Oops, something went wrong.