@amazon-devices/react-navigation__routers 2.0.1758683737
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +17 -0
- package/SystemBundles/b690027feea4132f8720e462805cba84c12cd0e25f99e321a1a2c8b794832a91.bundle +3515 -0
- package/SystemBundles/b690027feea4132f8720e462805cba84c12cd0e25f99e321a1a2c8b794832a91.map +1 -0
- package/SystemModules/amzn__react-navigation__routers-2.modules.txt +21 -0
- package/SystemModules/amzn__react-navigation__routers-2.private.modules.txt +13 -0
- package/SystemModules/keplerscript-system-bundle-config.json +7 -0
- package/THIRD_PARTY_NOTICES.txt +24 -0
- package/kepler-compatibility.json +8 -0
- package/kepler-transformed-package-info.json +3 -0
- package/lib/commonjs/BaseRouter.js +61 -0
- package/lib/commonjs/BaseRouter.js.map +1 -0
- package/lib/commonjs/CommonActions.js +53 -0
- package/lib/commonjs/CommonActions.js.map +1 -0
- package/lib/commonjs/DrawerRouter.js +154 -0
- package/lib/commonjs/DrawerRouter.js.map +1 -0
- package/lib/commonjs/StackRouter.js +310 -0
- package/lib/commonjs/StackRouter.js.map +1 -0
- package/lib/commonjs/TabRouter.js +253 -0
- package/lib/commonjs/TabRouter.js.map +1 -0
- package/lib/commonjs/index.js +80 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/BaseRouter.js +55 -0
- package/lib/module/BaseRouter.js.map +1 -0
- package/lib/module/CommonActions.js +44 -0
- package/lib/module/CommonActions.js.map +1 -0
- package/lib/module/DrawerRouter.js +145 -0
- package/lib/module/DrawerRouter.js.map +1 -0
- package/lib/module/StackRouter.js +302 -0
- package/lib/module/StackRouter.js.map +1 -0
- package/lib/module/TabRouter.js +245 -0
- package/lib/module/TabRouter.js.map +1 -0
- package/lib/module/index.js +8 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/src/BaseRouter.d.ts +27 -0
- package/lib/typescript/src/BaseRouter.d.ts.map +1 -0
- package/lib/typescript/src/CommonActions.d.ts +56 -0
- package/lib/typescript/src/CommonActions.d.ts.map +1 -0
- package/lib/typescript/src/DrawerRouter.d.ts +53 -0
- package/lib/typescript/src/DrawerRouter.d.ts.map +1 -0
- package/lib/typescript/src/StackRouter.d.ts +69 -0
- package/lib/typescript/src/StackRouter.d.ts.map +1 -0
- package/lib/typescript/src/TabRouter.d.ts +41 -0
- package/lib/typescript/src/TabRouter.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +11 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +177 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/package.json +1 -0
- package/src/BaseRouter.tsx +81 -0
- package/src/CommonActions.tsx +89 -0
- package/src/DrawerRouter.tsx +247 -0
- package/src/StackRouter.tsx +495 -0
- package/src/TabRouter.tsx +407 -0
- package/src/index.tsx +28 -0
- package/src/types.tsx +228 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type * as CommonActions from './CommonActions';
|
|
2
|
+
export type CommonNavigationAction = CommonActions.Action;
|
|
3
|
+
type NavigationRoute<ParamList extends ParamListBase, RouteName extends keyof ParamList> = Route<Extract<RouteName, string>, ParamList[RouteName]> & {
|
|
4
|
+
state?: NavigationState | PartialState<NavigationState>;
|
|
5
|
+
};
|
|
6
|
+
export type NavigationState<ParamList extends ParamListBase = ParamListBase> = Readonly<{
|
|
7
|
+
/**
|
|
8
|
+
* Unique key for the navigation state.
|
|
9
|
+
*/
|
|
10
|
+
key: string;
|
|
11
|
+
/**
|
|
12
|
+
* Index of the currently focused route.
|
|
13
|
+
*/
|
|
14
|
+
index: number;
|
|
15
|
+
/**
|
|
16
|
+
* List of valid route names as defined in the screen components.
|
|
17
|
+
*/
|
|
18
|
+
routeNames: Extract<keyof ParamList, string>[];
|
|
19
|
+
/**
|
|
20
|
+
* Alternative entries for history.
|
|
21
|
+
*/
|
|
22
|
+
history?: unknown[];
|
|
23
|
+
/**
|
|
24
|
+
* List of rendered routes.
|
|
25
|
+
*/
|
|
26
|
+
routes: NavigationRoute<ParamList, keyof ParamList>[];
|
|
27
|
+
/**
|
|
28
|
+
* Custom type for the state, whether it's for tab, stack, drawer etc.
|
|
29
|
+
* During rehydration, the state will be discarded if type doesn't match with router type.
|
|
30
|
+
* It can also be used to detect the type of the navigator we're dealing with.
|
|
31
|
+
*/
|
|
32
|
+
type: string;
|
|
33
|
+
/**
|
|
34
|
+
* Whether the navigation state has been rehydrated.
|
|
35
|
+
*/
|
|
36
|
+
stale: false;
|
|
37
|
+
}>;
|
|
38
|
+
export type InitialState = Readonly<Partial<Omit<NavigationState, 'stale' | 'routes'>> & {
|
|
39
|
+
routes: (Omit<Route<string>, 'key'> & {
|
|
40
|
+
state?: InitialState;
|
|
41
|
+
})[];
|
|
42
|
+
}>;
|
|
43
|
+
export type PartialRoute<R extends Route<string>> = Omit<R, 'key'> & {
|
|
44
|
+
key?: string;
|
|
45
|
+
state?: PartialState<NavigationState>;
|
|
46
|
+
};
|
|
47
|
+
export type PartialState<State extends NavigationState> = Partial<Omit<State, 'stale' | 'routes'>> & Readonly<{
|
|
48
|
+
stale?: true;
|
|
49
|
+
routes: PartialRoute<Route<State['routeNames'][number]>>[];
|
|
50
|
+
}>;
|
|
51
|
+
export type Route<RouteName extends string, Params extends object | undefined = object | undefined> = Readonly<{
|
|
52
|
+
/**
|
|
53
|
+
* Unique key for the route.
|
|
54
|
+
*/
|
|
55
|
+
key: string;
|
|
56
|
+
/**
|
|
57
|
+
* User-provided name for the route.
|
|
58
|
+
*/
|
|
59
|
+
name: RouteName;
|
|
60
|
+
/**
|
|
61
|
+
* Path associated with the route.
|
|
62
|
+
* Usually present when the screen was opened from a deep link.
|
|
63
|
+
*/
|
|
64
|
+
path?: string;
|
|
65
|
+
}> & (undefined extends Params ? Readonly<{
|
|
66
|
+
/**
|
|
67
|
+
* Params for this route
|
|
68
|
+
*/
|
|
69
|
+
params?: Readonly<Params>;
|
|
70
|
+
}> : Readonly<{
|
|
71
|
+
/**
|
|
72
|
+
* Params for this route
|
|
73
|
+
*/
|
|
74
|
+
params: Readonly<Params>;
|
|
75
|
+
}>);
|
|
76
|
+
export type ParamListBase = Record<string, object | undefined>;
|
|
77
|
+
export type NavigationAction = Readonly<{
|
|
78
|
+
/**
|
|
79
|
+
* Type of the action (e.g. `NAVIGATE`)
|
|
80
|
+
*/
|
|
81
|
+
type: string;
|
|
82
|
+
/**
|
|
83
|
+
* Additional data for the action
|
|
84
|
+
*/
|
|
85
|
+
payload?: object;
|
|
86
|
+
/**
|
|
87
|
+
* Key of the route which dispatched this action.
|
|
88
|
+
*/
|
|
89
|
+
source?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Key of the navigator which should handle this action.
|
|
92
|
+
*/
|
|
93
|
+
target?: string;
|
|
94
|
+
}>;
|
|
95
|
+
export type ActionCreators<Action extends NavigationAction> = {
|
|
96
|
+
[key: string]: (...args: any) => Action;
|
|
97
|
+
};
|
|
98
|
+
export type DefaultRouterOptions<RouteName extends string = string> = {
|
|
99
|
+
/**
|
|
100
|
+
* Name of the route to focus by on initial render.
|
|
101
|
+
* If not specified, usually the first route is used.
|
|
102
|
+
*/
|
|
103
|
+
initialRouteName?: RouteName;
|
|
104
|
+
};
|
|
105
|
+
export type RouterFactory<State extends NavigationState, Action extends NavigationAction, RouterOptions extends DefaultRouterOptions> = (options: RouterOptions) => Router<State, Action>;
|
|
106
|
+
export type RouterConfigOptions = {
|
|
107
|
+
routeNames: string[];
|
|
108
|
+
routeParamList: ParamListBase;
|
|
109
|
+
routeGetIdList: Record<string, ((options: {
|
|
110
|
+
params?: Record<string, any>;
|
|
111
|
+
}) => string | undefined) | undefined>;
|
|
112
|
+
};
|
|
113
|
+
export type Router<State extends NavigationState, Action extends NavigationAction> = {
|
|
114
|
+
/**
|
|
115
|
+
* Type of the router. Should match the `type` property in state.
|
|
116
|
+
* If the type doesn't match, the state will be discarded during rehydration.
|
|
117
|
+
*/
|
|
118
|
+
type: State['type'];
|
|
119
|
+
/**
|
|
120
|
+
* Initialize the navigation state.
|
|
121
|
+
*
|
|
122
|
+
* @param options.routeNames List of valid route names as defined in the screen components.
|
|
123
|
+
* @param options.routeParamsList Object containing params for each route.
|
|
124
|
+
*/
|
|
125
|
+
getInitialState(options: RouterConfigOptions): State;
|
|
126
|
+
/**
|
|
127
|
+
* Rehydrate the full navigation state from a given partial state.
|
|
128
|
+
*
|
|
129
|
+
* @param partialState Navigation state to rehydrate from.
|
|
130
|
+
* @param options.routeNames List of valid route names as defined in the screen components.
|
|
131
|
+
* @param options.routeParamsList Object containing params for each route.
|
|
132
|
+
*/
|
|
133
|
+
getRehydratedState(partialState: PartialState<State> | State, options: RouterConfigOptions): State;
|
|
134
|
+
/**
|
|
135
|
+
* Take the current state and updated list of route names, and return a new state.
|
|
136
|
+
*
|
|
137
|
+
* @param state State object to update.
|
|
138
|
+
* @param options.routeNames New list of route names.
|
|
139
|
+
* @param options.routeParamsList Object containing params for each route.
|
|
140
|
+
*/
|
|
141
|
+
getStateForRouteNamesChange(state: State, options: RouterConfigOptions & {
|
|
142
|
+
/**
|
|
143
|
+
* List of routes whose key has changed even if they still have the same name.
|
|
144
|
+
* This allows to remove screens declaratively.
|
|
145
|
+
*/
|
|
146
|
+
routeKeyChanges: string[];
|
|
147
|
+
}): State;
|
|
148
|
+
/**
|
|
149
|
+
* Take the current state and key of a route, and return a new state with the route focused
|
|
150
|
+
*
|
|
151
|
+
* @param state State object to apply the action on.
|
|
152
|
+
* @param key Key of the route to focus.
|
|
153
|
+
*/
|
|
154
|
+
getStateForRouteFocus(state: State, key: string): State;
|
|
155
|
+
/**
|
|
156
|
+
* Take the current state and action, and return a new state.
|
|
157
|
+
* If the action cannot be handled, return `null`.
|
|
158
|
+
*
|
|
159
|
+
* @param state State object to apply the action on.
|
|
160
|
+
* @param action Action object to apply.
|
|
161
|
+
* @param options.routeNames List of valid route names as defined in the screen components.
|
|
162
|
+
* @param options.routeParamsList Object containing params for each route.
|
|
163
|
+
*/
|
|
164
|
+
getStateForAction(state: State, action: Action, options: RouterConfigOptions): State | PartialState<State> | null;
|
|
165
|
+
/**
|
|
166
|
+
* Whether the action should also change focus in parent navigator
|
|
167
|
+
*
|
|
168
|
+
* @param action Action object to check.
|
|
169
|
+
*/
|
|
170
|
+
shouldActionChangeFocus(action: NavigationAction): boolean;
|
|
171
|
+
/**
|
|
172
|
+
* Action creators for the router.
|
|
173
|
+
*/
|
|
174
|
+
actionCreators?: ActionCreators<Action>;
|
|
175
|
+
};
|
|
176
|
+
export {};
|
|
177
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,aAAa,MAAM,iBAAiB,CAAC;AAEtD,MAAM,MAAM,sBAAsB,GAAG,aAAa,CAAC,MAAM,CAAC;AAE1D,KAAK,eAAe,CAClB,SAAS,SAAS,aAAa,EAC/B,SAAS,SAAS,MAAM,SAAS,IAC/B,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC,GAAG;IAC5D,KAAK,CAAC,EAAE,eAAe,GAAG,YAAY,CAAC,eAAe,CAAC,CAAC;CACzD,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,SAAS,SAAS,aAAa,GAAG,aAAa,IACzE,QAAQ,CAAC;IACP;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,UAAU,EAAE,OAAO,CAAC,MAAM,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC;IAC/C;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,EAA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AE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,KAAK,MAAM,GAAG,SAAS,CAAC,GACnE,SAAS,CACZ,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,MAAM,CAChB,KAAK,SAAS,eAAe,EAC7B,MAAM,SAAS,gBAAgB,IAC7B;IACF;;;OAGG;IACH,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAEpB;;;;;OAKG;IACH,eAAe,CAAC,OAAO,EAAE,mBAAmB,GAAG,KAAK,CAAC;IAErD;;;;;;OAMG;IACH,kBAAkB,CAChB,YAAY,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,KAAK,EACzC,OAAO,EAAE,mBAAmB,GAC3B,KAAK,CAAC;IAET;;;;;;OAMG;IACH,2BAA2B,CACzB,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,mBAAmB,GAAG;QAC7B;;;WAGG;QACH,eAAe,EAAE,MAAM,EAAE,CAAC;KAC3B,GACA,KAAK,CAAC;IAET;;;;;OAKG;IACH,qBAAqB,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,CAAC;IAExD;;;;;;;;OAQG;IACH,iBAAiB,CACf,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,mBAAmB,GAC3B,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAEtC;;;;OAIG;IACH,uBAAuB,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC;IAE3D;;OAEG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;CACzC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "@amazon-devices/react-navigation__routers", "description": "Routers to help build custom navigators", "version": "2.0.1758683737+6.1.9", "keywords": ["react", "react-native", "react-navigation"], "license": "See license in LICENSE", "repository": {"type": "git", "url": "https://github.com/react-navigation/react-navigation.git", "directory": "packages/routers"}, "bugs": {"url": "https://github.com/react-navigation/react-navigation/issues"}, "homepage": "https://reactnavigation.org/docs/custom-routers/", "main": "lib/commonjs/index.js", "react-native": "src/index.tsx", "source": "src/index.tsx", "module": "lib/module/index.js", "types": "lib/typescript/src/index.d.ts", "files": ["src", "lib", "kepler-compatibility.json", "!**/__tests__", "SystemModules", "SystemBundles", "THIRD_PARTY_NOTICES.txt", "kepler-transformed-package-info.json"], "sideEffects": false, "kepler": {"temporary-alias-for-package": {"name": "@react-navigation/routers", "version": "6.1.9"}}, "publishConfig": {"access": "public"}, "scripts": {"clean": "del lib"}, "dependencies": {"nanoid": "3.1.23"}, "devDependencies": {"del-cli": "5.0.0", "react-native-builder-bob": "0.20.4", "typescript": "4.9.4", "@types/jest": "26.0.15"}, "react-native-builder-bob": {"source": "src", "output": "lib", "targets": ["commonjs", "module", ["typescript", {"project": "tsconfig.build.json"}]]}}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
CommonNavigationAction,
|
|
5
|
+
NavigationState,
|
|
6
|
+
PartialState,
|
|
7
|
+
} from './types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base router object that can be used when writing custom routers.
|
|
11
|
+
* This provides few helper methods to handle common actions such as `RESET`.
|
|
12
|
+
*/
|
|
13
|
+
const BaseRouter = {
|
|
14
|
+
getStateForAction<State extends NavigationState>(
|
|
15
|
+
state: State,
|
|
16
|
+
action: CommonNavigationAction
|
|
17
|
+
): State | PartialState<State> | null {
|
|
18
|
+
switch (action.type) {
|
|
19
|
+
case 'SET_PARAMS': {
|
|
20
|
+
const index = action.source
|
|
21
|
+
? state.routes.findIndex((r) => r.key === action.source)
|
|
22
|
+
: state.index;
|
|
23
|
+
|
|
24
|
+
if (index === -1) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...state,
|
|
30
|
+
routes: state.routes.map((r, i) =>
|
|
31
|
+
i === index
|
|
32
|
+
? { ...r, params: { ...r.params, ...action.payload.params } }
|
|
33
|
+
: r
|
|
34
|
+
),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
case 'RESET': {
|
|
39
|
+
const nextState = action.payload as State | PartialState<State>;
|
|
40
|
+
|
|
41
|
+
if (
|
|
42
|
+
nextState.routes.length === 0 ||
|
|
43
|
+
nextState.routes.some(
|
|
44
|
+
(route: { name: string }) => !state.routeNames.includes(route.name)
|
|
45
|
+
)
|
|
46
|
+
) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (nextState.stale === false) {
|
|
51
|
+
if (
|
|
52
|
+
state.routeNames.length !== nextState.routeNames.length ||
|
|
53
|
+
nextState.routeNames.some(
|
|
54
|
+
(name) => !state.routeNames.includes(name)
|
|
55
|
+
)
|
|
56
|
+
) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
...nextState,
|
|
62
|
+
routes: nextState.routes.map((route) =>
|
|
63
|
+
route.key ? route : { ...route, key: `${route.name}-${nanoid()}` }
|
|
64
|
+
),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return nextState;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
default:
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
shouldActionChangeFocus(action: CommonNavigationAction) {
|
|
77
|
+
return action.type === 'NAVIGATE';
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export default BaseRouter;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { NavigationState, PartialState, Route } from './types';
|
|
2
|
+
|
|
3
|
+
type ResetState =
|
|
4
|
+
| PartialState<NavigationState>
|
|
5
|
+
| NavigationState
|
|
6
|
+
| (Omit<NavigationState, 'routes'> & {
|
|
7
|
+
routes: Omit<Route<string>, 'key'>[];
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export type Action =
|
|
11
|
+
| {
|
|
12
|
+
type: 'GO_BACK';
|
|
13
|
+
source?: string;
|
|
14
|
+
target?: string;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
type: 'NAVIGATE';
|
|
18
|
+
payload:
|
|
19
|
+
| {
|
|
20
|
+
key: string;
|
|
21
|
+
name?: undefined;
|
|
22
|
+
params?: object;
|
|
23
|
+
path?: string;
|
|
24
|
+
merge?: boolean;
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
name: string;
|
|
28
|
+
key?: string;
|
|
29
|
+
params?: object;
|
|
30
|
+
path?: string;
|
|
31
|
+
merge?: boolean;
|
|
32
|
+
};
|
|
33
|
+
source?: string;
|
|
34
|
+
target?: string;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
type: 'RESET';
|
|
38
|
+
payload: ResetState | undefined;
|
|
39
|
+
source?: string;
|
|
40
|
+
target?: string;
|
|
41
|
+
}
|
|
42
|
+
| {
|
|
43
|
+
type: 'SET_PARAMS';
|
|
44
|
+
payload: { params?: object };
|
|
45
|
+
source?: string;
|
|
46
|
+
target?: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export function goBack(): Action {
|
|
50
|
+
return { type: 'GO_BACK' };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function navigate(
|
|
54
|
+
options:
|
|
55
|
+
| { key: string; params?: object; path?: string; merge?: boolean }
|
|
56
|
+
| {
|
|
57
|
+
name: string;
|
|
58
|
+
key?: string;
|
|
59
|
+
params?: object;
|
|
60
|
+
path?: string;
|
|
61
|
+
merge?: boolean;
|
|
62
|
+
}
|
|
63
|
+
): Action;
|
|
64
|
+
// eslint-disable-next-line no-redeclare
|
|
65
|
+
export function navigate(name: string, params?: object): Action;
|
|
66
|
+
// eslint-disable-next-line no-redeclare
|
|
67
|
+
export function navigate(...args: any): Action {
|
|
68
|
+
if (typeof args[0] === 'string') {
|
|
69
|
+
return { type: 'NAVIGATE', payload: { name: args[0], params: args[1] } };
|
|
70
|
+
} else {
|
|
71
|
+
const payload = args[0] || {};
|
|
72
|
+
|
|
73
|
+
if (!payload.hasOwnProperty('key') && !payload.hasOwnProperty('name')) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
'You need to specify name or key when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { type: 'NAVIGATE', payload };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function reset(state: ResetState | undefined): Action {
|
|
84
|
+
return { type: 'RESET', payload: state };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function setParams(params: object): Action {
|
|
88
|
+
return { type: 'SET_PARAMS', payload: { params } };
|
|
89
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
|
|
3
|
+
import TabRouter, {
|
|
4
|
+
TabActionHelpers,
|
|
5
|
+
TabActions,
|
|
6
|
+
TabActionType,
|
|
7
|
+
TabNavigationState,
|
|
8
|
+
TabRouterOptions,
|
|
9
|
+
} from './TabRouter';
|
|
10
|
+
import type {
|
|
11
|
+
CommonNavigationAction,
|
|
12
|
+
ParamListBase,
|
|
13
|
+
PartialState,
|
|
14
|
+
Router,
|
|
15
|
+
} from './types';
|
|
16
|
+
export type DrawerStatus = 'open' | 'closed';
|
|
17
|
+
|
|
18
|
+
export type DrawerActionType =
|
|
19
|
+
| TabActionType
|
|
20
|
+
| {
|
|
21
|
+
type: 'OPEN_DRAWER' | 'CLOSE_DRAWER' | 'TOGGLE_DRAWER';
|
|
22
|
+
source?: string;
|
|
23
|
+
target?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type DrawerRouterOptions = TabRouterOptions & {
|
|
27
|
+
defaultStatus?: DrawerStatus;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type DrawerNavigationState<ParamList extends ParamListBase> = Omit<
|
|
31
|
+
TabNavigationState<ParamList>,
|
|
32
|
+
'type' | 'history'
|
|
33
|
+
> & {
|
|
34
|
+
/**
|
|
35
|
+
* Type of the router, in this case, it's drawer.
|
|
36
|
+
*/
|
|
37
|
+
type: 'drawer';
|
|
38
|
+
/**
|
|
39
|
+
* Default status of the drawer.
|
|
40
|
+
*/
|
|
41
|
+
default: DrawerStatus;
|
|
42
|
+
/**
|
|
43
|
+
* List of previously visited route keys and drawer open status.
|
|
44
|
+
*/
|
|
45
|
+
history: (
|
|
46
|
+
| { type: 'route'; key: string }
|
|
47
|
+
| { type: 'drawer'; status: DrawerStatus }
|
|
48
|
+
)[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type DrawerActionHelpers<ParamList extends ParamListBase> =
|
|
52
|
+
TabActionHelpers<ParamList> & {
|
|
53
|
+
/**
|
|
54
|
+
* Open the drawer sidebar.
|
|
55
|
+
*/
|
|
56
|
+
openDrawer(): void;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Close the drawer sidebar.
|
|
60
|
+
*/
|
|
61
|
+
closeDrawer(): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Open the drawer sidebar if closed, or close if opened.
|
|
65
|
+
*/
|
|
66
|
+
toggleDrawer(): void;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const DrawerActions = {
|
|
70
|
+
...TabActions,
|
|
71
|
+
openDrawer(): DrawerActionType {
|
|
72
|
+
return { type: 'OPEN_DRAWER' };
|
|
73
|
+
},
|
|
74
|
+
closeDrawer(): DrawerActionType {
|
|
75
|
+
return { type: 'CLOSE_DRAWER' };
|
|
76
|
+
},
|
|
77
|
+
toggleDrawer(): DrawerActionType {
|
|
78
|
+
return { type: 'TOGGLE_DRAWER' };
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default function DrawerRouter({
|
|
83
|
+
defaultStatus = 'closed',
|
|
84
|
+
...rest
|
|
85
|
+
}: DrawerRouterOptions): Router<
|
|
86
|
+
DrawerNavigationState<ParamListBase>,
|
|
87
|
+
DrawerActionType | CommonNavigationAction
|
|
88
|
+
> {
|
|
89
|
+
const router = TabRouter(rest) as unknown as Router<
|
|
90
|
+
DrawerNavigationState<ParamListBase>,
|
|
91
|
+
TabActionType | CommonNavigationAction
|
|
92
|
+
>;
|
|
93
|
+
|
|
94
|
+
const isDrawerInHistory = (
|
|
95
|
+
state:
|
|
96
|
+
| DrawerNavigationState<ParamListBase>
|
|
97
|
+
| PartialState<DrawerNavigationState<ParamListBase>>
|
|
98
|
+
) => Boolean(state.history?.some((it) => it.type === 'drawer'));
|
|
99
|
+
|
|
100
|
+
const addDrawerToHistory = (
|
|
101
|
+
state: DrawerNavigationState<ParamListBase>
|
|
102
|
+
): DrawerNavigationState<ParamListBase> => {
|
|
103
|
+
if (isDrawerInHistory(state)) {
|
|
104
|
+
return state;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
...state,
|
|
109
|
+
history: [
|
|
110
|
+
...state.history,
|
|
111
|
+
{
|
|
112
|
+
type: 'drawer',
|
|
113
|
+
status: defaultStatus === 'open' ? 'closed' : 'open',
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const removeDrawerFromHistory = (
|
|
120
|
+
state: DrawerNavigationState<ParamListBase>
|
|
121
|
+
): DrawerNavigationState<ParamListBase> => {
|
|
122
|
+
if (!isDrawerInHistory(state)) {
|
|
123
|
+
return state;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...state,
|
|
128
|
+
history: state.history.filter((it) => it.type !== 'drawer'),
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const openDrawer = (
|
|
133
|
+
state: DrawerNavigationState<ParamListBase>
|
|
134
|
+
): DrawerNavigationState<ParamListBase> => {
|
|
135
|
+
if (defaultStatus === 'open') {
|
|
136
|
+
return removeDrawerFromHistory(state);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return addDrawerToHistory(state);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const closeDrawer = (
|
|
143
|
+
state: DrawerNavigationState<ParamListBase>
|
|
144
|
+
): DrawerNavigationState<ParamListBase> => {
|
|
145
|
+
if (defaultStatus === 'open') {
|
|
146
|
+
return addDrawerToHistory(state);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return removeDrawerFromHistory(state);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
...router,
|
|
154
|
+
|
|
155
|
+
type: 'drawer',
|
|
156
|
+
|
|
157
|
+
getInitialState({ routeNames, routeParamList, routeGetIdList }) {
|
|
158
|
+
const state = router.getInitialState({
|
|
159
|
+
routeNames,
|
|
160
|
+
routeParamList,
|
|
161
|
+
routeGetIdList,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
...state,
|
|
166
|
+
default: defaultStatus,
|
|
167
|
+
stale: false,
|
|
168
|
+
type: 'drawer',
|
|
169
|
+
key: `drawer-${nanoid()}`,
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
getRehydratedState(
|
|
174
|
+
partialState,
|
|
175
|
+
{ routeNames, routeParamList, routeGetIdList }
|
|
176
|
+
) {
|
|
177
|
+
if (partialState.stale === false) {
|
|
178
|
+
return partialState;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let state = router.getRehydratedState(partialState, {
|
|
182
|
+
routeNames,
|
|
183
|
+
routeParamList,
|
|
184
|
+
routeGetIdList,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
if (isDrawerInHistory(partialState)) {
|
|
188
|
+
// Re-sync the drawer entry in history to correct it if it was wrong
|
|
189
|
+
state = removeDrawerFromHistory(state);
|
|
190
|
+
state = addDrawerToHistory(state);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
...state,
|
|
195
|
+
default: defaultStatus,
|
|
196
|
+
type: 'drawer',
|
|
197
|
+
key: `drawer-${nanoid()}`,
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
getStateForRouteFocus(state, key) {
|
|
202
|
+
const result = router.getStateForRouteFocus(state, key);
|
|
203
|
+
|
|
204
|
+
return closeDrawer(result);
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
getStateForAction(state, action, options) {
|
|
208
|
+
switch (action.type) {
|
|
209
|
+
case 'OPEN_DRAWER':
|
|
210
|
+
return openDrawer(state);
|
|
211
|
+
|
|
212
|
+
case 'CLOSE_DRAWER':
|
|
213
|
+
return closeDrawer(state);
|
|
214
|
+
|
|
215
|
+
case 'TOGGLE_DRAWER':
|
|
216
|
+
if (isDrawerInHistory(state)) {
|
|
217
|
+
return removeDrawerFromHistory(state);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return addDrawerToHistory(state);
|
|
221
|
+
|
|
222
|
+
case 'JUMP_TO':
|
|
223
|
+
case 'NAVIGATE': {
|
|
224
|
+
const result = router.getStateForAction(state, action, options);
|
|
225
|
+
|
|
226
|
+
if (result != null && result.index !== state.index) {
|
|
227
|
+
return closeDrawer(result as DrawerNavigationState<ParamListBase>);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
case 'GO_BACK':
|
|
234
|
+
if (isDrawerInHistory(state)) {
|
|
235
|
+
return removeDrawerFromHistory(state);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return router.getStateForAction(state, action, options);
|
|
239
|
+
|
|
240
|
+
default:
|
|
241
|
+
return router.getStateForAction(state, action, options);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
actionCreators: DrawerActions,
|
|
246
|
+
};
|
|
247
|
+
}
|