mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-12 22:51:18 +08:00
fix: more improvements to types
This commit is contained in:
@@ -16,10 +16,6 @@ type EventName =
|
||||
| 'didBlur'
|
||||
| 'refocus';
|
||||
|
||||
// const focusSubscriptions = new WeakMap<() => void, () => void>();
|
||||
// const blurSubscriptions = new WeakMap<() => void, () => void>();
|
||||
// const refocusSubscriptions = new WeakMap<() => void, () => void>();
|
||||
|
||||
export default function createCompatNavigationProp<
|
||||
NavigationPropType extends NavigationProp<ParamListBase>,
|
||||
ParamList extends ParamListBase = NavigationPropType extends NavigationProp<
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
TypedNavigator,
|
||||
NavigationProp,
|
||||
RouteProp,
|
||||
EventMapBase,
|
||||
NavigationRouteContext,
|
||||
} from '@react-navigation/native';
|
||||
import CompatScreen from './CompatScreen';
|
||||
@@ -19,7 +18,7 @@ export default function createCompatNavigatorFactory<
|
||||
ParamListBase,
|
||||
NavigationState,
|
||||
{},
|
||||
EventMapBase,
|
||||
any,
|
||||
React.ComponentType<any>
|
||||
>
|
||||
>(createNavigator: CreateNavigator) {
|
||||
@@ -116,7 +115,8 @@ export default function createCompatNavigatorFactory<
|
||||
typeof screenNavigationOptions === 'function'
|
||||
? {
|
||||
navigation: createCompatNavigationProp<
|
||||
NavigationPropType
|
||||
NavigationPropType,
|
||||
ParamList
|
||||
>(navigation, route, {}),
|
||||
navigationOptions: defaultNavigationOptions || {},
|
||||
screenProps,
|
||||
|
||||
@@ -17,8 +17,11 @@ import useOptionsGetters from './useOptionsGetters';
|
||||
import useEventEmitter from './useEventEmitter';
|
||||
import useSyncState from './useSyncState';
|
||||
import isSerializable from './isSerializable';
|
||||
|
||||
import type { NavigationContainerRef, NavigationContainerProps } from './types';
|
||||
import type {
|
||||
NavigationContainerEventMap,
|
||||
NavigationContainerRef,
|
||||
NavigationContainerProps,
|
||||
} from './types';
|
||||
|
||||
type State = NavigationState | PartialState<NavigationState> | undefined;
|
||||
|
||||
@@ -175,7 +178,7 @@ const BaseNavigationContainer = React.forwardRef(
|
||||
return state.routes[state.index];
|
||||
}, [getRootState]);
|
||||
|
||||
const emitter = useEventEmitter();
|
||||
const emitter = useEventEmitter<NavigationContainerEventMap>();
|
||||
|
||||
const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({});
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ export type EventArg<
|
||||
preventDefault(): void;
|
||||
}
|
||||
: {}) &
|
||||
(Data extends undefined ? {} : { readonly data: Data });
|
||||
(undefined extends Data ? {} : { readonly data: Data });
|
||||
|
||||
export type EventListenerCallback<
|
||||
EventMap extends EventMapBase,
|
||||
@@ -107,7 +107,7 @@ export type EventEmitter<EventMap extends EventMapBase> = {
|
||||
} & (EventMap[EventName]['canPreventDefault'] extends true
|
||||
? { canPreventDefault: true }
|
||||
: {}) &
|
||||
(EventMap[EventName]['data'] extends undefined
|
||||
(undefined extends EventMap[EventName]['data']
|
||||
? {}
|
||||
: { data: EventMap[EventName]['data'] })
|
||||
): EventArg<
|
||||
@@ -275,7 +275,7 @@ export type RouteProp<
|
||||
ParamList extends ParamListBase,
|
||||
RouteName extends keyof ParamList
|
||||
> = Omit<Route<Extract<RouteName, string>>, 'params'> &
|
||||
(ParamList[RouteName] extends undefined
|
||||
(undefined extends ParamList[RouteName]
|
||||
? {}
|
||||
: {
|
||||
/**
|
||||
@@ -419,7 +419,7 @@ export type NavigationContainerEventMap = {
|
||||
/**
|
||||
* The updated state object after the state change.
|
||||
*/
|
||||
state: NavigationState;
|
||||
state: NavigationState | PartialState<NavigationState> | undefined;
|
||||
};
|
||||
};
|
||||
/**
|
||||
|
||||
@@ -51,7 +51,7 @@ type Options<
|
||||
addStateGetter: (key: string, getter: NavigatorStateGetter) => void;
|
||||
onRouteFocus: (key: string) => void;
|
||||
router: Router<State, NavigationAction>;
|
||||
emitter: NavigationEventEmitter;
|
||||
emitter: NavigationEventEmitter<any>;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import * as React from 'react';
|
||||
import type { EventEmitter, EventConsumer, EventArg } from './types';
|
||||
|
||||
export type NavigationEventEmitter = EventEmitter<Record<string, any>> & {
|
||||
create: (target: string) => EventConsumer<Record<string, any>>;
|
||||
export type NavigationEventEmitter<
|
||||
T extends Record<string, any>
|
||||
> = EventEmitter<T> & {
|
||||
create: (target: string) => EventConsumer<T>;
|
||||
};
|
||||
|
||||
type Listeners = ((e: any) => void)[];
|
||||
@@ -10,9 +12,9 @@ type Listeners = ((e: any) => void)[];
|
||||
/**
|
||||
* Hook to manage the event system used by the navigator to notify screens of various events.
|
||||
*/
|
||||
export default function useEventEmitter(
|
||||
export default function useEventEmitter<T extends Record<string, any>>(
|
||||
listen?: (e: any) => void
|
||||
): NavigationEventEmitter {
|
||||
): NavigationEventEmitter<T> {
|
||||
const listenRef = React.useRef(listen);
|
||||
|
||||
React.useEffect(() => {
|
||||
|
||||
@@ -2,16 +2,20 @@ import * as React from 'react';
|
||||
import type { NavigationState } from '@react-navigation/routers';
|
||||
import NavigationContext from './NavigationContext';
|
||||
import type { NavigationEventEmitter } from './useEventEmitter';
|
||||
import type { EventMapCore } from './types';
|
||||
|
||||
type Options = {
|
||||
state: NavigationState;
|
||||
emitter: NavigationEventEmitter;
|
||||
type Options<State extends NavigationState> = {
|
||||
state: State;
|
||||
emitter: NavigationEventEmitter<EventMapCore<State>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to take care of emitting `focus` and `blur` events.
|
||||
*/
|
||||
export default function useFocusEvents({ state, emitter }: Options) {
|
||||
export default function useFocusEvents<State extends NavigationState>({
|
||||
state,
|
||||
emitter,
|
||||
}: Options<State>) {
|
||||
const navigation = React.useContext(NavigationContext);
|
||||
const lastFocusedKeyRef = React.useRef<string | undefined>();
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
RouteConfig,
|
||||
PrivateValueStore,
|
||||
EventMapBase,
|
||||
EventMapCore,
|
||||
} from './types';
|
||||
import useStateGetters from './useStateGetters';
|
||||
import useOnGetState from './useOnGetState';
|
||||
@@ -378,7 +379,7 @@ export default function useNavigationBuilder<
|
||||
: (initializedStateRef.current as State);
|
||||
}, [getCurrentState, isStateInitialized]);
|
||||
|
||||
const emitter = useEventEmitter((e) => {
|
||||
const emitter = useEventEmitter<EventMapCore<State>>((e) => {
|
||||
let routeNames = [];
|
||||
|
||||
let route: Route<string> | undefined;
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
} from '@react-navigation/routers';
|
||||
import type { NavigationEventEmitter } from './useEventEmitter';
|
||||
|
||||
import type { NavigationHelpers, NavigationProp } from './types';
|
||||
import type { EventMapBase, NavigationHelpers, NavigationProp } from './types';
|
||||
|
||||
type Options<State extends NavigationState> = {
|
||||
state: State;
|
||||
@@ -19,7 +19,7 @@ type Options<State extends NavigationState> = {
|
||||
cb: (options: Record<string, object>) => Record<string, object>
|
||||
) => void;
|
||||
router: Router<State, NavigationAction>;
|
||||
emitter: NavigationEventEmitter;
|
||||
emitter: NavigationEventEmitter<EventMapBase>;
|
||||
};
|
||||
|
||||
type NavigationCache<
|
||||
|
||||
@@ -20,7 +20,7 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
|
||||
visitedNavigators?: Set<string>
|
||||
) => boolean;
|
||||
getState: () => State;
|
||||
emitter: NavigationEventEmitter;
|
||||
emitter: NavigationEventEmitter<any>;
|
||||
router: Router<State, Action>;
|
||||
};
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
|
||||
* @param [params] Params object for the new route.
|
||||
*/
|
||||
replace<RouteName extends keyof ParamList>(
|
||||
...args: ParamList[RouteName] extends undefined
|
||||
...args: undefined extends ParamList[RouteName]
|
||||
? [RouteName] | [RouteName, ParamList[RouteName]]
|
||||
: [RouteName, ParamList[RouteName]]
|
||||
): void;
|
||||
@@ -63,7 +63,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
|
||||
* @param [params] Params object for the route.
|
||||
*/
|
||||
push<RouteName extends keyof ParamList>(
|
||||
...args: ParamList[RouteName] extends undefined | any
|
||||
...args: undefined extends ParamList[RouteName]
|
||||
? [RouteName] | [RouteName, ParamList[RouteName]]
|
||||
: [RouteName, ParamList[RouteName]]
|
||||
): void;
|
||||
|
||||
@@ -42,7 +42,7 @@ export type TabActionHelpers<ParamList extends ParamListBase> = {
|
||||
* @param [params] Params object for the route.
|
||||
*/
|
||||
jumpTo<RouteName extends Extract<keyof ParamList, string>>(
|
||||
...args: ParamList[RouteName] extends undefined | any
|
||||
...args: undefined extends ParamList[RouteName]
|
||||
? [RouteName] | [RouteName, ParamList[RouteName]]
|
||||
: [RouteName, ParamList[RouteName]]
|
||||
): void;
|
||||
|
||||
Reference in New Issue
Block a user