1- import { countries as countryList } from 'countries-list' ;
21import * as React from 'react' ;
32import { FlatList , TextInput as RNTextInput , View } from 'react-native' ;
43import { DeepPartial , Omit } from 'ts-essentials' ;
54
5+ import { TextInput , TextInputProps } from '.' ;
66import { useTheme } from '../../theme' ;
77import { mergeStyles , ReplaceReturnType } from '../../utils/mergeStyles' ;
88import { Button } from '../Button' ;
@@ -13,7 +13,6 @@ import {
1313 getPhoneNumberInputStyles ,
1414 PhoneNumberInputStyles ,
1515} from './PhoneNumberInput.styles' ;
16- import { TextInput , TextInputProps } from './TextInput' ;
1716import { GetTextInputStyles , TextInputStyles } from './TextInput.styles' ;
1817
1918export interface PhoneNumberInputProps
@@ -30,26 +29,29 @@ export interface PhoneNumberInputProps
3029 GetTextInputStyles ,
3130 DeepPartial < TextInputStyles & PhoneNumberInputStyles >
3231 > ;
32+ countryCodes ?: CountryCode [ ] ;
33+ getCountryCodeTitle ?: ( countryCode : string ) => string ;
3334}
3435
35- const countries = ( ( ) => {
36- return Object . keys ( countryList ) . map ( countryCode => ( {
37- countryCode,
38- key : countryCode ,
39- ...countryList [ countryCode ] ,
40- } ) ) ;
41- } ) ( ) ;
36+ export interface CountryCode {
37+ /** The value for countryCode */
38+ value : string ;
39+ /** Labels used in the list of countries to select the country code from */
40+ label : string ;
41+ }
4242
4343const PhoneNumberInputBase = ( props : PhoneNumberInputProps ) => {
4444 const {
45- countryCode = 'US' ,
45+ countryCode = '1' ,
46+ countryCodes = [ ] ,
4647 phoneNumber,
4748 onChangeCountryCode,
4849 onChangePhoneNumber,
4950 header,
5051 getStyles,
5152 innerRef,
5253 useHistory = false ,
54+ getCountryCodeTitle = ( code : string ) => code ,
5355 ...textInputProps
5456 } = props ;
5557 const [ isModalOpen , setIsModalOpen ] = React . useState ( false ) ;
@@ -80,7 +82,24 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
8082 name = "chevron-down"
8183 />
8284 }
83- title = { `+${ countryList [ countryCode ] . phone } ` }
85+ title = { getCountryCodeTitle ( countryCode ) }
86+ />
87+ < TextInput
88+ ref = { innerRef }
89+ name = "phone"
90+ getStyles = { ( ) => ( {
91+ containerStyle : {
92+ flex : 1 ,
93+ } ,
94+ inputStyle : {
95+ borderBottomLeftRadius : 0 ,
96+ borderTopLeftRadius : 0 ,
97+ } ,
98+ } ) }
99+ keyboardType = "phone-pad"
100+ value = { phoneNumber }
101+ onChangeText = { onChangePhoneNumber }
102+ { ...textInputProps }
84103 />
85104 < CloseableModal
86105 visible = { isModalOpen }
@@ -90,22 +109,22 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
90109 >
91110 < FlatList
92111 ListHeaderComponent = { header }
93- keyExtractor = { item => item . key }
112+ keyExtractor = { item => item . label }
94113 getItemLayout = { ( data , index ) => ( {
95114 index,
96115 length : theme . controlHeights . medium ,
97116 offset : theme . controlHeights . medium * index ,
98117 } ) }
99- data = { countries }
100- renderItem = { ( { item : country } ) => {
118+ data = { countryCodes }
119+ renderItem = { ( { item } ) => {
101120 return (
102121 < ListItem
103- key = { country . countryCode }
104- label = { country . name }
122+ key = { item . label }
123+ label = { item . label }
105124 onPress = { event => {
106125 event . preventDefault ( ) ;
107126 if ( onChangeCountryCode ) {
108- onChangeCountryCode ( country . countryCode ) ;
127+ onChangeCountryCode ( item . value ) ;
109128 }
110129 setIsModalOpen ( false ) ;
111130 } }
@@ -114,24 +133,11 @@ const PhoneNumberInputBase = (props: PhoneNumberInputProps) => {
114133 } }
115134 />
116135 </ CloseableModal >
117- < TextInput
118- ref = { innerRef }
119- name = "phone"
120- getStyles = { ( ) => ( {
121- inputStyle : {
122- borderBottomLeftRadius : 0 ,
123- borderTopLeftRadius : 0 ,
124- } ,
125- } ) }
126- keyboardType = "phone-pad"
127- value = { phoneNumber }
128- onChangeText = { onChangePhoneNumber }
129- { ...textInputProps }
130- />
131136 </ View >
132137 ) ;
133138} ;
134139
135- export const PhoneNumberInput = React . forwardRef < RNTextInput , TextInputProps > (
136- ( props , ref ) => < PhoneNumberInputBase { ...props } innerRef = { ref } /> ,
137- ) ;
140+ export const PhoneNumberInput = React . forwardRef <
141+ RNTextInput ,
142+ PhoneNumberInputProps
143+ > ( ( props , ref ) => < PhoneNumberInputBase { ...props } innerRef = { ref } /> ) ;
0 commit comments