-
Notifications
You must be signed in to change notification settings - Fork 197
/
PhoneInput.js
135 lines (119 loc) · 4.16 KB
/
PhoneInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React from 'react'
import PropTypes from 'prop-types'
import usePhoneDigits from './usePhoneDigits.js'
import { metadata as metadataType } from './PropTypes.js'
function PhoneInput({
Component,
country,
defaultCountry,
useNationalFormatForDefaultCountryValue = true,
value,
onChange,
metadata,
international,
withCountryCallingCode,
...rest
}, ref) {
// "Phone digits" includes not only "digits" but also a `+` sign.
const {
phoneDigits,
setPhoneDigits,
inputFormat
} = usePhoneDigits({
value,
onChange,
country,
defaultCountry,
international,
withCountryCallingCode,
useNationalFormatForDefaultCountryValue,
metadata
})
// * Passing `international` property is deprecated.
// * Passing `withCountryCallingCode` property is deprecated.
// * Passing `country` property: it should've been called `defaultCountry` instead
// because it only applies when the user inputs a phone number in a national format
// and is completely ignored when the user inputs a phone number in an international format.
return (
<Component
{...rest}
ref={ref}
metadata={metadata}
inputFormat={inputFormat}
international={international}
withCountryCallingCode={withCountryCallingCode}
country={country || defaultCountry}
value={phoneDigits}
onChange={setPhoneDigits}
/>
)
}
PhoneInput = React.forwardRef(PhoneInput)
PhoneInput.propTypes = {
/**
* The phone number (in E.164 format).
* Examples: `"+12"`, `"+12133734253"`.
* An "empty" `value` could be represented by any "falsy" value like `undefined`, `null` or an empty string `""`.
*/
value: PropTypes.string,
/**
* A function of `value: string?`.
* Updates the `value` property (to `undefined` in case it's empty).
*/
onChange: PropTypes.func.isRequired,
/**
* A two-letter country code for formatting `value`
* as a national phone number (example: `(213) 373-4253`),
* or as an international phone number without "country calling code"
* if `international` property is passed (example: `213 373 4253`).
* Example: "US".
* If no `country` is passed then `value`
* is formatted as an international phone number.
* (example: `+1 213 373 4253`)
*/
country: PropTypes.string,
/**
* A two-letter country code for formatting `value`
* when a user inputs a national phone number (example: `(213) 373-4253`).
* The user can still input a phone number in international format.
* Example: "US".
* `country` and `defaultCountry` properties are mutually exclusive.
*/
defaultCountry: PropTypes.string,
/**
* If `country` property is passed along with `international={true}` property
* then the phone number will be input in "international" format for that `country`
* (without "country calling code").
* For example, if `country="US"` property is passed to "without country select" input
* then the phone number will be input in the "national" format for `US` (`(213) 373-4253`).
* But if both `country="US"` and `international={true}` properties are passed then
* the phone number will be input in the "international" format for `US` (`213 373 4253`)
* (without "country calling code" `+1`).
*/
international: PropTypes.bool,
/**
* If `country` and `international` properties are set,
* then by default it won't include "country calling code" in the input field.
* To change that, pass `withCountryCallingCode` property,
* and it will include "country calling code" in the input field.
*/
withCountryCallingCode: PropTypes.bool,
/**
* A component that renders the `<input/>` itself and also
* parses and formats its `value` as the user inputs it.
* See `InputBasic.js` and `InputSmart.js` for an example.
*/
Component: PropTypes.elementType.isRequired,
/**
* When `defaultCountry` is defined and the initial `value` corresponds to `defaultCountry`,
* then the `value` will be formatted as a national phone number by default.
* To format the initial `value` of `defaultCountry` as an international number instead
* set `useNationalFormatForDefaultCountryValue` property to `true`.
*/
useNationalFormatForDefaultCountryValue: PropTypes.bool,
/**
* `libphonenumber-js` metadata.
*/
metadata: metadataType
}
export default PhoneInput