Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS Migration] migrate withNavigation.js to TypeScript #29531

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
},
{
selector: ['parameter', 'method'],
format: ['camelCase'],
format: ['camelCase', 'PascalCase'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB - why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, let me explain! So when it comes to lines 9-11 (and typing HOCs in general) we are following guidelines from these two sources:

it's the same approach in all TypeScript HOC PRs, that's something we agreed upon. About PascalCase, it's necessary because in HOCs components (WrappedComponent) are passed as function parameters.

Hope that answers your questions, if there is anything else you'd like me to explain let me know!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh got it. Cool, that makes sense!

},
],
'@typescript-eslint/ban-types': [
Expand Down
40 changes: 0 additions & 40 deletions src/components/withNavigation.js

This file was deleted.

24 changes: 24 additions & 0 deletions src/components/withNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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>>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return type for this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

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
2 changes: 1 addition & 1 deletion src/libs/getComponentDisplayName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentType} from 'react';

/** Returns the display name of a component */
export default function getComponentDisplayName(component: ComponentType): string {
export default function getComponentDisplayName<TProps>(component: ComponentType<TProps>): string {
return component.displayName ?? component.name ?? 'Component';
}
Loading