Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rn-phone-input-field

sohantalukder742MIT1.1.9TypeScript support: included

A React Native phone number input component built from scratch, featuring a text input for number entry, a custom dropdown for selecting country codes, and validation logic using regex or country-specific rules. It supports formatting, localization, and s

react-native, ios, android, phone, input, dropdown, phone-number-input, phone-number-dropdown, react-native-phone-input, react-native-phone-number-input, react-native-phone-number-dropdown, react-native-phone-input-field, react-native-phone-number-input-field, react-native-phone-input-dropdown, react-native-phone-number-input-dropdown, react-native-phone-input-component, react-native-phone-number-input-component, rn-phone-number-field

readme

rn-phone-input-field

npm version License: MIT Downloads

PhoneInput

🚀 Ultra-Lightweight Phone Input Component

Zero Dependencies. Maximum Performance.

rn-phone-input-field is a high-performance React Native phone input component engineered for production applications that demand both functionality and minimal bundle impact. Built entirely without external dependencies, it delivers enterprise-grade features while maintaining an exceptionally small footprint.

✨ Key Features

  • 🪶 Zero Dependencies: Completely self-contained with no external packages
  • ⚡ Ultra-Lightweight: Minimal bundle size impact on your application
  • 🌍 Comprehensive Country Support: Built-in country code database
  • ✅ Smart Validation: Real-time phone number format validation
  • 🎨 Fully Customizable: Extensive styling and configuration options
  • 📱 React Native Optimized: Native performance and platform consistency
  • 🔧 TypeScript Ready: Complete type definitions included

Why Choose rn-phone-input-field?

In an ecosystem where dependencies can quickly bloat your application, rn-phone-input-field stands out by providing full functionality without any external packages. This results in:

  • Faster app startup times
  • Reduced bundle size
  • Fewer security vulnerabilities
  • Simplified dependency management
  • Better long-term maintainability

Installation

# npm
npm install rn-phone-input-field

# yarn
yarn add rn-phone-input-field

# pnpm
pnpm add rn-phone-input-field

Quick Start

import React, { useRef } from 'react';
import { View } from 'react-native';
import { PhoneInput, PhoneInputRef } from 'rn-phone-input-field';

const App = () => {
  const phoneInputRef = useRef<PhoneInputRef>(null);

  const handlePhoneChange = (phoneNumber: string) => {
    console.log('Phone number:', phoneNumber);

    // Validate the number
    const isValid = phoneInputRef.current?.isValidNumber(phoneNumber);
    console.log('Is valid:', isValid);
  };

  return (
    <View style={{ padding: 20 }}>
      <PhoneInput
        ref={phoneInputRef}
        placeholder="Enter phone number"
        defaultCountry="US"
        onChangeText={handlePhoneChange}
        onSelectCountryCode={(country) => {
          console.log('Selected country:', country.name);
        }}
      />
    </View>
  );
};

export default App;

API Reference

PhoneInputProps

Property Type Default Description
onChangeText (value: string) => void - Callback fired when input value changes
onSelectCountryCode (country: CountryInfo) => void - Callback fired when country is selected
defaultCountry CountryCode "US" Initial country code
defaultValue string "" Initial input value
placeholder string - Input placeholder text
placeholderColor ColorValue "#999" Placeholder text color
containerStyle StyleProp<ViewStyle> - Container styling
textInputStyle StyleProp<TextStyle> - Text input styling
codeTextStyle StyleProp<TextStyle> - Country code text styling
iconContainerStyle StyleProp<ViewStyle> - Dropdown icon container styling
downArrowIcon React.ReactNode - Custom dropdown arrow component
inputProps TextInputProps - Additional TextInput props
searchInputProps TextInputProps - Country search input props

PhoneInputRef Methods

Method Type Description
isValidNumber (phoneNumber: string) => boolean Validates phone number format
onChangeText (value: string) => void Programmatically update input value
defaultCountry (code: CountryCode) => void Change default country
defaultValue (text: string) => void Set default input value

Advanced Usage

Custom Styling

<PhoneInput
  containerStyle={{
    borderWidth: 1,
    borderColor: '#e1e5e9',
    borderRadius: 8,
    paddingHorizontal: 12,
  }}
  textInputStyle={{
    fontSize: 16,
    color: '#2c3e50',
  }}
  codeTextStyle={{
    fontSize: 16,
    fontWeight: '600',
    color: '#3498db',
  }}
  placeholderColor="#95a5a6"
/>

Form Integration

import { Formik } from 'formik';
import * as Yup from 'yup';

const validationSchema = Yup.object({
  phone: Yup.string().required('Phone number is required'),
});

const ContactForm = () => {
  const phoneInputRef = useRef<PhoneInputRef>(null);

  return (
    <Formik
      initialValues={{ phone: '' }}
      validationSchema={validationSchema}
      onSubmit={(values) => {
        const isValid = phoneInputRef.current?.isValidNumber(values.phone);
        if (isValid) {
          console.log('Valid phone:', values.phone);
        }
      }}
    >
      {({ handleSubmit, setFieldValue, values }) => (
        <PhoneInput
          ref={phoneInputRef}
          defaultValue={values.phone}
          onChangeText={(phone) => setFieldValue('phone', phone)}
        />
      )}
    </Formik>
  );
};

Performance Metrics

Metric rn-phone-input-field Typical Alternatives
Bundle Size ~15KB ~1-3MB
Dependencies 0 3-8 packages
Install Time Fast Slower
Security Audit Clean Potential vulnerabilities

TypeScript Support

Full TypeScript support is included out of the box with comprehensive type definitions for all props and methods.

import { PhoneInput, PhoneInputRef, CountryCode } from 'rn-phone-input-field';

Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support


Built with ❤️ for the React Native community

⭐ Star this repo if you found it helpful!

changelog

Changelog

All notable changes to this project will be documented in this file.

[Unreleased] - 2024-05-26

Added

  • Added ESLint and Prettier configurations for improved code quality
  • Added GitHub templates for issues and pull requests
  • Added TypeScript configuration (tsconfig.json)
  • Added PhoneInput component and CountryCode type to the index exports

Changed

  • Enhanced .gitignore file with better patterns
  • Updated package.json with improved development dependencies
  • Refactored country picker modal and picker components
  • Improved type definitions across the codebase
  • Updated iOS project configuration

Fixed

  • Fixed issues in country picker modal
  • Fixed various UI and functionality issues in the picker component
  • Fixed package versioning

Technical Details

  • Major refactoring of the codebase structure
  • Improved type safety with enhanced TypeScript definitions
  • Updated dependencies and development tools
  • Enhanced project configuration for better development experience