rn-international-phone-number

repository·master·Indexed 18 days ago

https://github.com/astroonauta/react-native-international-phone-number

An international mobile phone input component for React Native, Expo, and React Native Web. Version 0.14.0 features automatic masking, validation, line-type detection, and a country dial code selector. It includes the PhoneInput UI component, a headless usePhoneInput hook for custom interfaces, and utility functions for phone number and country data management. Supports 33 languages and smart paste for E.164 numbers.

Tokens
23.6K
Snippets
36
Records
45
Agent score
62%

What's inside rn-international-phone-number

  1. Overview of rn-international-phone-number

    master

    rn-international-phone-number is an international mobile phone input component designed for React Native, Expo, and React Native Web. It provides a complete user experience for phone number entry, including a country dial code selector, flag picker, and automatic phone number masking.

    Key capabilities include:

    • Auto-formatting: Applies a phone mask based on the selected country.
    • Validation: Validates phone numbers with an optional onValidationChange callback.
    • Line Type Detection: Identifies if a number is MOBILE, FIXED_LINE, TOLL_FREE, VOIP, etc.
    • Smart Paste: Automatically switches the country when an E.164 formatted number is pasted.
    • Headless UI: Provides the usePhoneInput hook for developers who want to build completely custom UIs using the library's logic.
    • Cross-Platform: Fully compatible with iOS, Android, and Web.
  2. Overview of rn-international-phone-number features

    master

    This library provides a comprehensive international phone input solution for React Native, Expo, and React Native Web. Key features include:

    • Phone Input Mask: Automatic formatting based on the selected country.
    • Validation: Optional onValidationChange callback to track number validity.
    • Line Type Detection: Identifies if a number is MOBILE, FIXED_LINE, TOLL_FREE, VOIP, etc.
    • Smart Paste: Automatically detects and switches the country when an E.164 number is pasted.
    • Dynamic Placeholder: Displays country-aware example numbers when placeholderType="number" is used.
    • Headless Hook: The usePhoneInput hook allows for building fully custom UIs by exposing all internal state and setters.
    • i18n: Support for 33 languages.
    • Cross-Platform: Works on iOS, Android, and Web.
  3. Target interactive elements with testID

    master

    The library exposes specific testID values for all interactive elements within the PhoneInput component and the country selector modal. These IDs allow you to reliably target elements in automated tests using tools like @testing-library/react-native or Maestro.

    // PhoneInput elements
    const phoneInput = getByTestId('countryPickerPhoneInput');
    const flagContainerButton = getByTestId('countryPickerFlagContainerButton');
    
    // Country selector modal elements
    const countrySelectModalContainer = getByTestId('countrySelectContainer');
    const countrySelectModalContent = getByTestId('countrySelectContent');
    const countrySelectBackdrop = getByTestId('countrySelectBackdrop');
    const countrySelectList = getByTestId('countrySelectList');
    const countrySelectSearchInput = getByTestId('countrySelectSearchInput');
    const countrySelectItem = getByTestId('countrySelectItem');
    const countrySelectCloseButton = getByTestId('countrySelectCloseButton');
    const countrySelectAlphabetFilter = getByTestId('countrySelectAlphabetFilter');
  4. Customize accessibility props in PhoneInput

    master

    The PhoneInput component exposes a wide range of React Native accessibility props to help you build inclusive applications. You can customize the accessibilityLabel and accessibilityHint for almost every interactive element within the phone input field and the country selector modal (including the search input, country list, and alphabet filter).

    <PhoneInput
      accessibilityLabelPhoneInput="Phone number input"
      accessibilityHintPhoneInput="Enter your mobile number"
      accessibilityLabelCountriesButton="Select country"
      accessibilityLabelSearchInput="Search for a country"
      // ... other props
    />
  5. Configure fonts for React Native CLI (Web)

    master

    To support web functionality in a React Native CLI project, you must manually link the fonts used by rn-country-select.

    1. Create a react-native.config.js file in your project root with the following configuration:
    module.exports = {
      project: {
        ios: {},
        android: {},
      },
      assets: [
        './node_modules/rn-country-select/lib/assets/fonts',
      ],
    };
    1. Run the asset linking command:
    npx react-native-asset
  6. Quick Start with PhoneInput

    master

    To implement a basic international phone input, use the PhoneInput component from rn-international-phone-number. You can manage the phone number and the selected country using React state. The component automatically handles formatting, country selection, and validation.

    Key props for basic usage:

    • value: The current phone number string.
    • onChangePhoneNumber: Callback function triggered when the phone number changes.
    • country: The currently selected ICountry object.
    • onChangeCountry: Callback function triggered when the country selection changes.
    import React, {useState} from 'react';
    import {Text, View} from 'react-native';
    import PhoneInput, {ICountry} from 'rn-international-phone-number';
    
    export default function App() {
      const [phone, setPhone] = useState('');
      const [country, setCountry] = useState<ICountry | null>(null);
    
      return (
        <View style={{width: '100%', flex: 1, padding: 24}}>
          <PhoneInput
            value={phone}
            onChangePhoneNumber={setPhone}
            country={country}
            onChangeCountry={setCountry}
          />
    
          <Text style={{marginTop: 12}}>
            {`Country: ${country?.name?.common || '-'}\nNational: ${phone}`}
          </Text>
        </View>
      );
    }
  7. Quick start with PhoneInput component

    master

    The PhoneInput component provides a ready-to-use international phone number input with a country dial code selector and flag picker. You can control the phone number and the selected country using value, onChangePhoneNumber, country, and onChangeCountry props.

    import React, {useState} from 'react';
    import {View} from 'react-native';
    import PhoneInput, {ICountry} from 'rn-international-phone-number';
    
    export default function App() {
      const [phone, setPhone] = useState('');
      const [country, setCountry] = useState<ICountry | null>(null);
    
      return (
        <View style={{flex: 1, padding: 24}}>
          <PhoneInput
            value={phone}
            onChangePhoneNumber={setPhone}
            country={country}
            onChangeCountry={setCountry}
          />
        </View>
      );
    }
  8. Configure fonts for Expo (Web)

    master

    To support web functionality in an Expo project, you need to install expo-font and load the required emoji font at app startup.

    1. Install expo-font:
    npx expo install expo-font
    1. Load the TwemojiMozilla font in your application entry point:
    import {useFonts} from 'expo-font';
    
    useFonts({
      TwemojiMozilla: require('./node_modules/rn-country-select/lib/assets/fonts/TwemojiMozilla.woff2'),
    });

    Note: You must recompile your project after adding new fonts.

  9. Internationalize the PhoneInput component

    master

    You can localize the country selector in the PhoneInput component using the language prop. This prop accepts either a 2-letter ISO 639-1 code or a 3-letter ISO 639-2 code.

    For example, to set the language to Portuguese or Simplified Chinese, pass the corresponding code to the language prop.

    <PhoneInput language="pt" />
    <PhoneInput language="zh-Hans" />
  10. Project Metadata and Capabilities

    master

    The rn-international-phone-number package is an international mobile phone input component for React Native. Key capabilities include:

    • Auto-formatting: Dynamic masking based on the selected country.
    • Validation: Built-in phone number validation.
    • Line-type detection: Identifies line types (e.g., MOBILE, FIXED_LINE, TOLL_FREE).
    • Smart Paste: Intelligent handling of pasted phone numbers.
    • Dynamic Placeholder: Placeholders update based on country context.
    • Headless Support: Provides a headless hook for custom UI implementations.
    • i18n: Supports 33 languages.
    • Cross-platform: Works on iOS, Android, and Web.
    • Form Integration: Ready for use with react-hook-form, formik, and @tanstack/react-form.