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

[TS migration] Migrate 'CountrySelector.js' component to TypeScript #34454

4 changes: 4 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3128,4 +3128,8 @@ const CONST = {
MINI_CONTEXT_MENU_MAX_ITEMS: 4,
} as const;

type Country = keyof typeof CONST.ALL_COUNTRIES;

export type {Country};

export default CONST;
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
import PropTypes from 'prop-types';
import React, {useEffect} from 'react';
import React, {forwardRef, useEffect} from 'react';
import type {ForwardedRef} from 'react';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {Country} from '@src/CONST';
import ROUTES from '@src/ROUTES';
import FormHelpMessage from './FormHelpMessage';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import refPropTypes from './refPropTypes';

const propTypes = {
type CountrySelectorProps = {
/** Form error text. e.g when no country is selected */
errorText: PropTypes.string,
errorText?: string;

/** Callback called when the country changes. */
onInputChange: PropTypes.func.isRequired,
onInputChange: (value?: string) => void;

/** Current selected country */
value: PropTypes.string,
value?: Country;

/** inputID used by the Form component */
// eslint-disable-next-line react/no-unused-prop-types
inputID: PropTypes.string.isRequired,

/** React ref being forwarded to the MenuItemWithTopDescription */
forwardedRef: refPropTypes,
};

const defaultProps = {
errorText: '',
value: undefined,
forwardedRef: () => {},
inputID: string;
};

function CountrySelector({errorText, value: countryCode, onInputChange, forwardedRef}) {
function CountrySelector({errorText = '', value: countryCode, onInputChange}: CountrySelectorProps, ref: ForwardedRef<View>) {
const styles = useThemeStyles();
const {translate} = useLocalize();

Expand All @@ -51,12 +42,12 @@ function CountrySelector({errorText, value: countryCode, onInputChange, forwarde
<MenuItemWithTopDescription
shouldShowRightIcon
title={title}
ref={forwardedRef}
ref={ref}
descriptionTextStyle={countryTitleDescStyle}
description={translate('common.country')}
onPress={() => {
const activeRoute = Navigation.getActiveRouteWithoutParams();
Navigation.navigate(ROUTES.SETTINGS_PERSONAL_DETAILS_ADDRESS_COUNTRY.getRoute(countryCode, activeRoute));
Navigation.navigate(ROUTES.SETTINGS_PERSONAL_DETAILS_ADDRESS_COUNTRY.getRoute(countryCode ?? '', activeRoute));
}}
/>
<View style={styles.ml5}>
Expand All @@ -66,18 +57,6 @@ function CountrySelector({errorText, value: countryCode, onInputChange, forwarde
);
}

CountrySelector.propTypes = propTypes;
CountrySelector.defaultProps = defaultProps;
CountrySelector.displayName = 'CountrySelector';

const CountrySelectorWithRef = React.forwardRef((props, ref) => (
<CountrySelector
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));

CountrySelectorWithRef.displayName = 'CountrySelectorWithRef';

export default CountrySelectorWithRef;
export default forwardRef(CountrySelector);
3 changes: 2 additions & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CONST as COMMON_CONST} from 'expensify-common/lib/CONST';
import Str from 'expensify-common/lib/str';
import CONST from '@src/CONST';
import type {Country} from '@src/CONST';
import type {
AddressLineParams,
AlreadySignedInParams,
Expand Down Expand Up @@ -106,7 +107,7 @@ type StateValue = {

type States = Record<keyof typeof COMMON_CONST.STATES, StateValue>;

type AllCountries = Record<keyof typeof CONST.ALL_COUNTRIES, string>;
type AllCountries = Record<Country, string>;

/* eslint-disable max-len */
export default {
Expand Down
Loading