1.0.2 โ€ข Published 4 months ago

react-native-dialer-code-picker v1.0.2

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

react-native-dialer-code-picker โšกโšกโšก

This library offers a multi-language country dialer code picker with advanced search functionality, delivering a smooth and high-performance user experience. Designed to be fully cross-platform, it supports React Native and Expo out of the box.

๐Ÿš€ Built for Performance: Enhanced with optimized rendering and efficient animations for seamless navigation using FlashList.
๐Ÿž Issue-Free Experience: Developed with careful attention to detail, addressing common issues found in similar libraries.
๐Ÿ”„ Flexible & Customizable: Easily adaptable to your design needs with custom templates and styling options.

Inspired by the popular react-native-country-codes-picker, but enhanced with better performance and stability. If you're looking for a modern alternative with optimized rendering and customization capabilities, this is the picker for you.

Looking for a specific country or locale? Feel free to contribute with a PR. โšกโšกโšก


๐Ÿš€ Installation

You can install it using npm or yarn:

# Using npm
npm install react-native-dialer-code-picker @shopify/flash-list

# Using yarn
yarn add react-native-dialer-code-picker @shopify/flash-list

Note: This library uses FlashList from @shopify/flash-list for improved performance, so make sure to install it as a dependency.


โš™๏ธ Basic Usage

Using DialerPicker (Built-in Modal)

The DialerPicker component includes a built-in modal, so you can directly use it without handling modal logic manually.

import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import { DialerPicker } from 'react-native-dialer-code-picker';

const App = () => {
  const [isVisible, setIsVisible] = useState(false);
  const [selectedDialer, setSelectedDialer] = useState('');

  const handleDialerSelect = (item) => {
    setSelectedDialer(item.dial_code);
    setIsVisible(false);
  };

  return (
    <View>
      <Button title="Select Dialer Code" onPress={() => setIsVisible(true)} />
      <Text>Selected Dialer Code: {selectedDialer}</Text>
      <DialerPicker
        isVisible={isVisible}
        onDialCodeSelect={handleDialerSelect}
        onClose={() => setIsVisible(false)}
        searchPlaceholder="Search by country or code"
        lang="en"
      />
    </View>
  );
};

export default App;

๐Ÿ’ก Advanced Usage

Using DialerList (Custom Modal or Bottom Sheet)

If you want to use your own modal (e.g., BottomSheetModal), you can import DialerList and handle the modal separately.

import React, { useState, useCallback } from 'react';
import { View, Button } from 'react-native';
import { DialerList } from 'react-native-dialer-code-picker';
import {
  BottomSheetModal,
  BottomSheetModalProvider,
} from '@gorhom/bottom-sheet';

const BottomSheetDialer = () => {
  const [bottomSheetRef, setBottomSheetRef] = useState(null);

  const openSheet = useCallback(() => {
    bottomSheetRef?.present();
  }, [bottomSheetRef]);

  return (
    <BottomSheetModalProvider>
      <View>
        <Button title="Open Dialer" onPress={openSheet} />
        <BottomSheetModal
          ref={setBottomSheetRef}
          index={0}
          snapPoints={['50%']}
        >
          <DialerList
            onDialCodeSelect={(item) => {
              console.log(item.dial_code);
              bottomSheetRef?.dismiss();
            }}
            lang="en"
          />
        </BottomSheetModal>
      </View>
    </BottomSheetModalProvider>
  );
};

Comparison: DialerPicker vs. DialerList

FeatureDialerPicker (Built-in Modal)DialerList (Standalone)
Includes a modal?โœ… YesโŒ No
Manages its own state?โœ… YesโŒ No
Custom modal support?โŒ No, uses default modalโœ… Yes
Ideal for...?Quick implementationFull customization

๐Ÿ“š Props and API Details

DialerPicker Props

PropTypeDescriptionRequiredDefault
isVisiblebooleanControls the visibility of the modal.โœ…false
enableModalAvoidingbooleanEnables keyboard avoiding behavior on Android.โŒfalse
disableBackdropbooleanIf true, disables the backdrop behind the modal.โŒfalse
androidWindowSoftInputModestringDefines keyboard behavior on Android (e.g., "pan").โŒ-
searchPlaceholderstringPlaceholder text for the search input.โŒ"Search..."
searchPlaceholderTextColorstringColor of the placeholder text in the search input field.โŒ-
searchNotFoundMessagestringMessage displayed when no results are found.โŒ-
langstringSelected language for country names.โœ…"en"
defaultDialCodestringDefault dial code to be pre-selected.โŒ-
otherCountriesHeaderTitlestringTitle for the "Other Countries" section.โŒ-
searchContainerStyleStyleProp<ViewStyle>Custom styles for the search input container.โŒ-
excludedCountriesstring[]List of country codes to exclude from the picker.โŒ[]
showOnlystring[]List of country codes to exclusively show.โŒ[]
popularCountriesstring[]List of popular countries to show at the top.โŒ[]
onDialCodeSelect(item: DialerCode) => voidCallback when a dialer code is selected.โœ…-
onBackdropPress() => voidCallback when the backdrop is pressed.โŒ-
onClose() => voidCallback when the modal is closed.โŒ-
itemTemplate(props: DialerItemTemplateProps) => JSX.ElementCustom template to render each item.โŒDialerButton
headerComponent(props: DialerListHeaderComponentProps) => JSX.ElementCustom component for the list header.โŒ-
styleDialerStyleStyle object to customize the picker.โŒ-
showVerticalScrollIndicatorbooleanWhether to show the vertical scroll indicator. Default is false.โŒfalse

DialerList Props

PropTypeDescriptionRequiredDefault
excludedCountriesstring[]List of country codes to exclude.โŒ[]
showOnlystring[]List of country codes to exclusively show.โŒ[]
popularCountriesstring[]List of popular country codes displayed at the top.โŒ[]
onDialCodeSelect(item: DialerCode) => voidCallback triggered when a dial code is selected.โœ…-
langstringLanguage code for country names.โœ…"en"
headerComponent(props: DialerListHeaderComponentProps) => JSX.ElementCustom component for the list header.โŒ-
itemTemplate(props: DialerItemTemplateProps) => JSX.ElementCustom component for rendering each item.โŒDialerButton
styleDialerStyleStyle object to customize the component.โŒ-
searchContainerStyleStyleProp<ViewStyle>Custom styles for the search container.โŒ-
showVerticalScrollIndicatorbooleanWhether to show the vertical scroll indicator. Default is false.โŒfalse
searchValuestringText input for filtering countries by name or dial code.โŒ-

๐Ÿ“ License

This project is licensed under the MIT License.
See the LICENSE file for more details.


๐Ÿค Contributions

Contributions are welcome!
If you find a bug or want to add a new feature, please open an Issue or a Pull Request.