-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from CQCL/shadcn-input-otp
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use client" | ||
|
||
import * as React from "react" | ||
import { OTPInput, OTPInputContext } from "input-otp" | ||
import { Minus } from "lucide-react" | ||
|
||
import { cn } from "src" | ||
|
||
const InputOTP = React.forwardRef< | ||
React.ElementRef<typeof OTPInput>, | ||
React.ComponentPropsWithoutRef<typeof OTPInput> | ||
>(({ className, containerClassName, ...props }, ref) => ( | ||
<OTPInput | ||
ref={ref} | ||
containerClassName={cn( | ||
"flex items-center gap-2 has-[:disabled]:opacity-50", | ||
containerClassName | ||
)} | ||
className={cn("disabled:cursor-not-allowed", className)} | ||
{...props} | ||
/> | ||
)) | ||
InputOTP.displayName = "InputOTP" | ||
|
||
const InputOTPGroup = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> | ||
>(({ className, ...props }, ref) => ( | ||
<div ref={ref} className={cn("flex items-center", className)} {...props} /> | ||
)) | ||
InputOTPGroup.displayName = "InputOTPGroup" | ||
|
||
const InputOTPSlot = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> & { index: number } | ||
>(({ index, className, ...props }, ref) => { | ||
const inputOTPContext = React.useContext(OTPInputContext) | ||
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index] | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
className={cn( | ||
"relative flex h-9 w-9 items-center justify-center border-y border-r border-input text-sm shadow-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md", | ||
isActive && "z-10 ring-1 ring-ring", | ||
className | ||
)} | ||
{...props} | ||
> | ||
{char} | ||
{hasFakeCaret && ( | ||
<div className="pointer-events-none absolute inset-0 flex items-center justify-center"> | ||
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" /> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
}) | ||
InputOTPSlot.displayName = "InputOTPSlot" | ||
|
||
const InputOTPSeparator = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> | ||
>(({ ...props }, ref) => ( | ||
<div ref={ref} role="separator" {...props}> | ||
<Minus /> | ||
</div> | ||
)) | ||
InputOTPSeparator.displayName = "InputOTPSeparator" | ||
|
||
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
InputOTP, | ||
InputOTPGroup, | ||
InputOTPSeparator, | ||
InputOTPSlot, | ||
} from "src" | ||
|
||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
|
||
|
||
const InputOTPDemo = () => { | ||
return ( | ||
<InputOTP maxLength={6}> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={0} /> | ||
<InputOTPSlot index={1} /> | ||
<InputOTPSlot index={2} /> | ||
</InputOTPGroup> | ||
<InputOTPSeparator /> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={3} /> | ||
<InputOTPSlot index={4} /> | ||
<InputOTPSlot index={5} /> | ||
</InputOTPGroup> | ||
</InputOTP> | ||
|
||
); | ||
}; | ||
const meta: Meta<typeof InputOTPDemo> = { | ||
component: InputOTPDemo, | ||
|
||
}; | ||
|
||
export default meta; | ||
|
||
export const Default: StoryObj<typeof InputOTPDemo> = { | ||
args: {}, | ||
}; |