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

Commit

Permalink
fix: address cr
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 23, 2019
1 parent 6637ae0 commit 69eceed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
9 changes: 7 additions & 2 deletions src/__tests__/__fixtures__/MockRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {Router, CommonAction, NavigationState, DefaultRouterOptions} from '../../types';
import { BaseRouter } from '../../index';
import BaseRouter from '../../BaseRouter';
import {
Router,
CommonAction,
NavigationState,
DefaultRouterOptions,
} from '../../types';

export type MockActions = CommonAction & {
type: 'NOOP' | 'REVERSE' | 'UPDATE';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, act } from 'react-native-testing-library';
import Screen from '../Screen';
import NavigationContainer from '../NavigationContainer';
import useNavigationBuilder from '../useNavigationBuilder';
import MockRouter, {MockRouterKey} from './__fixtures__/MockRouter';
import MockRouter, { MockRouterKey } from './__fixtures__/MockRouter';

beforeEach(() => (MockRouterKey.current = 0));

Expand Down
18 changes: 11 additions & 7 deletions src/__tests__/useDescriptors.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ it('sets options with options prop as an object', () => {
const TestNavigator = (props: any) => {
const { state, descriptors } = useNavigationBuilder<
NavigationState,
{ title?: string }
{ title?: string },
any
>(MockRouter, props);
const { render, options } = descriptors[state.routes[state.index].key];

Expand Down Expand Up @@ -57,7 +58,8 @@ it('sets options with options prop as a fuction', () => {
const TestNavigator = (props: any) => {
const { state, descriptors } = useNavigationBuilder<
NavigationState,
{ title?: string }
{ title?: string },
any
>(MockRouter, props);
const { render, options } = descriptors[state.routes[state.index].key];

Expand Down Expand Up @@ -104,7 +106,8 @@ it('sets initial options with setOptions', () => {
{
title?: string;
color?: string;
}
},
any
>(MockRouter, props);
const { render, options } = descriptors[state.routes[state.index].key];

Expand Down Expand Up @@ -151,10 +154,11 @@ it('sets initial options with setOptions', () => {

it('updates options with setOptions', () => {
const TestNavigator = (props: any) => {
const { state, descriptors } = useNavigationBuilder<NavigationState, any>(
MockRouter,
props
);
const { state, descriptors } = useNavigationBuilder<
NavigationState,
any,
any
>(MockRouter, props);
const { render, options } = descriptors[state.routes[state.index].key];

return (
Expand Down
8 changes: 3 additions & 5 deletions src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as BaseActions from './BaseActions';
import * as React from 'react';

export type CommonAction = BaseActions.Action;

Expand Down Expand Up @@ -73,10 +72,9 @@ export type ActionCreators<Action extends NavigationAction> = {

export type DefaultRouterOptions = {
initialRouteName?: string;
children: React.ReactNode;
};

export type RouterHelper<
export type RouterFactory<
State extends NavigationState,
Action extends NavigationAction,
RouterOptions extends DefaultRouterOptions
Expand All @@ -90,7 +88,7 @@ export type Router<
* Initialize the navigation state.
*
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.initialRouteName Route to focus in the state.
* @param options.routeParamsList Object containing params for each route.
*/
getInitialState(options: {
routeNames: string[];
Expand All @@ -113,7 +111,7 @@ export type Router<
*
* @param state State object to update.
* @param options.routeNames New list of route names.
* @param options.initialRouteName Route to focus in the state.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForRouteNamesChange(
state: State,
Expand Down
24 changes: 14 additions & 10 deletions src/useNavigationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import {
ParamListBase,
RouteConfig,
Router,
RouterHelper,
RouterFactory,
} from './types';
import useOnRouteFocus from './useOnRouteFocus';
import useChildActionListeners from './useChildActionListeners';

type Options = {
children: React.ReactNode;
};

const isArrayEqual = (a: any[], b: any[]) =>
a.length === b.length && a.every((it, index) => it === b[index]);

Expand Down Expand Up @@ -54,27 +58,27 @@ const getRouteConfigsFromChildren = <ScreenOptions extends object>(
export default function useNavigationBuilder<
State extends NavigationState,
ScreenOptions extends object,
RouterOptions extends DefaultRouterOptions = DefaultRouterOptions
RouterOptions extends DefaultRouterOptions
>(
routerCreator: RouterHelper<State, any, RouterOptions>,
options: RouterOptions
createRouter: RouterFactory<State, any, RouterOptions>,
options: Options & RouterOptions
) {
useRegisterNavigator();

const { children, ...rest } = options;
const { current: router } = React.useRef<Router<State, any>>(
routerCreator(options)
createRouter((rest as unknown) as RouterOptions)
);
useRegisterNavigator();

const screens = getRouteConfigsFromChildren<ScreenOptions>(
options.children
).reduce(
const screens = getRouteConfigsFromChildren<ScreenOptions>(children).reduce(
(acc, curr) => {
acc[curr.name] = curr;
return acc;
},
{} as { [key: string]: RouteConfig<ParamListBase, string, ScreenOptions> }
);

const routeNames = Object.keys(screens)
const routeNames = Object.keys(screens);
const routeParamList = routeNames.reduce(
(acc, curr) => {
acc[curr] = screens[curr].initialParams;
Expand Down

0 comments on commit 69eceed

Please sign in to comment.