Skip to content

Commit

Permalink
Merge pull request #29531 from JKobrynski/migrateWithNavigationToType…
Browse files Browse the repository at this point in the history
…Script

[No QA] [TS Migration] migrate withNavigation.js to TypeScript
  • Loading branch information
dangrous authored Oct 19, 2023
2 parents 9b032d2 + 6119982 commit 2084476
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
40 changes: 0 additions & 40 deletions src/components/withNavigation.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/components/withNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, {ComponentType, ForwardedRef, RefAttributes} from 'react';
import {NavigationProp, useNavigation} from '@react-navigation/native';
import getComponentDisplayName from '../libs/getComponentDisplayName';

type WithNavigationProps = {
navigation: NavigationProp<ReactNavigation.RootParamList>;
};

export default function withNavigation<TProps extends WithNavigationProps, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): (props: Omit<TProps, keyof WithNavigationProps>, ref: ForwardedRef<TRef>) => React.JSX.Element | null {
function WithNavigation(props: Omit<TProps, keyof WithNavigationProps>, ref: ForwardedRef<TRef>) {
const navigation = useNavigation();
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
navigation={navigation}
/>
);
}

WithNavigation.displayName = `withNavigation(${getComponentDisplayName(WrappedComponent)})`;
return React.forwardRef(WithNavigation);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import React from 'react';
import {View} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import PropTypes from 'prop-types';

import * as ModalStackNavigators from '../ModalStackNavigators';
import RHPScreenOptions from '../RHPScreenOptions';
import useWindowDimensions from '../../../../hooks/useWindowDimensions';
import {withNavigationPropTypes} from '../../../../components/withNavigation';
import styles from '../../../../styles/styles';
import Overlay from './Overlay';
import NoDropZone from '../../../../components/DragAndDrop/NoDropZone';

const Stack = createStackNavigator();

const propTypes = {
...withNavigationPropTypes,
/* Navigation functions provided by React Navigation */
navigation: PropTypes.shape({
goBack: PropTypes.func.isRequired,
}).isRequired,
};

function RightModalNavigator(props) {
Expand Down
6 changes: 4 additions & 2 deletions src/libs/Navigation/AppNavigator/ReportScreenIDSetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {withOnyx} from 'react-native-onyx';
import ONYXKEYS from '../../../ONYXKEYS';
import * as ReportUtils from '../../ReportUtils';
import reportPropTypes from '../../../pages/reportPropTypes';
import {withNavigationPropTypes} from '../../../components/withNavigation';
import * as App from '../../actions/App';
import usePermissions from '../../../hooks/usePermissions';
import CONST from '../../../CONST';
Expand Down Expand Up @@ -40,7 +39,10 @@ const propTypes = {
}),
}).isRequired,

...withNavigationPropTypes,
/* Navigation functions provided by React Navigation */
navigation: PropTypes.shape({
setParams: PropTypes.func.isRequired,
}).isRequired,
};

const defaultProps = {
Expand Down
6 changes: 4 additions & 2 deletions src/libs/Navigation/AppNavigator/ReportScreenWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';
import {withNavigationPropTypes} from '../../../components/withNavigation';
import ReportScreen from '../../../pages/home/ReportScreen';
import ReportScreenIDSetter from './ReportScreenIDSetter';

Expand All @@ -17,7 +16,10 @@ const propTypes = {
}),
}).isRequired,

...withNavigationPropTypes,
/* Navigation functions provided by React Navigation */
navigation: PropTypes.shape({
setParams: PropTypes.func.isRequired,
}).isRequired,
};

const defaultProps = {};
Expand Down

0 comments on commit 2084476

Please sign in to comment.