@@ -12,7 +12,7 @@ npm install react-native-input-code-otp
12
12
13
13
## Usage
14
14
15
- ``` ts
15
+ ``` tsx
16
16
import {
17
17
TextInputOTP ,
18
18
TextInputOTPSlot ,
@@ -35,7 +35,7 @@ export function MyComponent() {
35
35
<TextInputOTPSlot index = { 5 } />
36
36
</TextInputOTPGroup >
37
37
</TextInputOTP >
38
- )
38
+ );
39
39
}
40
40
```
41
41
@@ -46,10 +46,14 @@ export function MyComponent() {
46
46
| ` maxLength ` | number - Required | The max number of digits to OTP code. |
47
47
| ` onFilled ` | (code: string) => void - Optional | The callback function is executed when the OTP input has been entirely completed, and it receives the OTP code as a parameter. |
48
48
49
+ ---
50
+
49
51
| TextInputOTPGroup | Type | Description |
50
52
| ----------------- | -------------------- | ----------------------------- |
51
53
| ` groupStyles ` | ViewStyle - Optional | Custom styles for the ` View ` . |
52
54
55
+ ---
56
+
53
57
| TextInputOTPSlot | Type | Description |
54
58
| ----------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------- |
55
59
| ` index ` | number - Required | The position of the slot within the OTP input sequence. Each slot must have a unique index starting from 0. |
@@ -58,6 +62,8 @@ export function MyComponent() {
58
62
| ` slotTextStyles ` | TextStyle - Optional | Custom styles for the ` Text ` . |
59
63
| ` focusedSlotTextStyles ` | TextStyle - Optional | Custom styles applied to the slot ` Text ` when it is focused. |
60
64
65
+ ---
66
+
61
67
| TextInputOTPSeparator | Type | Description |
62
68
| --------------------- | -------------------- | ----------------------------- |
63
69
| ` separatorStyles ` | ViewStyle - Optional | Custom styles for the ` View ` . |
@@ -66,12 +72,116 @@ export function MyComponent() {
66
72
67
73
The ` TextInputOTP ` component exposes these functions with ` ref ` :
68
74
69
- | Prop | Type | Description |
70
- | ---------- | ------------------------ | -------------------------------------------------------------------------- |
71
- | ` clear ` | () => void; | Resets the OTP input by clearing all entered values. |
72
- | ` focus ` | () => void; | Activates the OTP input field, allowing the user to type. |
73
- | ` blue ` | () => void; | Deactivates the OTP input field, removing focus. |
74
- | ` setValue ` | (value: string) => void; | Sets a custom value to the OTP input fields, overriding any current input. |
75
+ | Prop | Type | Description |
76
+ | ---------- | ----------------------- | -------------------------------------------------------------------------- |
77
+ | ` clear ` | () => void | Resets the OTP input by clearing all entered values. |
78
+ | ` focus ` | () => void | Activates the OTP input field, allowing the user to type. |
79
+ | ` blue ` | () => void | Deactivates the OTP input field, removing focus. |
80
+ | ` setValue ` | (value: string) => void | Sets a custom value to the OTP input fields, overriding any current input. |
81
+
82
+ ## Examples
83
+
84
+ <details >
85
+ <summary >Usage with react-hook-form</summary >
86
+
87
+ ``` tsx
88
+ import { Button , View } from ' react-native' ;
89
+ import {
90
+ TextInputOTP ,
91
+ TextInputOTPSlot ,
92
+ TextInputOTPGroup ,
93
+ TextInputOTPSeparator ,
94
+ } from ' react-native-input-code-otp' ;
95
+ import { Controller , useForm } from ' react-hook-form' ;
96
+
97
+ type FormValues = {
98
+ code: string ;
99
+ };
100
+
101
+ export function MyComponent() {
102
+ const { control, handleSubmit } = useForm <FormValues >({
103
+ defaultValues: {
104
+ code: ' ' ,
105
+ },
106
+ });
107
+
108
+ function onSubmit({ code }: FormValues ) {
109
+ console .log ({ code });
110
+ }
111
+
112
+ return (
113
+ <View >
114
+ <Controller
115
+ name = " code"
116
+ control = { control }
117
+ render = { ({ field : { onChange , value } }) => (
118
+ <TextInputOTP value = { value } onChangeText = { onChange } maxLength = { 6 } >
119
+ <TextInputOTPGroup >
120
+ <TextInputOTPSlot index = { 0 } />
121
+ <TextInputOTPSlot index = { 1 } />
122
+ <TextInputOTPSlot index = { 2 } />
123
+ </TextInputOTPGroup >
124
+ <TextInputOTPSeparator />
125
+ <TextInputOTPGroup >
126
+ <TextInputOTPSlot index = { 3 } />
127
+ <TextInputOTPSlot index = { 4 } />
128
+ <TextInputOTPSlot index = { 5 } />
129
+ </TextInputOTPGroup >
130
+ </TextInputOTP >
131
+ )}
132
+ />
133
+
134
+ <Button title = " Submit" onPress = { handleSubmit (onSubmit )} />
135
+ </View >
136
+ );
137
+ }
138
+ ```
139
+
140
+ </details >
141
+
142
+ <details >
143
+ <summary >Usage of ref to programmatically set OTP value</summary >
144
+
145
+ ``` tsx
146
+ import { useRef } from ' react' ;
147
+ import { Button , View } from ' react-native' ;
148
+ import {
149
+ TextInputOTP ,
150
+ TextInputOTPSlot ,
151
+ TextInputOTPGroup ,
152
+ TextInputOTPSeparator ,
153
+ } from ' react-native-input-code-otp' ;
154
+
155
+ export function MyComponent() {
156
+ const inputRef = useRef <TextInputOTPRef >(null );
157
+
158
+ function onSomeAction() {
159
+ inputRef .current ?.setValue (' 123456' );
160
+ }
161
+
162
+ return (
163
+ <View >
164
+ <TextInputOTP ref = { inputRef } maxLength = { 6 } >
165
+ <TextInputOTPGroup >
166
+ <TextInputOTPSlot index = { 0 } />
167
+ <TextInputOTPSlot index = { 1 } />
168
+ <TextInputOTPSlot index = { 2 } />
169
+ </TextInputOTPGroup >
170
+ <TextInputOTPSeparator />
171
+ <TextInputOTPGroup >
172
+ <TextInputOTPSlot index = { 3 } />
173
+ <TextInputOTPSlot index = { 4 } />
174
+ <TextInputOTPSlot index = { 5 } />
175
+ </TextInputOTPGroup >
176
+ </TextInputOTP >
177
+
178
+ <Button title = " Submit" onPress = { onSomeAction } />
179
+ </View >
180
+ );
181
+ }
182
+ ```
183
+
184
+ </details >
75
185
76
186
## Contributing
77
187
0 commit comments