-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrevolt.tsx
139 lines (129 loc) · 2.97 KB
/
revolt.tsx
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { View, Text, StyleSheet, type ViewStyle, Alert } from 'react-native';
import { OTPInput, type SlotProps } from 'input-otp-native';
import type { OTPInputRef } from 'input-otp-native';
import React, { useRef } from 'react';
import Animated, {
useAnimatedStyle,
withRepeat,
withTiming,
withSequence,
useSharedValue,
} from 'react-native-reanimated';
import { useEffect } from 'react';
export default function RevoltOTPInput() {
const ref = useRef<OTPInputRef>(null);
const onComplete = (code: string) => {
Alert.alert('Completed with code:', code);
ref.current?.clear();
};
return (
<View>
<OTPInput
ref={ref}
onComplete={onComplete}
containerStyle={styles.container}
maxLength={6}
render={({ slots }) => (
<View style={styles.slotsContainer}>
{slots.map((slot, idx) => (
<React.Fragment key={idx}>
<Slot {...slot} />
{idx === 2 && <FakeDash />}
</React.Fragment>
))}
</View>
)}
/>
</View>
);
}
function Slot({ char, isActive, hasFakeCaret }: SlotProps) {
return (
<View style={[styles.slot, isActive && styles.activeSlot]}>
{char !== null && <Text style={styles.char}>{char}</Text>}
{hasFakeCaret && <FakeCaret />}
</View>
);
}
function FakeDash() {
return (
<View style={styles.fakeDashContainer}>
<View style={styles.fakeDash} />
</View>
);
}
function FakeCaret({ style }: { style?: ViewStyle }) {
const opacity = useSharedValue(1);
useEffect(() => {
opacity.value = withRepeat(
withSequence(
withTiming(0, { duration: 500 }),
withTiming(1, { duration: 500 })
),
-1,
true
);
}, [opacity]);
const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));
return (
<View style={styles.fakeCaretContainer}>
<Animated.View style={[styles.fakeCaret, style, animatedStyle]} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
slotsContainer: {
flexDirection: 'row',
gap: 12,
},
slot: {
width: 40,
height: 40,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1,
borderColor: '#E5E7EB',
borderRadius: 8,
backgroundColor: '#F9FAFB',
},
activeSlot: {
borderColor: '#2563EB',
borderWidth: 2,
},
char: {
fontSize: 18,
fontWeight: '500',
color: '#111827',
},
fakeCaretContainer: {
position: 'absolute',
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
},
fakeCaret: {
width: 2,
height: 24,
backgroundColor: '#2563EB',
borderRadius: 1,
},
fakeDashContainer: {
width: 8,
alignItems: 'center',
justifyContent: 'center',
},
fakeDash: {
width: 8,
height: 2,
backgroundColor: '#E5E7EB',
borderRadius: 1,
},
});