Created
April 21, 2020 06:10
-
-
Save amirhayati/5ee7d2f17d40574ce2222dc4da644db6 to your computer and use it in GitHub Desktop.
This file contains 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} from 'react'; | |
import {SafeAreaView, Text, StyleSheet} from 'react-native'; | |
import { | |
CodeField, | |
Cursor, | |
useBlurOnFulfill, | |
useClearByFocusCell, | |
} from 'react-native-confirmation-code-field'; | |
const CELL_COUNT = 6; | |
const App = () => { | |
const [value, setValue] = useState(''); | |
const ref = useBlurOnFulfill({value, cellCount: CELL_COUNT}); | |
const [props, getCellOnLayoutHandler] = useClearByFocusCell({ | |
value, | |
setValue, | |
}); | |
return ( | |
<SafeAreaView style={styles.root}> | |
<Text style={styles.title}>Verification</Text> | |
<CodeField | |
ref={ref} | |
{...props} | |
value={value} | |
onChangeText={setValue} | |
cellCount={CELL_COUNT} | |
rootStyle={styles.codeFiledRoot} | |
keyboardType="number-pad" | |
renderCell={({index, symbol, isFocused}) => ( | |
<Text | |
key={index} | |
style={[styles.cell, isFocused && styles.focusCell]} | |
onLayout={getCellOnLayoutHandler(index)}> | |
{symbol || (isFocused ? <Cursor /> : null)} | |
</Text> | |
)} | |
/> | |
</SafeAreaView> | |
); | |
}; | |
const styles = StyleSheet.create({ | |
root: {flex: 1, padding: 20}, | |
title: {textAlign: 'center', fontSize: 30}, | |
codeFiledRoot: {marginTop: 20}, | |
cell: { | |
width: 40, | |
height: 40, | |
lineHeight: 38, | |
fontSize: 24, | |
borderBottomWidth: 2, | |
borderBottomColor: '#aeaeae', | |
textAlign: 'center', | |
}, | |
focusCell: { | |
borderBottomColor: '#61af5c', | |
}, | |
}); | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment