Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,57 @@ The `react-native-otp-entry` component exposes these functions with `ref`:
| `blur` | () => void; | Blurs the OTP input. |
| `setValue` | (value: string) => void; | Sets the value of the OTP input. |

## Editing and pasting digits

Tap a filled cell to select its character, then type to replace it without deleting
the following digits. Tapping an empty cell places the cursor after the existing
text. The selected digit remains visible (or masked with `secureTextEntry`).

Pasting uses the current selection and the available space up to `numberOfDigits`;
it does not automatically replace the entire code. After insertion the cursor is
placed after the inserted text, so Backspace can delete it normally. Native
long-press selection and paste menus remain available.

`clear()` resets the cursor to the beginning, and `setValue()` places it at the end
of the assigned value. `onFilled` and `blurOnFilled` also apply when a replacement
leaves all cells filled. If you provide `textInputProps.selection`, that selection
remains controlled by your application; update it in response to native events.

## Multiple OTP inputs and focus

`autoFocus` defaults to `true`. When rendering more than one `OtpInput` on a screen,
set `autoFocus={false}` on the inputs that should not receive initial focus:

```jsx
<OtpInput autoFocus={true} />
<OtpInput autoFocus={false} />
```

For inputs inside a modal or a transitioning screen, use `autoFocus={false}` and
call `ref.current?.focus()` when the modal or screen is ready (for example, from
React Native Modal's `onShow`). The visible focus indicator alone does not confirm
that the native keyboard has opened; keyboard behavior also depends on the host
screen, platform, and keyboard.

## Unique identifiers for automation

Use a different `textInputProps.testID` or `textInputProps.nativeID` for each OTP
instance. The hidden input retains the exact identifier, and each pressable digit
container receives the same identifier followed by its zero-based index:

```jsx
<OtpInput
numberOfDigits={4}
textInputProps={{ testID: "login-code", nativeID: "login-code" }}
/>
```

This produces `login-code` for the hidden input and `login-code-0` through
`login-code-3` for the digit containers. Enter text through the hidden input.
Without a custom `testID`, the existing `otp-input` digit selectors and
`otp-input-hidden` input selector are preserved. `textProps.testID` continues to
identify digit text separately, and accessibility labels are not changed.

## License

This project is licensed under the [MIT License](https://github.com/your-username/react-native-otp-entry/blob/main/LICENSE).
Expand Down
43 changes: 33 additions & 10 deletions src/OtpInput/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ import { useOtpInput } from "./useOtpInput";

export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
const {
models: { text, inputRef, focusedInputIndex, isFocused, placeholder },
actions: { clear, handlePress, handleTextChange, focus, handleFocus, handleBlur, blur },
models: { text, inputRef, focusedInputIndex, isFocused, placeholder, selection },
actions: {
clear,
handlePress,
handleTextChange,
focus,
handleFocus,
handleBlur,
blur,
handleCellLayout,
handleInputLayout,
handleInputPressIn,
handleInputPressOut,
handleSelectionChange,
},
forms: { setTextWithRef },
} = useOtpInput(props);
const {
Expand Down Expand Up @@ -73,18 +86,20 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
const isPlaceholderCell = !!placeholder && !text?.[index];
const char = isPlaceholderCell ? placeholder?.[index] || " " : text[index];
const isFocusedInput = index === focusedInputIndex && !disabled && Boolean(isFocused);
const isFilledLastInput = text.length === numberOfDigits && index === text.length - 1;
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));

return (
<Pressable
key={`${char}-${index}`}
key={index}
disabled={disabled}
onPress={handlePress}
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
testID="otp-input"
onPress={() => handlePress(index)}
onLayout={(event) => handleCellLayout(index, event)}
style={generatePinCodeContainerStyle(isFocusedInput, text[index])}
testID={textInputProps?.testID ? `${textInputProps.testID}-${index}` : "otp-input"}
nativeID={
textInputProps?.nativeID ? `${textInputProps.nativeID}-${index}` : undefined
}
>
{isFocusedInput && !hideStick ? (
{isFocusedInput && !text[index] && !hideStick ? (
<VerticalStick
focusColor={focusColor}
style={focusStickStyle}
Expand All @@ -109,7 +124,6 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
})}
<TextInput
value={text}
onChangeText={handleTextChange}
maxLength={numberOfDigits}
inputMode={type === "numeric" ? type : "text"}
textContentType="oneTimeCode"
Expand All @@ -124,6 +138,15 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
onBlur={handleBlur}
caretHidden={Platform.OS === "ios"}
{...textInputProps}
selection={selection}
onChangeText={(value) => {
handleTextChange(value);
textInputProps?.onChangeText?.(value);
}}
onLayout={handleInputLayout}
onPressIn={handleInputPressIn}
onPressOut={handleInputPressOut}
onSelectionChange={handleSelectionChange}
style={[StyleSheet.absoluteFill, styles.hiddenInput, textInputProps?.style]}
/>
</View>
Expand Down
11 changes: 8 additions & 3 deletions src/OtpInput/VerticalStick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@ export const VerticalStick: React.FC<VerticalStickProps> = memo(
const opacityAnim = useRef(new Animated.Value(1)).current;

useEffect(() => {
Animated.loop(
const animation = Animated.loop(
Animated.sequence([
Animated.timing(opacityAnim, {
toValue: 0,
useNativeDriver: true,
isInteraction: false,
duration: focusStickBlinkingDuration,
}),
Animated.timing(opacityAnim, {
toValue: 1,
useNativeDriver: true,
isInteraction: false,
duration: focusStickBlinkingDuration,
}),
]),
{
iterations: -1,
}
).start();
}, []);
);
animation.start();

return () => animation.stop();
}, [focusStickBlinkingDuration, opacityAnim]);

return (
<Animated.View style={{ opacity: opacityAnim }}>
Expand Down
Loading