| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- const router = {
- type: 'tab',
-
- getInitialState({ routeNames, routeParamList }) {
- const index =
- options.initialRouteName === undefined
- ? 0
- : routeNames.indexOf(options.initialRouteName);
-
- return {
- stale: false,
- type: 'tab',
- key: shortid(),
- index,
- routeNames,
- routes: routeNames.map(name => ({
- name,
- key: name,
- params: routeParamList[name],
- })),
- };
- },
-
- getRehydratedState(partialState, { routeNames, routeParamList }) {
- const state = partialState;
-
- if (state.stale === false) {
- return state as NavigationState;
- }
-
- const routes = state.routes
- .filter(route => routeNames.includes(route.name))
- .map(
- route =>
- ({
- ...route,
- key: route.key || `${route.name}-${shortid()}`,
- params:
- routeParamList[route.name] !== undefined
- ? {
- ...routeParamList[route.name],
- ...route.params,
- }
- : route.params,
- } as Route<string>)
- );
-
- return {
- stale: false,
- type: 'tab',
- key: shortid(),
- index:
- typeof state.index === 'number' && state.index < routes.length
- ? state.index
- : 0,
- routeNames,
- routes,
- };
- },
-
- getStateForRouteNamesChange(state, { routeNames }) {
- const routes = state.routes.filter(route =>
- routeNames.includes(route.name)
- );
-
- return {
- ...state,
- routeNames,
- routes,
- index: Math.min(state.index, routes.length - 1),
- };
- },
-
- getStateForRouteFocus(state, key) {
- const index = state.routes.findIndex(r => r.key === key);
-
- if (index === -1 || index === state.index) {
- return state;
- }
-
- return { ...state, index };
- },
-
- getStateForAction(state, action) {
- switch (action.type) {
- case 'NAVIGATE': {
- const index = state.routes.findIndex(
- route => route.name === action.payload.name
- );
-
- if (index === -1) {
- return null;
- }
-
- return { ...state, index };
- }
-
- default:
- return BaseRouter.getStateForAction(state, action);
- }
- },
-
- shouldActionChangeFocus() {
- return false;
- },
- };
-
- const SimpleRouter = () => router;
-
- export default SimpleRouter;
|