-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
types.tsx
520 lines (485 loc) · 14.8 KB
/
types.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import * as React from 'react';
import {
DefaultRouterOptions,
NavigationState,
NavigationAction,
InitialState,
PartialState,
Route,
ParamListBase,
} from '@react-navigation/routers';
export type DefaultNavigatorOptions<
ScreenOptions extends object
> = DefaultRouterOptions & {
/**
* Children React Elements to extract the route configuration from.
* Only `Screen` components are supported as children.
*/
children: React.ReactNode;
/**
* Default options for all screens under this navigator.
*/
screenOptions?:
| ScreenOptions
| ((props: {
route: RouteProp<ParamListBase, string>;
navigation: any;
}) => ScreenOptions);
};
export type EventMapBase = Record<
string,
{ data?: any; canPreventDefault?: boolean }
>;
export type EventMapCore<State extends NavigationState> = {
focus: { data: undefined };
blur: { data: undefined };
state: { data: { state: State } };
};
export type EventArg<
EventName extends string,
CanPreventDefault extends boolean | undefined = false,
Data = undefined
> = {
/**
* Type of the event (e.g. `focus`, `blur`)
*/
readonly type: EventName;
readonly target?: string;
} & (CanPreventDefault extends true
? {
/**
* Whether `event.preventDefault()` was called on this event object.
*/
readonly defaultPrevented: boolean;
/**
* Prevent the default action which happens on this event.
*/
preventDefault(): void;
}
: {}) &
(Data extends undefined ? {} : { readonly data: Data });
export type EventListenerCallback<
EventMap extends EventMapBase,
EventName extends keyof EventMap
> = (
e: EventArg<
Extract<EventName, string>,
EventMap[EventName]['canPreventDefault'],
EventMap[EventName]['data']
>
) => void;
export type EventConsumer<EventMap extends EventMapBase> = {
/**
* Subscribe to events from the parent navigator.
*
* @param type Type of the event (e.g. `focus`, `blur`)
* @param callback Callback listener which is executed upon receiving the event.
*/
addListener<EventName extends Extract<keyof EventMap, string>>(
type: EventName,
callback: EventListenerCallback<EventMap, EventName>
): () => void;
removeListener<EventName extends Extract<keyof EventMap, string>>(
type: EventName,
callback: EventListenerCallback<EventMap, EventName>
): void;
};
export type EventEmitter<EventMap extends EventMapBase> = {
/**
* Emit an event to child screens.
*
* @param options.type Type of the event (e.g. `focus`, `blur`)
* @param [options.data] Optional information regarding the event.
* @param [options.target] Key of the target route which should receive the event.
* If not specified, all routes receive the event.
*/
emit<EventName extends Extract<keyof EventMap, string>>(
options: {
type: EventName;
target?: string;
} & (EventMap[EventName]['canPreventDefault'] extends true
? { canPreventDefault: true }
: {}) &
(EventMap[EventName]['data'] extends undefined
? {}
: { data: EventMap[EventName]['data'] })
): EventArg<
EventName,
EventMap[EventName]['canPreventDefault'],
EventMap[EventName]['data']
>;
};
export class PrivateValueStore<A, B, C> {
/**
* UGLY HACK! DO NOT USE THE TYPE!!!
*
* TypeScript requires a type to be used to be able to infer it.
* The type should exist as its own without any operations such as union.
* So we need to figure out a way to store this type in a property.
* The problem with a normal property is that it shows up in intelliSense.
* Adding private keyword works, but the annotation is stripped away in declaration.
* Turns out if we use an empty string, it doesn't show up in intelliSense.
*/
protected ''?: { a: A; b: B; c: C };
}
type NavigationHelpersCommon<
ParamList extends ParamListBase,
State extends NavigationState = NavigationState
> = {
/**
* Dispatch an action or an update function to the router.
* The update function will receive the current state,
*
* @param action Action object or update function.
*/
dispatch(
action: NavigationAction | ((state: State) => NavigationAction)
): void;
/**
* Navigate to a route in current navigation tree.
*
* @param name Name of the route to navigate to.
* @param [params] Params object for the route.
*/
navigate<RouteName extends keyof ParamList>(
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;
/**
* Navigate to a route in current navigation tree.
*
* @param route Object with `key` or `name` for the route to navigate to, and a `params` object.
*/
navigate<RouteName extends keyof ParamList>(
route:
| { key: string; params?: ParamList[RouteName] }
| { name: RouteName; key?: string; params: ParamList[RouteName] }
): void;
/**
* Reset the navigation state to the provided state.
*
* @param state Navigation state object.
*/
reset(state: PartialState<State> | State): void;
/**
* Go back to the previous route in history.
*/
goBack(): void;
/**
* Check if the screen is focused. The method returns `true` if focused, `false` otherwise.
* Note that this method doesn't re-render screen when the focus changes. So don't use it in `render`.
* To get notified of focus changes, use `addListener('focus', cb)` and `addListener('blur', cb)`.
* To conditionally render content based on focus state, use the `useIsFocused` hook.
*/
isFocused(): boolean;
/**
* Check if dispatching back action will be handled by navigation.
* Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
*/
canGoBack(): boolean;
/**
* Returns the parent navigator, if any. Reason why the function is called
* dangerouslyGetParent is to warn developers against overusing it to eg. get parent
* of parent and other hard-to-follow patterns.
*/
dangerouslyGetParent<T = NavigationProp<ParamListBase> | undefined>(): T;
/**
* Returns the navigator's state. Reason why the function is called
* dangerouslyGetState is to discourage developers to use internal navigation's state.
* Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
*/
dangerouslyGetState(): State;
} & PrivateValueStore<ParamList, keyof ParamList, {}>;
export type NavigationHelpers<
ParamList extends ParamListBase,
EventMap extends EventMapBase = {}
> = NavigationHelpersCommon<ParamList> &
EventEmitter<EventMap> & {
/**
* Update the param object for the route.
* The new params will be shallow merged with the old one.
*
* @param params Params object for the current route.
*/
setParams<RouteName extends keyof ParamList>(
params: Partial<ParamList[RouteName]>
): void;
};
export type NavigationContainerProps = {
/**
* Initial navigation state for the child navigators.
*/
initialState?: InitialState;
/**
* Callback which is called with the latest navigation state when it changes.
*/
onStateChange?: (state: NavigationState | undefined) => void;
/**
* Whether this navigation container should be independent of parent containers.
* If this is not set to `true`, this container cannot be nested inside another container.
* Setting it to `true` disconnects any children navigators from parent container.
*/
independent?: boolean;
/**
* Children elements to render.
*/
children: React.ReactNode;
};
export type NavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
State extends NavigationState = NavigationState,
ScreenOptions extends object = {},
EventMap extends EventMapBase = {}
> = NavigationHelpersCommon<ParamList, State> & {
/**
* Update the param object for the route.
* The new params will be shallow merged with the old one.
*
* @param params Params object for the current route.
*/
setParams(params: Partial<ParamList[RouteName]>): void;
/**
* Update the options for the route.
* The options object will be shallow merged with default options object.
*
* @param options Options object for the route.
*/
setOptions(options: Partial<ScreenOptions>): void;
} & EventConsumer<EventMap & EventMapCore<State>> &
PrivateValueStore<ParamList, RouteName, EventMap>;
export type RouteProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList
> = Omit<Route<Extract<RouteName, string>>, 'params'> &
(ParamList[RouteName] extends undefined
? {}
: {
/**
* Params for this route
*/
params: ParamList[RouteName];
});
export type CompositeNavigationProp<
A extends NavigationProp<ParamListBase, string, any, any>,
B extends NavigationHelpersCommon<ParamListBase, any>
> = Omit<A & B, keyof NavigationProp<any>> &
NavigationProp<
/**
* Param list from both navigation objects needs to be combined
* For example, we should be able to navigate to screens in both A and B
*/
(A extends NavigationHelpersCommon<infer T> ? T : never) &
(B extends NavigationHelpersCommon<infer U> ? U : never),
/**
* The route name should refer to the route name specified in the first type
* Ideally it should work for any of them, but it's not possible to infer that way
*/
A extends NavigationProp<any, infer R> ? R : string,
/**
* The type of state should refer to the state specified in the first type
*/
A extends NavigationProp<any, any, infer S> ? S : NavigationState,
/**
* Screen options from both navigation objects needs to be combined
* This allows typechecking `setOptions`
*/
(A extends NavigationProp<any, any, any, infer O> ? O : {}) &
(B extends NavigationProp<any, any, any, infer P> ? P : {}),
/**
* Event consumer config should refer to the config specified in the first type
* This allows typechecking `addListener`/`removeListener`
*/
A extends NavigationProp<any, any, any, any, infer E> ? E : {}
>;
export type Descriptor<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
State extends NavigationState = NavigationState,
ScreenOptions extends object = {},
EventMap extends EventMapBase = {}
> = {
/**
* Render the component associated with this route.
*/
render(): JSX.Element;
/**
* Options for the route.
*/
options: ScreenOptions;
/**
* Navigation object for the screen
*/
navigation: NavigationProp<
ParamList,
RouteName,
State,
ScreenOptions,
EventMap
>;
};
export type ScreenListeners<
State extends NavigationState,
EventMap extends EventMapBase
> = Partial<
{
[EventName in keyof (EventMap &
EventMapCore<State>)]: EventListenerCallback<EventMap, EventName>;
}
>;
export type RouteConfig<
ParamList extends ParamListBase,
RouteName extends keyof ParamList,
State extends NavigationState,
ScreenOptions extends object,
EventMap extends EventMapBase
> = {
/**
* Route name of this screen.
*/
name: RouteName;
/**
* Navigator options for this screen.
*/
options?:
| ScreenOptions
| ((props: {
route: RouteProp<ParamList, RouteName>;
navigation: any;
}) => ScreenOptions);
/**
* Event listeners for this screen.
*/
listeners?:
| ScreenListeners<State, EventMap>
| ((props: {
route: RouteProp<ParamList, RouteName>;
navigation: any;
}) => ScreenListeners<State, EventMap>);
/**
* Initial params object for the route.
*/
initialParams?: Partial<ParamList[RouteName]>;
} & (
| {
/**
* React component to render for this screen.
*/
component: React.ComponentType<any>;
}
| {
/**
* Render callback to render content of this screen.
*/
children: (props: {
route: RouteProp<ParamList, RouteName>;
navigation: any;
}) => React.ReactNode;
}
);
export type NavigationContainerEventMap = {
/**
* Event which fires when the navigation state changes.
*/
state: {
data: {
/**
* The updated state object after the state change.
*/
state: NavigationState;
};
};
/**
* Event which fires when an action is dispatched.
* Only intended for debugging purposes, don't use it for app logic.
* This event will be emitted before state changes have been applied.
*/
__unsafe_action__: {
data: {
/**
* The action object which was dispatched.
*/
action: NavigationAction;
/**
* Whether the action was a no-op, i.e. resulted any state changes.
*/
noop: boolean;
};
};
};
export type NavigationContainerRef = NavigationHelpers<ParamListBase> &
EventConsumer<NavigationContainerEventMap> & {
/**
* Reset the navigation state of the root navigator to the provided state.
*
* @param state Navigation state object.
*/
resetRoot(state?: PartialState<NavigationState> | NavigationState): void;
/**
* Get the rehydrated navigation state of the navigation tree.
*/
getRootState(): NavigationState;
/**
* Get the currently focused navigation route.
*/
getCurrentRoute(): Route<string> | undefined;
/**
* Get the currently focused route's options.
*/
getCurrentOptions(): object | undefined;
};
export type TypedNavigator<
ParamList extends ParamListBase,
State extends NavigationState,
ScreenOptions extends object,
EventMap extends EventMapBase,
Navigator extends React.ComponentType<any>
> = {
/**
* Navigator component which manages the child screens.
*/
Navigator: React.ComponentType<
Omit<
React.ComponentProps<Navigator>,
'initialRouteName' | 'screenOptions'
> & {
/**
* Name of the route to focus by on initial render.
* If not specified, usually the first route is used.
*/
initialRouteName?: keyof ParamList;
/**
* Default options for all screens under this navigator.
*/
screenOptions?:
| ScreenOptions
| ((props: {
route: RouteProp<ParamList, keyof ParamList>;
navigation: any;
}) => ScreenOptions);
/**
* Configuration for screens
*/
children: React.ReactNode;
}
>;
/**
* Component used for specifying route configuration.
*/
Screen: <RouteName extends keyof ParamList>(
_: RouteConfig<ParamList, RouteName, State, ScreenOptions, EventMap>
) => null;
};
export type PathConfig = {
[routeName: string]:
| string
| {
path?: string;
exact?: boolean;
parse?: Record<string, (value: string) => any>;
stringify?: Record<string, (value: any) => string>;
screens?: PathConfig;
initialRouteName?: string;
};
};