Repository navigation
Expand file tree
/
Copy pathOTPProviderExample.tsx
More file actions
83 lines (74 loc) · 2.05 KB
/
Copy pathOTPProviderExample.tsx
File metadata and controls
83 lines (74 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { View, Button, TouchableOpacity, Text, Keyboard } from 'react-native';
import {
OTPInputCell,
OTPProvider,
useOTPInputCellState,
} from 'react-native-otp-receiver';
import { useOTPContext } from '../../src/components/OTPProvider';
import styles from './styles';
const getCodeFromSMS = (sms: string) => {
return sms.match(/\d{6}/)?.[0] || '';
};
const toUpperCase = (char: string) => {
return char.toUpperCase();
};
const shape = [3, 3];
const isCharValidRegex = /^[a-zA-Z0-9]$/;
export default function OTPProviderExample() {
return (
<OTPProvider
codeInputShape={shape}
onCodeEntered={(code) => {
console.log('Code Entered', code);
}}
onSubmitCode={(data) => {
Keyboard.dismiss();
console.log('Submit Code', data);
}}
parseSMS={getCodeFromSMS}
parseEnteredCodeChar={toUpperCase}
parsePastedCode={(code) => code.split('-')}
validateCodeChar={isCharValidRegex}
expectSMSOnMount
>
<CodeInputRenderer />
</OTPProvider>
);
}
function CodeInputRenderer() {
const { isCodeValid, submitCode } = useOTPContext();
return (
<>
<View style={styles.otpContainer}>
<CodeInputComponent index={0} />
<Text style={styles.codeInputValue}>-</Text>
<CodeInputComponent index={1} />
</View>
<Button title="Submit" disabled={!isCodeValid} onPress={submitCode} />
</>
);
}
function CodeInputComponent({ index }: { index: number }) {
const { isFocused, value, getRef } = useOTPInputCellState(index);
const backgroundColor = isFocused ? 'white' : '#DEDEDE';
const borderColor = isFocused ? '#000000' : '#3A4A3A';
const width = 90;
return (
<TouchableOpacity
style={[
styles.otpWrapper,
{
width,
backgroundColor,
borderColor,
},
]}
onPress={() => {
getRef()?.focus();
}}
>
<OTPInputCell style={styles.otpInputHidden} index={index} />
<Text style={styles.codeInputValue}>{value}</Text>
</TouchableOpacity>
);
}