Skip to content

Commit

Permalink
Merge pull request Expensify#30072 from VickyStash/ts-migration/withN…
Browse files Browse the repository at this point in the history
…avigationFallback

[No QA] [TS migration] Migrate 'withNavigationFallback.js' HOC to TypeScript
  • Loading branch information
techievivek authored Nov 6, 2023
2 parents a13b7fd + c0166dd commit 8dc7c46
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
47 changes: 0 additions & 47 deletions src/components/withNavigationFallback.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/components/withNavigationFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {NavigationContext} from '@react-navigation/core';
import {NavigationProp} from '@react-navigation/native';
import {ParamListBase} from '@react-navigation/routers';
import React, {ComponentType, ForwardedRef, forwardRef, ReactElement, RefAttributes, useContext, useMemo} from 'react';

type AddListenerCallback = () => void;

type RemoveListenerCallback = () => void;

type NavigationContextValue = {
isFocused: () => boolean;
addListener: () => AddListenerCallback;
removeListener: () => RemoveListenerCallback;
};

export default function <TProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>): (props: TProps & RefAttributes<TRef>) => ReactElement | null {
function WithNavigationFallback(props: TProps, ref: ForwardedRef<TRef>) {
const context = useContext(NavigationContext);

const navigationContextValue: NavigationContextValue = useMemo(
() => ({
isFocused: () => true,
addListener: () => () => {},
removeListener: () => () => {},
}),
[],
);

return context ? (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
/>
) : (
<NavigationContext.Provider value={navigationContextValue as unknown as NavigationProp<ParamListBase>}>
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
/>
</NavigationContext.Provider>
);
}

WithNavigationFallback.displayName = 'WithNavigationFocusWithFallback';

return forwardRef(WithNavigationFallback);
}

0 comments on commit 8dc7c46

Please sign in to comment.