@@ -7,14 +7,17 @@ import {AsYouType} from "libphonenumber-js"
7
7
import Input from "@material-ui/core/Input/Input"
8
8
import WorldIcon from "@material-ui/icons/Language"
9
9
import ArrowIcon from "@material-ui/icons/ArrowDropDown"
10
- import * as _ from "lodash"
11
10
import MenuList from "@material-ui/core/MenuList/MenuList"
12
11
import Icon from "@material-ui/core/Icon/Icon"
13
12
import Grid from "@material-ui/core/Grid/Grid"
14
- import Paper from "@material-ui/core/Paper/Paper" ;
15
- import Popper from "@material-ui/core/Popper/Popper" ;
16
- import ClickAwayListener from "@material-ui/core/ClickAwayListener/ClickAwayListener" ;
17
- import MenuItem from "@material-ui/core/MenuItem/MenuItem" ;
13
+ import Paper from "@material-ui/core/Paper/Paper"
14
+ import Popper from "@material-ui/core/Popper/Popper"
15
+ import ClickAwayListener from "@material-ui/core/ClickAwayListener/ClickAwayListener"
16
+ import MenuItem from "@material-ui/core/MenuItem/MenuItem"
17
+ import withStyles from "@material-ui/core/styles/withStyles"
18
+ import { Country } from "./country"
19
+
20
+ const sortBy = require ( "lodash/sortBy" )
18
21
19
22
const styles = {
20
23
worldIcon : {
@@ -35,36 +38,39 @@ const styles = {
35
38
} ,
36
39
popper : {
37
40
zIndex : 999
38
- }
41
+ } ,
42
+ input : { marginRight : 0 } ,
43
+ textField : { paddingBottom : 0 } ,
44
+ button : { padding : 0 }
39
45
}
40
46
41
- const lookup = require ( 'country-data' ) . lookup
42
-
47
+ const lookup = require ( "country-data" ) . lookup
43
48
44
49
export interface PhoneInputProps {
45
50
phoneValueOnChange : ( a : string ) => void ,
46
51
countryValueOnChange : ( a : string ) => void ,
47
52
error : boolean ,
48
53
helperText : string
54
+ classes ?: Record < string , string >
49
55
}
50
56
51
- export default class PhoneInput extends React . Component < PhoneInputProps > {
52
-
53
- getCountries = ( ) => {
54
- const countries = lookup . countries ( { status : "assigned" } )
55
- . filter ( ( y : any ) => y . countryCallingCodes != "" )
56
- return _ . sortBy ( countries , "name" )
57
- }
57
+ function getCountries ( ) : Country [ ] {
58
+ const countries = lookup . countries ( { status : "assigned" } )
59
+ . filter ( ( y : any ) => y . countryCallingCodes != "" )
60
+ return sortBy ( countries , "name" )
61
+ }
58
62
63
+ @( withStyles ( styles ) as any)
64
+ export class PhoneInput extends React . Component < PhoneInputProps > {
59
65
state = {
60
66
code : "" ,
61
67
phone : "" ,
62
- anchorEl : null ,
63
- flag : null ,
68
+ anchorEl : null as any ,
69
+ flag : "" ,
64
70
search : "" ,
65
- allCountries : this . getCountries ( ) ,
71
+ allCountries : getCountries ( ) ,
66
72
countryCode : ""
67
- } as any ;
73
+ }
68
74
69
75
_onChange = ( e : any ) => {
70
76
const { phoneValueOnChange, countryValueOnChange} = this . props
@@ -88,16 +94,12 @@ export default class PhoneInput extends React.Component<PhoneInputProps> {
88
94
}
89
95
90
96
handleClick = ( event : any ) => {
91
- this . setState ( { anchorEl : event . currentTarget } ) ;
92
- } ;
97
+ this . setState ( { anchorEl : event . currentTarget } )
98
+ }
93
99
94
100
handleClose = ( ) => {
95
- this . setState ( { anchorEl : null } ) ;
96
- } ;
97
-
98
- handleOpen = ( ) => {
99
- this . setState ( { anchorEl : null } ) ;
100
- } ;
101
+ this . setState ( { anchorEl : null } )
102
+ }
101
103
102
104
handleClickItem = ( event : any , code : string , flag : string , countryCode : string ) => {
103
105
const phone = this . state . phone ? this . state . phone . replace ( this . state . code , code ) :
@@ -109,20 +111,27 @@ export default class PhoneInput extends React.Component<PhoneInputProps> {
109
111
phone,
110
112
flag,
111
113
countryCode
112
- } ) ;
113
- } ;
114
-
115
- handleChange = ( event : any ) => {
116
- this . setState ( { [ event . target . name ] : event . target . value } ) ;
117
- } ;
114
+ } )
115
+ }
118
116
119
117
handleSearch = ( event : any ) => {
120
- this . setState ( { search : event . target . value } ) ;
118
+ this . setState ( { search : event . target . value } )
121
119
}
122
120
121
+ renderCountry = ( country : Country ) =>
122
+ < CountryMenuItem
123
+ key = { country . name }
124
+ onClick = { event => this . handleClickItem ( event , country . countryCallingCodes [ 0 ] , country . emoji , country . alpha2 ) }
125
+ flagSVG = { country . emoji }
126
+ name = { country . name }
127
+ countryCode = { country . countryCallingCodes [ 0 ] }
128
+ search = { this . state . search }
129
+ />
130
+
123
131
render ( ) {
124
- const { error, helperText} = this . props ;
125
- const { anchorEl, search, allCountries} = this . state ;
132
+ const { error, helperText, classes : classesProp } = this . props
133
+ const { anchorEl, allCountries} = this . state
134
+ const classes = classesProp !
126
135
return < Grid container direction = { "column" } >
127
136
< Grid item >
128
137
< TextField
@@ -131,15 +140,15 @@ export default class PhoneInput extends React.Component<PhoneInputProps> {
131
140
label = { "Phone Number" }
132
141
fullWidth = { true }
133
142
value = { this . state . phone }
134
- style = { { paddingBottom : 0 } }
143
+ className = { classes . textField }
135
144
error = { error }
136
145
helperText = { helperText }
137
146
InputProps = { {
138
147
startAdornment :
139
- < InputAdornment position = "start" style = { { marginRight : 0 } } >
140
- < Button onClick = { this . handleClick } style = { { padding : 0 } } >
148
+ < InputAdornment position = "start" className = { classes . input } >
149
+ < Button onClick = { this . handleClick } className = { classes . button } >
141
150
< Grid >
142
- { this . state . flag ? < Icon > { this . state . flag } </ Icon > : < WorldIcon style = { styles . worldIcon } /> }
151
+ { this . state . flag ? < Icon > { this . state . flag } </ Icon > : < WorldIcon className = { classes . worldIcon } /> }
143
152
</ Grid >
144
153
< Grid >
145
154
< ArrowIcon />
@@ -151,23 +160,15 @@ export default class PhoneInput extends React.Component<PhoneInputProps> {
151
160
</ Grid >
152
161
153
162
< Grid item >
154
- < Popper open = { Boolean ( anchorEl ) } anchorEl = { anchorEl } placement = { "bottom-start" } style = { styles . popper } >
163
+ < Popper open = { Boolean ( anchorEl ) } anchorEl = { anchorEl } placement = { "bottom-start" } className = { classes . popper } >
155
164
< Paper >
156
165
< ClickAwayListener onClickAway = { this . handleClose } >
157
- < MenuList style = { styles . list } >
158
- < MenuItem style = { styles . hiddenInput } >
166
+ < MenuList className = { classes . list } >
167
+ < MenuItem className = { classes . hiddenInput } >
159
168
< Input onChange = { this . handleSearch } autoFocus disableUnderline
160
169
inputProps = { { padding : 0 } } />
161
170
</ MenuItem >
162
- { allCountries . map ( ( x : any ) =>
163
- < CountryMenuItem
164
- key = { x . name }
165
- onClick = { event => this . handleClickItem ( event , x . countryCallingCodes [ 0 ] , x . emoji , x . alpha2 ) }
166
- flagSVG = { x . emoji }
167
- name = { x . name }
168
- countryCode = { x . countryCallingCodes }
169
- search = { search }
170
- /> ) }
171
+ { allCountries . map ( this . renderCountry ) }
171
172
</ MenuList >
172
173
</ ClickAwayListener >
173
174
</ Paper >
0 commit comments