Created
July 26, 2019 11:26
-
-
Save veb/ca6b60ad6742fda8422b78d961200a30 to your computer and use it in GitHub Desktop.
Phone Input with formatter - see https://veb.co.nz/code/react/2019/07/25/react-native-phone-input.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react' | |
import { formatNumber } from "libphonenumber-js"; | |
import { Image, View, TextInput } from 'react-native'; | |
const App = ({ placeholder, onChange, confirm }) => { | |
const [phoneNumber, setPhoneNumber] = useState(''); | |
useEffect(() => { | |
let tempPhone = phoneNumber.replace(/\+/g, ''); | |
let removeSpaces = tempPhone.replace(/ /g, ''); | |
onChange(removeSpaces) | |
}, [phoneNumber, onChange]) | |
const onBlur = () => { | |
const formattedNumber = formatNumber(`+64${phoneNumber.replace(/^\+64/, "")}`, "International") | |
setPhoneNumber(formattedNumber); | |
}; | |
return ( | |
<View row> | |
<Image | |
source={require('app/images/flags/nz.png')} | |
style= | |
/> | |
<TextInput | |
onBlur={onBlur} | |
onChangeText={setPhoneNumber} | |
placeholder={placeholder} | |
hideUnderline | |
value={phoneNumber} | |
keyboardType={'phone-pad'} /> | |
</View> | |
) | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment