Skip to content

Commit d86a6e6

Browse files
committed
fix: set autoFocus to default as true
1 parent 32d7cca commit d86a6e6

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/components/text-input.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { TextInputOTPProps, TextInputOTPRef } from '../types';
66
export const TextInput = forwardRef<
77
TextInputOTPRef,
88
Omit<TextInputOTPProps, 'children'>
9-
>((props, ref) => {
9+
>(({ autoFocus = true, ...rest }, ref) => {
1010
const {
1111
inputRef,
1212
handleKeyPress,
@@ -32,7 +32,8 @@ export const TextInput = forwardRef<
3232
onChangeText={handleChangeText}
3333
style={styles.input}
3434
keyboardType="number-pad"
35-
{...props}
35+
autoFocus={autoFocus}
36+
{...rest}
3637
/>
3738
);
3839
});

src/hooks/use-text-input-otp.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ const TextInputOTPContext = createContext<TextInputOTPContextProps>({
2727
});
2828

2929
export function TextInputOTPProvider({
30+
autoFocus = true,
3031
maxLength,
3132
onFilled,
3233
children,
33-
}: PropsWithChildren<Pick<TextInputOTPProps, 'maxLength' | 'onFilled'>>) {
34+
}: PropsWithChildren<
35+
Pick<TextInputOTPProps, 'autoFocus' | 'maxLength' | 'onFilled'>
36+
>) {
3437
const [code, setCode] = useState(Array(maxLength).fill(''));
35-
const [currentIndex, setCurrentIndex] = useState(0);
38+
const [currentIndex, setCurrentIndex] = useState(autoFocus ? 0 : -1);
3639
const inputRef = useRef<TextInput>(null);
3740

3841
function updateCodeAtIndex(index: number, value: string) {

src/types/text-input-otp.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { StyleProp, TextInputProps, ViewStyle } from 'react-native';
33

44
export type TextInputOTPProps = {
55
children: ReactNode;
6+
autoFocus?: boolean;
67
maxLength: number;
78
onFilled?: (text: string) => void;
89
containerStyles?: StyleProp<ViewStyle>;

0 commit comments

Comments
 (0)