New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mui-phone-input

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mui-phone-input

Advanced, highly customizable phone input component for Material UI.

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
442
decreased by-3.28%
Maintainers
0
Weekly downloads
 
Created
Source

MUI Phone Input

npm Playground distro types License Tests

Advanced phone input component for Material UI that leverages the react-phone-hooks supporting all countries. The package is compatible with @material-ui/core, @mui/material, @mui/base and @mui/joy distributions. It provides built-in support for area codes and strict validation.

MUI Phone Input

Value

The value of the component is an object containing the parts of the phone number. This format of value gives a wide range of opportunities for handling the data in your desired way.

{
  countryCode: 1,
  areaCode: "702",
  phoneNumber: "1234567",
  isoCode: "us",
  valid: function valid(strict)
}

Validation

The validation is checked by the valid function of the value object that returns a boolean value. An example with the react-hook-form is shown below:

import {useMemo, useState} from "react";
import {useForm} from "react-hook-form";
import PhoneInput from "mui-phone-input";
import {checkValidity, parsePhoneNumber} from "react-phone-hooks";

const Demo = () => {
  const {register, handleSubmit} = useForm();
  const [value, setValue] = useState({});

  const phoneProps = register("phone", {
    validate: (value: any) => checkValidity(parsePhoneNumber(value)),
  })

  const onChange = async (value: any) => {
    await phoneProps.onChange({target: {value, name: phoneProps.name}});
    setValue(value);
  }

  const error = useMemo(() => value.valid && !value.valid(), [value]);

  return (
    <form onSubmit={handleSubmit(console.log)}>
      <PhoneInput
        enableSearch
        error={error}
        {...phoneProps}
        variant="filled"
        onChange={onChange}
        searchVariant="standard"
      />
      <button type="submit">Submit</button>
    </form>
  )
}

export default Demo;

The valid function primarily checks if a phone number has a length appropriate for its specified country. In addition, a more comprehensive validation can be performed, including verifying the dial and area codes' accuracy for the selected country. To activate the strict validation, pass true as the first argument to the valid function.

Localization

The package provides a built-in localization feature that allows you to change the language of the component. The locale function returns the language object that can be passed to the createTheme function of Material UI.

import {createTheme, ThemeProvider} from "@mui/material/styles";
import PhoneInput, {locale} from "mui-phone-input";

const theme = createTheme({}, locale("frFR"));

<ThemeProvider theme={theme}>
  <PhoneInput/>
</ThemeProvider>

NOTE: If you use localization in the documented way, you should replace the language object with the locale function, specifying the desired language code.

Props

Apart from the phone-specific properties described below, all Input and TextField properties supported by the used Material distribution can be applied to the phone input component.

PropertyDescriptionType
valueAn object containing a parsed phone number or the raw number.object / string
countryCountry code to be selected by default. By default, it will show the flag of the user's country.string
enableArrowShows an arrow next to the country flag. Default value is false.boolean
enableSearchEnables search in the country selection dropdown menu. Default value is false.boolean
searchVariantAccepts an Input variant, and values depend on the chosen Material distribution.TextFieldVariants
searchNotFoundThe value is shown if enableSearch is true and the query does not match any country. Default value is No country found.string
searchPlaceholderThe value is shown if enableSearch is true. Default value is Search country.string
disableDropdownDisables the manual country selection through the dropdown menu.boolean
disableParenthesesDisables parentheses from the input masks. Default enabled.boolean
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[]
onChangeThe only difference from the default onChange is that value comes first.function(value, event)
onMountThe callback is triggered once the component gets mounted.function(value)

Contribute

Any contribution is welcome. Don't hesitate to open an issue or discussion if you have questions about your project's usage and integration. For ideas or suggestions, please open a pull request. Your name will shine on our contributors' list. Be proud of what you build!

License

Copyright (C) 2023 Artyom Vancyan. MIT

Keywords

FAQs

Package last updated on 16 Nov 2024

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