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

fix(input-otp): autofocus #4296

Merged
merged 2 commits into from
Dec 9, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/smooth-trainers-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/input-otp": patch
---

Fixing the autofocus functionality in input-otp component(#4250)
6 changes: 6 additions & 0 deletions apps/docs/content/docs/components/input-otp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ You can customize the styles of the `InputOtp` component using the `classNames`
description: "Allows to set custom class names for the Input slots.",
default: "-"
},
{
attribute: "autoFocus",
type: "boolean",
description: "Whether the element should receive focus on render.",
default: "false"
},
{
attribute: "textAlign",
type: "left | center | right",
Expand Down
15 changes: 15 additions & 0 deletions packages/components/input-otp/__tests__/input-otp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ describe("InputOtp Component", () => {
expect(onComplete).toHaveBeenCalledTimes(2);
expect(onComplete).toHaveBeenCalledWith("1234");
});

it("should autofocus when autofocus prop is passed.", () => {
// eslint-disable-next-line jsx-a11y/no-autofocus
render(<InputOtp autoFocus length={4} />);
const segments = screen.getAllByRole("presentation");

expect(segments[0]).toHaveAttribute("data-focus", "true");
});

it("should not autofocus when autofocus prop is not passed.", () => {
render(<InputOtp length={4} />);
const segments = screen.getAllByRole("presentation");

expect(segments[0]).not.toHaveAttribute("data-focus", "true");
});
});

describe("InputOtp with react-hook-form", () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/components/input-otp/src/use-input-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
ref: inputRef,
name: name,
value: value,
autoFocus,
onChange: setValue,
onBlur: chain(focusProps.onBlur, props?.onBlur),
onComplete: onComplete,
Expand All @@ -254,6 +255,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
setValue,
props.onBlur,
onComplete,
autoFocus,
],
);

Expand Down
Loading