Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lastInputFocused props is added #1

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
<td>false</td>
<td>Auto focuses input on initial page load.</td>
</tr>
<tr>
<td>lastInputFocused</td>
<td>boolean</td>
<td>false</td>
<td>false</td>
<td>Auto focuses the last input element on initial page load. <code>shouldAutoFocus</code> has higher priority if two auto focus props are set.</td>
</tr>
<tr>
<td>skipDefaultStyles</td>
<td>boolean</td>
Expand Down
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface OTPInputProps {
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
/** Whether the first input should be auto focused */
shouldAutoFocus?: boolean;
/** Whether the last input should be auto focused */
lastInputFocused?: boolean;
/** Placeholder for the inputs */
placeholder?: string;
/** Function to render the separator */
Expand All @@ -60,6 +62,7 @@ const OTPInput = ({
onPaste,
renderInput,
shouldAutoFocus = false,
lastInputFocused = false,
inputType = 'text',
renderSeparator,
placeholder,
Expand All @@ -81,8 +84,10 @@ const OTPInput = ({
React.useEffect(() => {
if (shouldAutoFocus) {
inputRefs.current[0]?.focus();
} else if (lastInputFocused) {
focusInput(numInputs - 1);
}
}, [shouldAutoFocus]);
}, [shouldAutoFocus, lastInputFocused]);

const getPlaceholderValue = () => {
if (typeof placeholder === 'string') {
Expand Down