Skip to content

Commit

Permalink
chore(nits): nnits
Browse files Browse the repository at this point in the history
  • Loading branch information
Maharshi Alpesh authored and Maharshi Alpesh committed Sep 27, 2024
1 parent bc5286c commit 0166f8f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion apps/docs/content/components/input-otp/allowedKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const App = `import {InputOtp} from "@nextui-org/react";
export default function App() {
const exps = [
{name: "For below InputOtp, only small alphabets (a to z) are alllowed:", value: "^[a-z]*$"} ,
{name: "For below InputOtp, only small alphabets (a to z) are allowed:", value: "^[a-z]*$"} ,
{name: "For below InputOtp, only big alphabets(A to Z) are allowed:", value: "^[A-Z]*$"}
];
Expand Down
25 changes: 21 additions & 4 deletions apps/docs/content/components/input-otp/usage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
const App = `import {InputOtp} from "@nextui-org/react";
const App = `import {InputOtp, Button} from "@nextui-org/react";
export default function App() {
const [value, setValue] = React.useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("Submitted OTP:", value);
alert("Submitted OTP: " + value);
};
return (
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<InputOtp length={4} />
</div>
<form onSubmit={handleSubmit} className="flex flex-col items-center gap-4">
<InputOtp
length={4}
value={value}
onValueChange={setValue}
color="primary"
size="lg"
/>
<Button type="submit" color="primary">
Verify OTP
</Button>
</form>
);
}`;

Expand Down
2 changes: 0 additions & 2 deletions packages/components/input-otp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ This package contains the InputOTPcomponent.

Please refer to the [documentation](https://nextui.org/docs/components/input-otp) for more information.

> This is an internal utility, not intended for public usage.
## Installation

```sh
Expand Down
20 changes: 9 additions & 11 deletions packages/components/input-otp/__tests__/input-otp.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import {act, fireEvent, render, renderHook} from "@testing-library/react";
import {act, render, renderHook} from "@testing-library/react";
import {useForm} from "react-hook-form";
import userEvent, {UserEvent} from "@testing-library/user-event";

Expand Down Expand Up @@ -208,10 +208,7 @@ describe("InputOtp", () => {
});

it("should call onFill callback when inputOtp is completely filled", async () => {
let testVar = 0;
const onFill = () => {
testVar = 1;
};
const onFill = jest.fn();

render(<InputOtp length={4} onFill={onFill} />);

Expand All @@ -226,7 +223,7 @@ describe("InputOtp", () => {
await user.paste("1234");
});

expect(testVar).toBe(1);
expect(onFill).toHaveBeenCalledTimes(1);
});
});

Expand All @@ -236,6 +233,11 @@ describe("InputOtp with react hook form", () => {
let inputOtp3: Element;
let submitButton: HTMLButtonElement;
let onSubmit: () => void;
let user: UserEvent;

beforeAll(() => {
user = userEvent.setup();
});

beforeEach(() => {
const {result} = renderHook(() =>
Expand Down Expand Up @@ -279,17 +281,13 @@ describe("InputOtp with react hook form", () => {
});

it("should not submit form when required field is empty", async () => {
const user = userEvent.setup();

await user.click(submitButton);

expect(onSubmit).toHaveBeenCalledTimes(0);
});

it("should submit form when required field is not empty", async () => {
fireEvent.change(inputOtp3, {target: {value: "1234"}});

const user = userEvent.setup();
await user.type(inputOtp3, "1234");

await user.click(submitButton);

Expand Down
3 changes: 2 additions & 1 deletion packages/components/input-otp/src/input-otp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const InputOtp = forwardRef<"div", InputOtpProps>((props, ref) => {
))}
</div>
);
}, [getSegmentWrapperProps]);
}, [length, getSegmentWrapperProps]);

const inputSection = useMemo(() => {
return (
Expand Down Expand Up @@ -65,6 +65,7 @@ const InputOtp = forwardRef<"div", InputOtpProps>((props, ref) => {
description,
getHelperWrapperProps,
getErrorMessageProps,
getDescriptionProps,
]);

return (
Expand Down
6 changes: 3 additions & 3 deletions packages/components/input-otp/src/use-input-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
onFill(value);
}
},
[onValueChange],
[onValueChange, onFill, length],
);

const [value, setValue] = useControlledState(
Expand All @@ -137,10 +137,10 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
const onKeyDownCapture = (e: React.KeyboardEvent<HTMLInputElement>) => {
const key = e.key;

if (key == "Backspace") {
if (key === "Backspace") {
return;
}
if (key == "ArrowLeft" || key == "ArrowRight") {
if (key === "ArrowLeft" || key === "ArrowRight") {
e.stopPropagation();
e.preventDefault();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/theme/src/components/input-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ const inputOtp = tv({
segment: "rounded-none",
},
sm: {
segment: "rounded-small",
segment: "rounded-sm",
},
md: {
segment: "rounded-medium",
segment: "rounded-md",
},
lg: {
segment: "rounded-large",
segment: "rounded-lg",
},
full: {
segment: "rounded-full",
Expand Down

0 comments on commit 0166f8f

Please sign in to comment.