@fenadev/react-native-phone-input

中文文档
0.4.1发布于 2025/09/27MITTypeScriptESM + CJS近 30 天下载 4339

Simplified and optimized Phone Number Input Component for React Native with dark theme support, validation feedback, and SVG icons

原始使用文档
文档目录27 节
  1. 🚀 What's New in This Fork
  2. Autopick Feature
  3. Masking Feature
  4. Features
  5. Installation
  6. Peer Dependencies
  7. Additional Setup
  8. Usage
  9. 🎯 New API Examples
  10. With Prefix Visible (Default)
  11. Without Prefix
  12. Multiple Countries
  13. Props
  14. 🚀 New Simplified Props
  15. 📱 Core Props
  16. 🎨 Styling & Customization Props
  17. Theme Properties
  18. Utility Functions
  19. isValidNumber(phoneNumber: string, countryCode: string): bo…
  20. Dependencies
  21. Contributing
  22. TODO
  23. 🚀 Performance Improvements
  24. 📊 Migration Guide
  25. From Original react-native-phone-entry to This Fork
  26. Key Changes
  27. License

react-native-phone-input

npm version License

react-native-phone-input is a simplified and optimized Phone Number Input Component for React Native that provides an intuitive interface for entering and validating international phone numbers. This fork eliminates redundant API complexity and provides a cleaner, more maintainable solution.

🚀 What's New in This Fork

  • 🎯 Simplified API: No more redundant callingCode - it's automatically derived from countryCode
  • ⚡ Performance Optimized: O(1) country code lookups using pre-calculated maps
  • 🔧 Better Developer Experience: Cleaner props with defaultCountryCode and showPrefixCode
  • 🛡️ Enhanced Error Handling: Better validation and fallback mechanisms
  • 📱 Flexible Display: Control whether to show the calling code prefix or not

Autopick Feature

A feature that automatically picks the right country while typing the phone number.

Masking Feature

A feature that picks a right mask based on the country code.

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 with automatic adaptation
  • ✅ Visual validation feedback with check/X icons
  • 🎨 SVG icons that adapt to theme automatically
  • ♿ Accessibility support
  • 💪 Written in TypeScript
  • 🚀 Simplified API with automatic calling code derivation
  • ⚡ Performance optimized with O(1) lookups and memoization
  • 🎛️ Flexible prefix display control

Installation

npm install @fenadev/react-native-phone-input

# or

yarn add @fenadev/react-native-phone-input

Peer Dependencies

This library requires react-native-svg as a peer dependency. If you don't have it installed:

npm install react-native-svg

# or

yarn add react-native-svg

Additional Setup

For react-native-svg, follow the installation guide for your platform.

For iOS, you may need to run:

cd ios && pod install

Usage

  1. Import the PhoneInput component:
import { PhoneInput, isValidNumber } from '@fenadev/react-native-phone-input';
  1. NEW Simplified API (Recommended):
export default function App() {
  const [countryCode, setCountryCode] = useState<CountryCode>('US');
  return (
    <PhoneInput
      defaultCountryCode="CL"        // 🎯 Just specify the country
      showPrefixCode={true}          // 🎯 Control prefix display
      onChangeText={(text) =>
        console.log(
          'Phone number:',
          text,
          'isValidNumber:',
          isValidNumber(text, countryCode)
        )
      }
      onChangeCountry={(country) => {
        console.log('Country:', country);
        setCountryCode(country.cca2);
      }}
    />
  );
}
  1. Different Countries:
export default function App() {
  const [countryCode, setCountryCode] = useState<CountryCode>('US');
  return (
    <PhoneInput
      defaultCountryCode="MX"        // 🇲🇽 Mexico
      showPrefixCode={true}
      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
  defaultCountryCode="CL"           // 🎯 Simplified country selection
  showPrefixCode={true}              // 🎯 Show/hide calling code prefix
  value="+56 9 1234 5678"
  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,
    validationIconStyle: styles.validationIcon,
    enableDarkTheme: false,
  }}
  hideDropdownIcon={false}
  isCallingCodeEditable={false}
  showValidationIcon={true}
  onValidationChange={(isValid) => console.log('Phone number is valid:', isValid)}
/>

🎯 New API Examples

With Prefix Visible (Default)

<PhoneInput defaultCountryCode="CL" showPrefixCode={true} />
// Result: Input shows "+56"

Without Prefix

<PhoneInput defaultCountryCode="CL" showPrefixCode={false} />
// Result: Input shows only the local number

Multiple Countries

<PhoneInput defaultCountryCode="MX" />  // Automatically uses +52
<PhoneInput defaultCountryCode="ES" />  // Automatically uses +34
<PhoneInput defaultCountryCode="US" />  // Automatically uses +1

Props

🚀 New Simplified Props

PropTypeDescriptionDefault
defaultCountryCodeCountryCodeNEW: Default country code (e.g., 'CL', 'US', 'MX')'US'
showPrefixCodebooleanNEW: Whether to show the calling code prefix in inputtrue

📱 Core Props

PropTypeDescriptionDefault
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

🎨 Styling & Customization Props

PropTypeDescriptionDefault
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
showValidationIconbooleanShows validation icon (check/X) next to the inputfalse
isValidbooleanExternal validation state (overrides internal validation)undefined
onValidationChange(isValid: boolean) => voidCallback when validation state changesundefined

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
validationIconStyleStyleProp<ViewStyle>Style for the validation iconundefined
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 '@fenadev/react-native-phone-input';

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

🚀 Performance Improvements

This fork includes several performance optimizations:

  • O(1) Country Code Lookups: Pre-calculated maps for instant country code resolution
  • Eliminated Redundant API: No more manual callingCode specification
  • Optimized State Management: Reduced unnecessary re-renders
  • Better Error Handling: Graceful fallbacks and validation

📊 Migration Guide

From Original react-native-phone-entry to This Fork

// ❌ Original library (redundant)
<PhoneInput
  defaultValues={{
    countryCode: 'CL',
    callingCode: '+56',    // Redundant!
    phoneNumber: '+56',    // Redundant!
  }}
/>

// ✅ This fork (simplified)
<PhoneInput
  defaultCountryCode="CL"  // Calling code derived automatically
  showPrefixCode={true}     // Control prefix display
/>

Key Changes

  • Eliminated: defaultValues.callingCode - now automatically derived
  • Eliminated: defaultValues.phoneNumber - use value prop instead
  • Added: defaultCountryCode - cleaner country selection
  • Added: showPrefixCode - control prefix visibility

License

This project is licensed under the MIT License.


Fork of: react-native-phone-entry by anday013

Made with create-react-native-library

Inspired by react-native-phone-number-input

版本与发布记录

@fenadev/react-native-phone-input 共发布 6 个版本,首次发布于 2025/09/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.1latest2025/09/271.1 年前
0.4.02025/09/271.1 年前
0.3.32025/09/261.1 年前
0.3.22025/09/261.1 年前
0.3.12025/09/261.1 年前
0.3.02025/09/261.1 年前

关于 @fenadev/react-native-phone-input 的常见问题

@fenadev/react-native-phone-input 是什么?

Simplified and optimized Phone Number Input Component for React Native with dark theme support, validation feedback, and SVG icons

如何安装 @fenadev/react-native-phone-input?

在项目目录运行 npm install @fenadev/react-native-phone-input,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)、react-native-svg(>=13.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fenadev/react-native-phone-input 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fenadev/react-native-phone-input 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fenadev/react-native-phone-input 可以商用吗?

@fenadev/react-native-phone-input 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fenadev/react-native-phone-input 的下载量有多少?

根据 npm 官方下载统计,@fenadev/react-native-phone-input 在 2026/10/15 – 2026/11/13 这 30 天内被下载 4,339 次,日均约 145 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fenadev/react-native-phone-input 还在维护吗?

最新版本 0.4.1 发布于 2025/09/27(1.1 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。