@@ -2,9 +2,11 @@ import React from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import { omit } from 'lodash' ;
4
4
5
- import { Form , Input , Message , Dropdown , Flag } from 'semantic' ;
5
+ import { Input , Dropdown , Flag } from 'semantic' ;
6
6
7
- import { COUNTRIES , getFormatLength , formatPhone } from 'utils/phone' ;
7
+ import { COUNTRIES , formatPhone } from 'utils/phone' ;
8
+
9
+ import Field from './Field' ;
8
10
9
11
export default class PhoneField extends React . Component {
10
12
constructor ( props ) {
@@ -27,17 +29,14 @@ export default class PhoneField extends React.Component {
27
29
}
28
30
29
31
onChange = ( evt , { value, ...rest } ) => {
30
- const { country } = this . state ;
31
- const maxLength = getFormatLength ( country ) ;
32
-
33
32
value = value . replace ( / [ ( ) @ . + - ] / g, '' ) ;
34
- value = value . replace ( / ^ 1 ( \d ) / , '$1' ) ;
35
- value = value . replace ( / [ ^ \d - ] / gi, '' ) ;
33
+ value = value . replace ( / ^ [ 0 1 ] ( \d ) / , '$1' ) ;
34
+ value = value . replace ( / [ a - z ] / gi, '' ) ;
36
35
value = value . trim ( ) ;
37
- value = value . slice ( 0 , maxLength ) ;
38
-
39
36
if ( value ) {
40
37
value = `${ this . getPrefix ( ) } ${ value } ` ;
38
+ } else {
39
+ value = undefined ;
41
40
}
42
41
this . props . onChange ( evt , {
43
42
...rest ,
@@ -46,24 +45,21 @@ export default class PhoneField extends React.Component {
46
45
} ;
47
46
48
47
render ( ) {
49
- const { required, label, error } = this . props ;
48
+ const { label } = this . props ;
49
+ const props = omit ( this . props , Object . keys ( PhoneField . propTypes ) ) ;
50
50
return (
51
- < Form . Field
52
- required = { required }
53
- disabled = { this . props . disabled }
54
- error = { error ?. hasField ?. ( 'phone' ) } >
51
+ < Field { ...props } >
55
52
{ label && < label > { label } </ label > }
56
53
< Input
57
- { ...omit ( this . props , Object . keys ( PhoneField . propTypes ) ) }
54
+ { ...Field . getInnerProps ( props ) }
58
55
{ ...this . renderLabelProps ( ) }
59
56
type = "tel"
60
57
autoComplete = "tel"
61
58
value = { this . renderFormattedValue ( ) }
62
59
onChange = { this . onChange }
63
60
ref = { this . ref }
64
61
/>
65
- { this . renderFieldErrors ( ) }
66
- </ Form . Field >
62
+ </ Field >
67
63
) ;
68
64
}
69
65
@@ -115,24 +111,6 @@ export default class PhoneField extends React.Component {
115
111
const country = COUNTRIES [ this . state . country ] ;
116
112
return formatPhone ( value , country ) ;
117
113
}
118
-
119
- renderFieldErrors ( ) {
120
- const { error } = this . props ;
121
- const details = error ?. getFieldDetails ?. ( 'phone' ) ;
122
- if ( details ) {
123
- return (
124
- < React . Fragment >
125
- < Message size = "small" error >
126
- < Message . Content >
127
- { details . map ( ( d , i ) => {
128
- return < div key = { i } > { d . message } </ div > ;
129
- } ) }
130
- </ Message . Content >
131
- </ Message >
132
- </ React . Fragment >
133
- ) ;
134
- }
135
- }
136
114
}
137
115
138
116
PhoneField . propTypes = {
@@ -147,5 +125,4 @@ PhoneField.defaultProps = {
147
125
intl : false ,
148
126
icon : 'phone' ,
149
127
country : 'us' ,
150
- placeholder : 'Phone Number' ,
151
128
} ;
0 commit comments