https://github.com/jaebradley/phone-number-prop-type
Phone Number React Prop Type
https://github.com/jaebradley/phone-number-prop-type
phone-number proptype-validators proptypes react
Last synced: about 1 month ago
JSON representation
Phone Number React Prop Type
- Host: GitHub
- URL: https://github.com/jaebradley/phone-number-prop-type
- Owner: jaebradley
- License: mit
- Created: 2017-12-16T00:09:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T15:41:15.000Z (over 3 years ago)
- Last Synced: 2025-02-14T17:42:16.890Z (over 1 year ago)
- Topics: phone-number, proptype-validators, proptypes, react
- Language: JavaScript
- Size: 977 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/jaebradley/phone-number-prop-type)
[](https://codecov.io/gh/jaebradley/phone-number-prop-type)
[](https://www.npmjs.com/package/phone-number-prop-type)
[](https://www.npmjs.com/package/phone-number-prop-type)
# Phone Number Prop Type
## Why are `Prop Types` important?
[`Prop Types`](https://reactjs.org/docs/typechecking-with-proptypes.html) kind've [serve as the interface layer for your component](https://wecodetheweb.com/2015/06/02/why-react-proptypes-are-important/) - what data does my component depend on and what is the expected format of that data?
Thus, defining and validating props accurately can add value from a readability and debugging point of view.
## What this package does
This package is used to validate if a React prop value is a valid phone number. Currently, there is no phone number prop type defined by [the `prop-types` package](https://www.npmjs.com/package/prop-types). While using `PropType.string` works, why not be as specific as possible when validating your props?
Additionally, though [it's relatively straightforward to create a custom prop type validator](https://www.ian-thomas.net/custom-proptype-validation-with-react/), if you need to implement similar prop type checking in multiple packages, you might not want to repeat yourself.
This package depends on the [`google-libphonenumber`](https://www.npmjs.com/package/google-libphonenumber) package. `google-libphonenumber` **will only validate [`E.164` formatted](https://en.wikipedia.org/wiki/E.164)) phone numbers**.
## Installation
```bash
npm install phone-number-prop-type --save
```
## Usage
```javascript
import React from 'react';
import PropTypes from 'prop-types';
import phoneNumberPropType from 'phone-number-prop-type';
const PhoneNumber = ({ phoneNumber }) =>
Here is my phone number: {phoneNumber}!
;
PhoneNumber.defaultProps = {
phoneNumber: '+1 555-555-555',
}
PhoneNumber.propTypes = {
phoneNumber: phoneNumberPropType,
}
export default PhoneNumber;
```
[`stackblitz` Example](https://stackblitz.com/edit/react-phone-number-prop-type-1?embed=1&file=index.js&view=editor)