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
28 changes: 20 additions & 8 deletions src/OtpInput/OtpInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forwardRef, useImperativeHandle } from "react";
import { Platform, Pressable, Text, TextInput, View } from "react-native";
import { forwardRef, useImperativeHandle, useMemo } from "react";
import { Animated, Platform, Pressable, Text, TextInput, View } from "react-native";
import { styles } from "./OtpInput.styles";
import { OtpInputProps, OtpInputRef } from "./OtpInput.types";
import { VerticalStick } from "./VerticalStick";
Expand All @@ -22,6 +22,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
theme = {},
textInputProps,
type = "numeric",
useAnimatedComponents = false,
} = props;
const {
containerStyle,
Expand Down Expand Up @@ -57,8 +58,19 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
return stylesArray;
};

const { ViewComponent, PressableComponent, TextComponent } = useMemo(() => {
if (useAnimatedComponents) {
return {
ViewComponent: Animated.View,
PressableComponent: Animated.createAnimatedComponent(Pressable),
TextComponent: Animated.Text,
};
}
return { ViewComponent: View, PressableComponent: Pressable, TextComponent: Text };
}, [useAnimatedComponents]);

return (
<View style={[styles.container, containerStyle, inputsContainerStyle]}>
<ViewComponent style={[styles.container, containerStyle, inputsContainerStyle]}>
{Array(numberOfDigits)
.fill(0)
.map((_, index) => {
Expand All @@ -68,7 +80,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));

return (
<Pressable
<PressableComponent
key={`${char}-${index}`}
disabled={disabled}
onPress={handlePress}
Expand All @@ -82,11 +94,11 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
focusStickBlinkingDuration={focusStickBlinkingDuration}
/>
) : (
<Text style={[styles.codeText, pinCodeTextStyle]}>
<TextComponent style={[styles.codeText, pinCodeTextStyle]}>
{char && secureTextEntry ? "•" : char}
</Text>
</TextComponent>
)}
</Pressable>
</PressableComponent>
);
})}
<TextInput
Expand All @@ -107,6 +119,6 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
{...textInputProps}
style={[styles.hiddenInput, textInputProps?.style]}
/>
</View>
</ViewComponent>
);
});
1 change: 1 addition & 0 deletions src/OtpInput/OtpInput.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface OtpInputProps {
disabled?: boolean;
textInputProps?: TextInputProps;
type?: "alpha" | "numeric" | "alphanumeric";
useAnimatedComponents?: boolean;
}

export interface OtpInputRef {
Expand Down
Loading