Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
feat: add a simple stack and material tabs integration (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 authored and osdnk committed Aug 3, 2019
1 parent ce7d163 commit e0bee10
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 184 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"extends": "satya164",
"settings": {
"react": { "version": "16" },
"import/core-modules": ["@navigation-ex/core", "@navigation-ex/routers"]
"import/core-modules": [
"@navigation-ex/core",
"@navigation-ex/routers",
"@navigation-ex/stack",
"@navigation-ex/material-top-tabs"
]
},
"env": { "browser": true }
}
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/expo",
"**/expo/**",
"**/react-native",
"**/react-native/**"
]
},
"publishConfig": {
Expand Down
2 changes: 2 additions & 0 deletions packages/example/App.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import 'react-native-gesture-handler';

export { default } from './src/index';
4 changes: 4 additions & 0 deletions packages/example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = {
'@babel/runtime',
'react',
'react-native',
'react-native-gesture-handler',
'react-native-reanimated',
'react-native-safe-area-view',
'react-native-tab-view',
'shortid',
],
},
Expand Down
6 changes: 5 additions & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"react": "16.8.3",
"react-dom": "^16.8.3",
"react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
"react-native-gesture-handler": "~1.3.0",
"react-native-paper": "3.0.0-alpha.3",
"react-native-reanimated": "~1.1.0",
"react-native-tab-view": "2.7.1",
"react-native-web": "^0.11.4",
"scheduler": "^0.14.0",
"shortid": "^2.2.14"
Expand All @@ -36,6 +39,7 @@
},
"resolutions": {
"react": "16.8.3",
"react-dom": "^16.8.3"
"react-dom": "^16.8.3",
"react-native-safe-area-view": "0.14.6"
}
}
105 changes: 0 additions & 105 deletions packages/example/src/StackNavigator.tsx

This file was deleted.

26 changes: 19 additions & 7 deletions packages/example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import {
InitialState,
useFocusEffect,
} from '@navigation-ex/core';
import createStackNavigator, { StackNavigationProp } from './StackNavigator';
import createTabNavigator, { TabNavigationProp } from './TabNavigator';
import createStackNavigator, {
StackNavigationProp,
} from '@navigation-ex/stack';
import createMaterialTopTabNavigator, {
MaterialTopTabNavigationProp,
} from '@navigation-ex/material-top-tabs';

type StackParamList = {
first: { author: string };
Expand All @@ -34,7 +38,7 @@ YellowBox.ignoreWarnings(['Require cycle:', 'Warning: Async Storage']);

const Stack = createStackNavigator<StackParamList>();

const Tab = createTabNavigator<TabParamList>();
const Tab = createMaterialTopTabNavigator<TabParamList>();

const First = ({
navigation,
Expand Down Expand Up @@ -111,7 +115,7 @@ const Fourth = ({
navigation,
}: {
navigation: CompositeNavigationProp<
TabNavigationProp<TabParamList, 'fourth'>,
MaterialTopTabNavigationProp<TabParamList, 'fourth'>,
StackNavigationProp<StackParamList>
>;
}) => (
Expand All @@ -129,7 +133,7 @@ const Fifth = ({
navigation,
}: {
navigation: CompositeNavigationProp<
TabNavigationProp<TabParamList, 'fifth'>,
MaterialTopTabNavigationProp<TabParamList, 'fifth'>,
StackNavigationProp<StackParamList>
>;
}) => (
Expand Down Expand Up @@ -192,8 +196,16 @@ export default function App() {
<Stack.Screen name="third" options={{ title: 'Baz' }}>
{() => (
<Tab.Navigator initialRouteName="fifth">
<Tab.Screen name="fourth" component={Fourth} />
<Tab.Screen name="fifth" component={Fifth} />
<Tab.Screen
name="fourth"
component={Fourth}
options={{ title: 'This' }}
/>
<Tab.Screen
name="fifth"
component={Fifth}
options={{ title: 'That' }}
/>
</Tab.Navigator>
)}
</Stack.Screen>
Expand Down
22 changes: 22 additions & 0 deletions packages/material-top-tabs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@navigation-ex/material-top-tabs",
"version": "0.0.1",
"main": "src/index",
"license": "MIT",
"dependencies": {
"@navigation-ex/core": "^0.0.1",
"@navigation-ex/routers": "^0.0.1"
},
"devDependencies": {
"@types/react": "^16.8.24",
"@types/react-native": "^0.60.2",
"react": "16.8.3",
"react-native": "^0.59.8",
"react-native-tab-view": "^2.0.0"
},
"peerDependencies": {
"react": "*",
"react-native": "*",
"react-native-tab-view": "^2.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable react-native/no-inline-styles */

import * as React from 'react';
import { View } from 'react-native';
import { TabView, TabBar } from 'react-native-tab-view';
import {
useNavigationBuilder,
NavigationProp,
Expand All @@ -14,9 +12,15 @@ import {
TabNavigationState,
} from '@navigation-ex/routers';

type Props = TabRouterOptions & {
children: React.ReactNode;
};
type Props = TabRouterOptions &
Partial<
Omit<
React.ComponentProps<typeof TabView>,
'navigationState' | 'onIndexChange' | 'renderScene' | 'renderTabBar'
>
> & {
children: React.ReactNode;
};

export type TabNavigationOptions = {
/**
Expand All @@ -25,7 +29,7 @@ export type TabNavigationOptions = {
title?: string;
};

export type TabNavigationProp<
export type MaterialTopTabNavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string
> = NavigationProp<
Expand All @@ -47,29 +51,37 @@ export type TabNavigationProp<
): void;
};

export function TabNavigator(props: Props) {
const { state, descriptors } = useNavigationBuilder<
export function TabNavigator({
initialRouteName,
backBehavior,
children,
...rest
}: Props) {
const { state, descriptors, navigation } = useNavigationBuilder<
TabNavigationState,
TabNavigationOptions,
TabRouterOptions
>(TabRouter, props);
>(TabRouter, {
initialRouteName,
backBehavior,
children,
});

return (
<View style={{ display: 'flex', flexDirection: 'row', height: '100%' }}>
{state.routes.map((route, i, self) => (
<View
key={route.key}
style={{
width: `${100 / self.length}%`,
padding: 10,
borderRadius: 3,
backgroundColor: i === state.index ? 'tomato' : 'white',
}}
>
{descriptors[route.key].render()}
</View>
))}
</View>
<TabView
{...rest}
navigationState={state}
onIndexChange={index =>
navigation.navigate({ key: state.routes[index].key }, {})
}
renderScene={({ route }) => descriptors[route.key].render()}
renderTabBar={props => (
<TabBar
getLabelText={({ route }) => descriptors[route.key].options.title}
{...props}
/>
)}
/>
);
}

Expand Down
Loading

0 comments on commit e0bee10

Please sign in to comment.