0.2.1 • Published 4 months ago

react-native-phone-entry v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

react-native-phone-entry

npm version cov License

react-native-phone-entry is a simple and fully modifiable Phone Number Input Component for React Native that provides an intuitive interface for entering and validating international phone numbers. It includes country code selection, number formatting, and validation features.

Features

  • 🌍 International phone number input with country picker
  • 📱 Automatic phone number formatting based on country
  • 🔍 Dynamic country and mask adaptation based on typed country code
  • ✨ Highly customizable appearance and styling
  • 🎯 Phone number validation using Google's libphonenumber
  • 🎨 Dark theme support
  • ♿ Accessibility support
  • 💪 Written in TypeScript

Installation

npm install react-native-phone-entry

# or

yarn add react-native-phone-entry

Usage

  1. Import the PhoneInput component:
import { PhoneInput, isValidNumber } from 'react-native-phone-entry';
  1. Basic usage:
export default function App() {
  const [countryCode, setCountryCode] = useState < CountryCode > 'US';
  return (
    <PhoneInput
      defaultValues={{
        countryCode: 'US',
        callingCode: '+1',
        phoneNumber: '+1',
      }}
      onChangeText={(text) =>
        console.log(
          'Phone number:',
          text,
          'isValidNumber:',
          isValidNumber(text, countryCode)
        )
      }
      onChangeCountry={(country) => {
        console.log('Country:', country);
        setCountryCode(country.cca2);
      }}
    />
  );
}
  1. Advanced usage with customization:
<PhoneInput
  defaultValues={{
    countryCode: 'US',
    callingCode: '+1',
    phoneNumber: '+123456789',
  }}
  value="+123456789"
  onChangeText={(text) => console.log('Phone number:', text)}
  onChangeCountry={(country) => console.log('Country:', country)}
  autoFocus={true}
  disabled={false}
  countryPickerProps={{
    withFilter: true,
    withFlag: true,
    withCountryNameButton: true,
  }}
  theme={{
    containerStyle: styles.phoneContainer,
    textInputStyle: styles.input,
    flagButtonStyle: styles.flagButton,
    codeTextStyle: styles.codeText,
    dropDownImageStyle: styles.dropDownImage,
    enableDarkTheme: false,
  }}
  hideDropdownIcon={false}
  isCallingCodeEditable={false}
/>

Props

PropTypeDescriptionDefault
defaultValuesobjectDefault values for country code, calling code, and phone numberundefined
valuestringControlled value for the phone number inputundefined
onChangeText(text: string) => voidCallback when phone number changesundefined
onChangeCountry(country: Country) => voidCallback when selected country changesundefined
autoFocusbooleanAutomatically focuses the input when mountedfalse
disabledbooleanDisables the inputfalse
countryPickerPropsCountryPickerPropsProps for the country picker modal{}
maskInputPropsMaskInputPropsProps for the masked input component{}
themeThemeTheme configuration for styling the component{}
hideDropdownIconbooleanHides the dropdown arrow icon when set to truefalse
renderCustomDropdownReactNodeCustom component to replace the default dropdown arrowundefined
flagPropsobjectProps for customizing the country flag{}
dropDownImagePropsImagePropsProps for customizing the dropdown arrow image{}
isCallingCodeEditablebooleanControls whether the calling code can be editedtrue

Theme Properties

PropertyTypeDescriptionDefault
containerStyleStyleProp<ViewStyle>Style for the main containerundefined
textInputStyleStyleProp<TextStyle>Style for the text inputundefined
codeTextStyleStyleProp<TextStyle>Style for the calling code textundefined
flagButtonStyleStyleProp<ViewStyle>Style for the flag buttonundefined
dropDownImageStyleStyleProp<ImageStyle>Style for the dropdown arrow imageundefined
enableDarkThemebooleanEnables dark theme for the componentfalse

Utility Functions

isValidNumber(phoneNumber: string, countryCode: string): boolean

Validates a phone number for a specific country using Google's libphonenumber.

import { isValidNumber } from 'react-native-phone-entry';

const isValid = isValidNumber('+1234567890', 'US');

Dependencies

This library depends on the following packages:

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

TODO

  • Expose ref of the input
  • Add custom country picker modal

License

This project is licensed under the MIT License.


Made with create-react-native-library

Inspired by react-native-phone-number-input