|
1 |
| -import { View, Text, StyleSheet } from 'react-native'; |
| 1 | +import { View, Text, StyleSheet, Pressable } from 'react-native'; |
2 | 2 | import { OTPInput, type SlotProps } from 'input-otp-native';
|
3 | 3 | import { FakeCaret } from './fake-caret';
|
| 4 | +import type { OTPInputRef } from '../../src/types'; |
| 5 | +import { useRef } from 'react'; |
4 | 6 |
|
5 | 7 | export default function StripeOTPInput() {
|
6 |
| - return ( |
7 |
| - <OTPInput |
8 |
| - containerStyle={styles.container} |
9 |
| - maxLength={6} |
10 |
| - render={({ slots }) => ( |
11 |
| - <> |
12 |
| - <View style={styles.slotsContainer}> |
13 |
| - {slots.slice(0, 3).map((slot, idx) => ( |
14 |
| - <Slot key={idx} {...slot} index={idx} /> |
15 |
| - ))} |
16 |
| - </View> |
| 8 | + const ref = useRef<OTPInputRef>(null); |
17 | 9 |
|
18 |
| - <FakeDash /> |
| 10 | + return ( |
| 11 | + <View> |
| 12 | + <OTPInput |
| 13 | + ref={ref} |
| 14 | + containerStyle={styles.container} |
| 15 | + maxLength={6} |
| 16 | + render={({ slots, isFocused }) => ( |
| 17 | + <> |
| 18 | + <View style={styles.slotsContainer}> |
| 19 | + {slots.slice(0, 3).map((slot, idx) => ( |
| 20 | + <Slot key={idx} {...slot} index={idx} /> |
| 21 | + ))} |
| 22 | + </View> |
| 23 | + {isFocused && <FakeDash />} |
| 24 | + <FakeDash /> |
19 | 25 |
|
20 |
| - <View style={styles.slotsContainer}> |
21 |
| - {slots.slice(3).map((slot, idx) => ( |
22 |
| - <Slot key={idx} {...slot} index={idx} /> |
23 |
| - ))} |
24 |
| - </View> |
25 |
| - </> |
26 |
| - )} |
27 |
| - /> |
| 26 | + <View style={styles.slotsContainer}> |
| 27 | + {slots.slice(3).map((slot, idx) => ( |
| 28 | + <Slot key={idx} {...slot} index={idx} /> |
| 29 | + ))} |
| 30 | + </View> |
| 31 | + </> |
| 32 | + )} |
| 33 | + /> |
| 34 | + <View style={styles.actionsContainer}> |
| 35 | + <Pressable |
| 36 | + style={({ pressed }) => [ |
| 37 | + styles.actionButton, |
| 38 | + pressed && styles.actionButtonPressed, |
| 39 | + ]} |
| 40 | + onPress={() => ref.current?.focus()} |
| 41 | + > |
| 42 | + <Text style={styles.actionButtonText}>Focus</Text> |
| 43 | + </Pressable> |
| 44 | + <Pressable |
| 45 | + style={({ pressed }) => [ |
| 46 | + styles.actionButton, |
| 47 | + pressed && styles.actionButtonPressed, |
| 48 | + ]} |
| 49 | + onPress={() => ref.current?.blur()} |
| 50 | + > |
| 51 | + <Text style={styles.actionButtonText}>Blur</Text> |
| 52 | + </Pressable> |
| 53 | + <Pressable |
| 54 | + style={({ pressed }) => [ |
| 55 | + styles.actionButton, |
| 56 | + pressed && styles.actionButtonPressed, |
| 57 | + ]} |
| 58 | + onPress={() => ref.current?.clear()} |
| 59 | + > |
| 60 | + <Text style={styles.actionButtonText}>Clear</Text> |
| 61 | + </Pressable> |
| 62 | + <Pressable |
| 63 | + style={({ pressed }) => [ |
| 64 | + styles.actionButton, |
| 65 | + pressed && styles.actionButtonPressed, |
| 66 | + ]} |
| 67 | + onPress={() => ref.current?.setValue('123')} |
| 68 | + > |
| 69 | + <Text style={styles.actionButtonText}>Set Value</Text> |
| 70 | + </Pressable> |
| 71 | + </View> |
| 72 | + </View> |
28 | 73 | );
|
29 | 74 | }
|
30 | 75 |
|
@@ -97,4 +142,34 @@ const styles = StyleSheet.create({
|
97 | 142 | backgroundColor: '#E5E7EB',
|
98 | 143 | borderRadius: 1,
|
99 | 144 | },
|
| 145 | + actionsContainer: { |
| 146 | + flexDirection: 'row', |
| 147 | + flexWrap: 'wrap', |
| 148 | + gap: 8, |
| 149 | + marginTop: 24, |
| 150 | + justifyContent: 'center', |
| 151 | + }, |
| 152 | + actionButton: { |
| 153 | + paddingHorizontal: 16, |
| 154 | + paddingVertical: 8, |
| 155 | + backgroundColor: '#4F46E5', |
| 156 | + borderRadius: 6, |
| 157 | + elevation: 2, |
| 158 | + shadowColor: '#000', |
| 159 | + shadowOffset: { |
| 160 | + width: 0, |
| 161 | + height: 1, |
| 162 | + }, |
| 163 | + shadowOpacity: 0.2, |
| 164 | + shadowRadius: 1.41, |
| 165 | + }, |
| 166 | + actionButtonPressed: { |
| 167 | + backgroundColor: '#4338CA', |
| 168 | + transform: [{ scale: 0.98 }], |
| 169 | + }, |
| 170 | + actionButtonText: { |
| 171 | + color: '#FFFFFF', |
| 172 | + fontSize: 14, |
| 173 | + fontWeight: '500', |
| 174 | + }, |
100 | 175 | });
|
0 commit comments