Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

antd-phone-input

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

antd-phone-input

Advanced, highly customizable phone input component for Ant Design.

  • 0.1.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.7K
increased by62.67%
Maintainers
1
Weekly downloads
 
Created
Source

Antd Phone Input

npm antd types License PRs Welcome Tests

Advanced phone input component for Ant Design that provides support for all countries and is compatible with antd 4 and 5 versions. It has built-in support for area codes and provides validation to ensure that the entered numbers are valid. This open-source project is designed to simplify the process of collecting phone numbers from users.

Installation

npm i antd-phone-input
yarn add antd-phone-input

Usage

The latest version does not require any additional actions for loading the styles as it uses the cssinjs ecosystem.

Antd 5.x

import React from "react";
import FormItem from "antd/es/form/FormItem";
import PhoneInput from "antd-phone-input";

const Demo = () => {
  return (
    <FormItem name="phone">
      <PhoneInput enableSearch/>
    </FormItem>
  )
}

latest

Antd 4.x

For 4.x versions, you should use the legacy endpoint.

import PhoneInput from "antd-phone-input/legacy";

For including the styles, you should import them in the main less file after importing either the antd/dist/antd.less or antd/dist/antd.dark.less styles.

@import "~antd/dist/antd";
+ @import "~antd-phone-input/legacy/style";

legacy

Value

The value of the component is an object containing the parts of a phone number. This format of value gives a wide range of opportunities for handling the data in your custom way. For example, you can easily merge the parts of the phone number into a single string.

{
  "countryCode": 1,
  "areaCode": 702,
  "phoneNumber": "1234567",
  "isoCode": "us",
  "valid": true
}

Validation

The valid property of the value object shows the real-time validity of the phone number depending on the country. So this can be used in a validator like this:

const validator = (_, {valid}) => {
  if (valid) {
    return Promise.resolve();
  }
  return Promise.reject("Invalid phone number");
}

return (
  <FormItem rules={[{validator}]}>
    <PhoneInput/>
  </FormItem>
)

Props

PropertyDescriptionType
sizeEither large, middle or small. Default value is middle. See at ant docs for more.string
valueAn object containing the parts of phone number. E.g. value={{countryCode: 1, areaCode: 702, phoneNumber: "1234567"}}.object
styleApplies CSS styles to the container element.CSSProperties
classNameThe value will be assigned to the container element.string
disabledDisables the whole input component.boolean
enableSearchEnables search in the country selection dropdown menu. Default value is false.boolean
disableDropdownDisables the manual country selection through the dropdown menu.boolean
inputPropsHTML properties of input to pass into the input. E.g. inputProps={{autoFocus: true}}.InputHTMLAttributes
searchPlaceholderThe value is shown if enableSearch is true. Default value is search.string
searchNotFoundThe value is shown if enableSearch is true and the query does not match any country. Default value is No entries to show.string
placeholderCustom placeholder. Default placeholder is 1 (702) 123-4567.string
countryCountry code to be selected by default. By default, it will show the flag of the user's country.string
regionsShow only the countries of the specified regions. See the list of available regions.string[]
onlyCountriesCountry codes to be included in the list. E.g. onlyCountries={['us', 'ca', 'uk']}.string[]
excludeCountriesCountry codes to be excluded from the list of countries. E.g. excludeCountries={['us', 'ca', 'uk']}.string[]
preferredCountriesCountry codes to be at the top of the list. E.g. preferredCountries={['us', 'ca', 'uk']}.string[]
onChangeCallback when the user is inputting. See at ant docs for more.function(value, e)
onPressEnterThe callback function that is triggered when Enter key is pressed.function(e)
onFocusThe callback is triggered when the input element is focused.function(e, value)
onClickThe callback is triggered when the user clicks on the input element.function(e, value)
onBlurThe callback is triggered when the input element gets blurred or unfocused.function(e, value)
onKeyDownThe callback is triggered when any key is pressed down.function(e)
onMountThe callback is triggered once the component gets mounted.function(e)

Contribute

Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And don't forget to add tests for your changes.

License

Copyright (C) 2023 Artyom Vancyan. MIT

Keywords

FAQs

Package last updated on 13 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc