Skip to content

Commit 1bbcc64

Browse files
feat: add method to listen cell text change
1 parent 9e3e6c7 commit 1bbcc64

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ Supports both Android and iOS.
2727

2828
The following props are applicable for the component along with **props supported by react native `TextInput` component**
2929

30-
| Prop | Type | Optional | Default | Description |
31-
| ---------------- | ------ | -------- | ------------ | -------------------------------------------------------------------------------- |
32-
| defaultValue | string | Yes | '' | Default Value that can be set based on OTP / Pin received from parent container. |
33-
| handleTextChange | func | No | n/a | callback with concated string of all cells as argument. |
34-
| inputCount | number | Yes | 4 | Number of Text Input Cells to be present. |
35-
| tintColor | string | Yes | #3CB371 | Color for Cell Border on being focused. |
36-
| offTintColor | string | Yes | #DCDCDC | Color for Cell Border Border not focused. |
37-
| inputCellLength | number | Yes | 1 | Number of character that can be entered inside a single cell. |
38-
| containerStyle | object | Yes | {} | style for overall container. |
39-
| textInputStyle | object | Yes | {} | style for text input. |
40-
| testIDPrefix | string | Yes | 'otp*input*' | testID prefix, the result will be `otp_input_0` until inputCount |
41-
| autoFocus | bool | Yes | false | Input should automatically get focus when the components loads |
30+
| Prop | Type | Optional | Default | Description |
31+
| -------------------- | ------ | -------- | ------------ | -------------------------------------------------------------------------------------- |
32+
| defaultValue | string | Yes | '' | Default Value that can be set based on OTP / Pin received from parent container. |
33+
| handleTextChange | func | No | n/a | callback with concated string of all cells as argument. |
34+
| handleCellTextChange | func | Yes | n/a | callback for text change in individual cell with cell text and cell index as arguments |
35+
| inputCount | number | Yes | 4 | Number of Text Input Cells to be present. |
36+
| tintColor | string | Yes | #3CB371 | Color for Cell Border on being focused. |
37+
| offTintColor | string | Yes | #DCDCDC | Color for Cell Border Border not focused. |
38+
| inputCellLength | number | Yes | 1 | Number of character that can be entered inside a single cell. |
39+
| containerStyle | object | Yes | {} | style for overall container. |
40+
| textInputStyle | object | Yes | {} | style for text input. |
41+
| testIDPrefix | string | Yes | 'otp*input*' | testID prefix, the result will be `otp_input_0` until inputCount |
42+
| autoFocus | bool | Yes | false | Input should automatically get focus when the components loads |
4243

4344
#### Helper Functions
4445

@@ -83,7 +84,7 @@ const ParentComponent = () => {
8384
<View>
8485
<OTPTextInput ref={e => (otpInput = e)} >
8586
<Button title="clear" onClick={clearText}>
86-
</View> ̰
87+
</View>
8788
);
8889
}
8990
```
@@ -93,3 +94,4 @@ const ParentComponent = () => {
9394
If you think I have helped you, feel free to get me coffee. 😊
9495

9596
<a href="https://www.buymeacoffee.com/naveenvignesh" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" height="60" ></a>
97+

index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from "react";
22
import { View, TextInput, StyleSheet, ViewStyle, KeyboardType, NativeSyntheticEvent, TextInputKeyPressEventData } from "react-native";
3+
34
interface IState {
45
focusedInput: number;
56
otpText: string[];
@@ -13,6 +14,7 @@ interface IProps {
1314
tintColor: string | string[];
1415
offTintColor: string | string[];
1516
handleTextChange(text: string): void;
17+
handleCellTextChange?(text: string, cellIndex: number): void;
1618
keyboardType: KeyboardType;
1719
testIDPrefix: string;
1820
autoFocus: boolean;
@@ -106,7 +108,7 @@ class OTPTextView extends Component<IProps, IState> {
106108
};
107109

108110
onTextChange = (text: string, i: number) => {
109-
const { inputCellLength, inputCount, handleTextChange } = this.props;
111+
const { inputCellLength, inputCount, handleTextChange, handleCellTextChange } = this.props;
110112

111113
if (text && !this.basicValidation(text)) {
112114
return;
@@ -124,6 +126,7 @@ class OTPTextView extends Component<IProps, IState> {
124126
},
125127
() => {
126128
handleTextChange(this.state.otpText.join(""));
129+
handleCellTextChange && handleCellTextChange(text, i);
127130
if (text.length === inputCellLength && i !== inputCount - 1) {
128131
this.inputs[i + 1].focus();
129132
}
@@ -190,10 +193,10 @@ class OTPTextView extends Component<IProps, IState> {
190193
);
191194
};
192195

193-
setValue = (value: string) => {
196+
setValue = (value: string, isPaste: boolean = false) => {
194197
const { inputCount, inputCellLength } = this.props;
195198

196-
const updatedFocusInput = value.length - 1;
199+
const updatedFocusInput = isPaste ? inputCount - 1 : value.length - 1;
197200

198201
this.setState(
199202
{

0 commit comments

Comments
 (0)