Skip to content

Commit d7f32f3

Browse files
committed
feat(example): add example actions
1 parent 534b556 commit d7f32f3

File tree

2 files changed

+101
-24
lines changed

2 files changed

+101
-24
lines changed

example/src/fake-caret.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { View, StyleSheet } from 'react-native';
1+
import { View, StyleSheet, type ViewStyle } from 'react-native';
22
import Animated, {
33
useAnimatedStyle,
44
withRepeat,
@@ -8,7 +8,7 @@ import Animated, {
88
} from 'react-native-reanimated';
99
import { useEffect } from 'react';
1010

11-
export function FakeCaret() {
11+
export function FakeCaret({ style }: { style?: ViewStyle }) {
1212
const opacity = useSharedValue(1);
1313

1414
useEffect(() => {
@@ -28,7 +28,9 @@ export function FakeCaret() {
2828

2929
return (
3030
<View style={styles.fakeCaretContainer}>
31-
<Animated.View style={[styles.fakeCaret, animatedStyle]} />
31+
<Animated.View
32+
style={StyleSheet.flatten([styles.fakeCaret, style, animatedStyle])}
33+
/>
3234
</View>
3335
);
3436
}

example/src/stripe.tsx

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
1-
import { View, Text, StyleSheet } from 'react-native';
1+
import { View, Text, StyleSheet, Pressable } from 'react-native';
22
import { OTPInput, type SlotProps } from 'input-otp-native';
33
import { FakeCaret } from './fake-caret';
4+
import type { OTPInputRef } from '../../src/types';
5+
import { useRef } from 'react';
46

57
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);
179

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 />
1925

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>
2873
);
2974
}
3075

@@ -97,4 +142,34 @@ const styles = StyleSheet.create({
97142
backgroundColor: '#E5E7EB',
98143
borderRadius: 1,
99144
},
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+
},
100175
});

0 commit comments

Comments
 (0)