Skip to content

Commit 94d78a7

Browse files
author
johelder
committed
feat: add new examples to readme
1 parent e52155e commit 94d78a7

File tree

1 file changed

+118
-8
lines changed

1 file changed

+118
-8
lines changed

README.md

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npm install react-native-input-code-otp
1212

1313
## Usage
1414

15-
```ts
15+
```tsx
1616
import {
1717
TextInputOTP,
1818
TextInputOTPSlot,
@@ -35,7 +35,7 @@ export function MyComponent() {
3535
<TextInputOTPSlot index={5} />
3636
</TextInputOTPGroup>
3737
</TextInputOTP>
38-
)
38+
);
3939
}
4040
```
4141

@@ -46,10 +46,14 @@ export function MyComponent() {
4646
| `maxLength` | number - Required | The max number of digits to OTP code. |
4747
| `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. |
4848

49+
---
50+
4951
| TextInputOTPGroup | Type | Description |
5052
| ----------------- | -------------------- | ----------------------------- |
5153
| `groupStyles` | ViewStyle - Optional | Custom styles for the `View`. |
5254

55+
---
56+
5357
| TextInputOTPSlot | Type | Description |
5458
| ----------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------- |
5559
| `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() {
5862
| `slotTextStyles` | TextStyle - Optional | Custom styles for the `Text`. |
5963
| `focusedSlotTextStyles` | TextStyle - Optional | Custom styles applied to the slot `Text` when it is focused. |
6064

65+
---
66+
6167
| TextInputOTPSeparator | Type | Description |
6268
| --------------------- | -------------------- | ----------------------------- |
6369
| `separatorStyles` | ViewStyle - Optional | Custom styles for the `View`. |
@@ -66,12 +72,116 @@ export function MyComponent() {
6672

6773
The `TextInputOTP` component exposes these functions with `ref`:
6874

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>
75185

76186
## Contributing
77187

0 commit comments

Comments
 (0)