Skip to content

Commit

Permalink
Merge pull request Expensify#29426 from software-mansion-labs/ts-migr…
Browse files Browse the repository at this point in the history
…ation/with-navigation-focus-hoc

[No QA][TS migration] Migrate 'withNavigationFocus.js' HOC to TypeScript
  • Loading branch information
pecanoro authored Oct 24, 2023
2 parents d0e72b1 + 0b580c6 commit d282473
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 51 deletions.
6 changes: 4 additions & 2 deletions src/components/AvatarWithImagePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback';
import AttachmentModal from './AttachmentModal';
import DotIndicatorMessage from './DotIndicatorMessage';
import * as Browser from '../libs/Browser';
import withNavigationFocus, {withNavigationFocusPropTypes} from './withNavigationFocus';
import withNavigationFocus from './withNavigationFocus';
import compose from '../libs/compose';

const propTypes = {
Expand Down Expand Up @@ -91,8 +91,10 @@ const propTypes = {
/** File name of the avatar */
originalFileName: PropTypes.string,

/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,

...withLocalizePropTypes,
...withNavigationFocusPropTypes,
};

const defaultProps = {
Expand Down
6 changes: 4 additions & 2 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import OptionsList from '../OptionsList';
import CONST from '../../CONST';
import styles from '../../styles/styles';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import withNavigationFocus, {withNavigationFocusPropTypes} from '../withNavigationFocus';
import withNavigationFocus from '../withNavigationFocus';
import TextInput from '../TextInput';
import ArrowKeyFocusManager from '../ArrowKeyFocusManager';
import KeyboardShortcut from '../../libs/KeyboardShortcut';
Expand All @@ -32,9 +32,11 @@ const propTypes = {
/** List styles for OptionsList */
listStyles: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),

/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,

...optionsSelectorPropTypes,
...withLocalizePropTypes,
...withNavigationFocusPropTypes,
};

const defaultProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/RoomNameInput/roomNameInputPropTypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PropTypes from 'prop-types';
import {withNavigationFocusPropTypes} from '../withNavigationFocus';

const propTypes = {
/** Callback to execute when the text input is modified correctly */
Expand Down Expand Up @@ -29,7 +28,8 @@ const propTypes = {
/** Whether we should wait before focusing the TextInput, useful when using transitions on Android */
shouldDelayFocus: PropTypes.bool,

...withNavigationFocusPropTypes,
/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,
};

const defaultProps = {
Expand Down
40 changes: 0 additions & 40 deletions src/components/withNavigationFocus.js

This file was deleted.

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

type WithNavigationFocusProps = {
isFocused: boolean;
};

export default function withNavigationFocus<TProps extends WithNavigationFocusProps, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): (props: Omit<TProps, keyof WithNavigationFocusProps> & React.RefAttributes<TRef>) => React.ReactElement | null {
function WithNavigationFocus(props: Omit<TProps, keyof WithNavigationFocusProps>, ref: ForwardedRef<TRef>) {
const isFocused = useIsFocused();
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
isFocused={isFocused}
/>
);
}

WithNavigationFocus.displayName = `withNavigationFocus(${getComponentDisplayName(WrappedComponent)})`;
return React.forwardRef(WithNavigationFocus);
}
7 changes: 4 additions & 3 deletions src/pages/signin/LoginForm/BaseLoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import GoogleSignIn from '../../../components/SignInButtons/GoogleSignIn';
import isInputAutoFilled from '../../../libs/isInputAutoFilled';
import * as PolicyUtils from '../../../libs/PolicyUtils';
import Log from '../../../libs/Log';
import withNavigationFocus, {withNavigationFocusPropTypes} from '../../../components/withNavigationFocus';
import withNavigationFocus from '../../../components/withNavigationFocus';
import usePrevious from '../../../hooks/usePrevious';
import * as MemoryOnlyKeys from '../../../libs/actions/MemoryOnlyKeys/MemoryOnlyKeys';

Expand Down Expand Up @@ -66,13 +66,14 @@ const propTypes = {
/** Whether or not the sign in page is being rendered in the RHP modal */
isInModal: PropTypes.bool,

/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,

...windowDimensionsPropTypes,

...withLocalizePropTypes,

...toggleVisibilityViewPropTypes,

...withNavigationFocusPropTypes,
};

const defaultProps = {
Expand Down
5 changes: 3 additions & 2 deletions src/pages/workspace/WorkspaceNewRoomPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {View} from 'react-native';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import withNavigationFocus, {withNavigationFocusPropTypes} from '../../components/withNavigationFocus';
import withNavigationFocus from '../../components/withNavigationFocus';
import * as Report from '../../libs/actions/Report';
import * as App from '../../libs/actions/App';
import useLocalize from '../../hooks/useLocalize';
Expand Down Expand Up @@ -61,7 +61,8 @@ const propTypes = {
/** A collection of objects for all policies which key policy member objects by accountIDs */
allPolicyMembers: PropTypes.objectOf(PropTypes.objectOf(policyMemberPropType)),

...withNavigationFocusPropTypes,
/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,
};
const defaultProps = {
betas: [],
Expand Down

0 comments on commit d282473

Please sign in to comment.