-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c2e54a
commit 0729a10
Showing
13 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
'@tszhong0411/tailwind-config': patch | ||
'@tszhong0411/ui': patch | ||
--- | ||
|
||
Add input-otp component |
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,46 @@ | ||
--- | ||
title: Input OTP | ||
description: Accessible one-time password component with copy paste functionality. | ||
--- | ||
|
||
<ComponentPreview name='input-otp/input-otp' /> | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from '@tszhong0411/ui' | ||
``` | ||
|
||
```tsx | ||
<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> | ||
``` | ||
|
||
## Examples | ||
|
||
### Pattern | ||
|
||
<ComponentPreview name='input-otp/pattern' /> | ||
|
||
### Separator | ||
|
||
<ComponentPreview name='input-otp/separator' /> | ||
|
||
### Controlled | ||
|
||
<ComponentPreview name='input-otp/controlled' /> | ||
|
||
### Form | ||
|
||
<ComponentPreview name='input-otp/form' /> |
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,28 @@ | ||
'use client' | ||
|
||
import { InputOTP, InputOTPGroup, InputOTPSlot } from '@tszhong0411/ui' | ||
import { useState } from 'react' | ||
|
||
const InputOTPControlledDemo = () => { | ||
const [value, setValue] = useState('') | ||
|
||
return ( | ||
<div className='space-y-2'> | ||
<InputOTP maxLength={6} value={value} onChange={setValue}> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={0} /> | ||
<InputOTPSlot index={1} /> | ||
<InputOTPSlot index={2} /> | ||
<InputOTPSlot index={3} /> | ||
<InputOTPSlot index={4} /> | ||
<InputOTPSlot index={5} /> | ||
</InputOTPGroup> | ||
</InputOTP> | ||
<div className='text-center text-sm'> | ||
{value === '' ? <>Enter your one-time password.</> : <>You entered: {value}</>} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default InputOTPControlledDemo |
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,80 @@ | ||
'use client' | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { | ||
Button, | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
InputOTP, | ||
InputOTPGroup, | ||
InputOTPSlot, | ||
toast | ||
} from '@tszhong0411/ui' | ||
import { useForm } from 'react-hook-form' | ||
import { z } from 'zod' | ||
|
||
const FormSchema = z.object({ | ||
pin: z.string().min(6, { | ||
message: 'Your one-time password must be 6 characters.' | ||
}) | ||
}) | ||
|
||
const InputOTPFormDemo = () => { | ||
const form = useForm<z.infer<typeof FormSchema>>({ | ||
resolver: zodResolver(FormSchema), | ||
defaultValues: { | ||
pin: '' | ||
} | ||
}) | ||
|
||
const onSubmit = (data: z.infer<typeof FormSchema>) => { | ||
toast('You submitted the following values:', { | ||
description: ( | ||
<pre className='mt-2 w-[340px] rounded-md bg-zinc-950 p-4'> | ||
<code className='text-white'>{JSON.stringify(data, null, 2)}</code> | ||
</pre> | ||
) | ||
}) | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className='space-y-6'> | ||
<FormField | ||
control={form.control} | ||
name='pin' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>One-Time Password</FormLabel> | ||
<FormControl> | ||
<InputOTP maxLength={6} {...field}> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={0} /> | ||
<InputOTPSlot index={1} /> | ||
<InputOTPSlot index={2} /> | ||
<InputOTPSlot index={3} /> | ||
<InputOTPSlot index={4} /> | ||
<InputOTPSlot index={5} /> | ||
</InputOTPGroup> | ||
</InputOTP> | ||
</FormControl> | ||
<FormDescription> | ||
Please enter the one-time password sent to your phone. | ||
</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<Button type='submit'>Submit</Button> | ||
</form> | ||
</Form> | ||
) | ||
} | ||
|
||
export default InputOTPFormDemo |
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,21 @@ | ||
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from '@tszhong0411/ui' | ||
|
||
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> | ||
) | ||
} | ||
|
||
export default InputOTPDemo |
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,23 @@ | ||
import { | ||
InputOTP, | ||
InputOTPGroup, | ||
InputOTPSlot, | ||
REGEXP_ONLY_DIGITS_AND_CHARS | ||
} from '@tszhong0411/ui' | ||
|
||
const InputOTPPatternDemo = () => { | ||
return ( | ||
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={0} /> | ||
<InputOTPSlot index={1} /> | ||
<InputOTPSlot index={2} /> | ||
<InputOTPSlot index={3} /> | ||
<InputOTPSlot index={4} /> | ||
<InputOTPSlot index={5} /> | ||
</InputOTPGroup> | ||
</InputOTP> | ||
) | ||
} | ||
|
||
export default InputOTPPatternDemo |
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,24 @@ | ||
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from '@tszhong0411/ui' | ||
|
||
const InputOTPSeparatorDemo = () => { | ||
return ( | ||
<InputOTP maxLength={6}> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={0} /> | ||
<InputOTPSlot index={1} /> | ||
</InputOTPGroup> | ||
<InputOTPSeparator /> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={2} /> | ||
<InputOTPSlot index={3} /> | ||
</InputOTPGroup> | ||
<InputOTPSeparator /> | ||
<InputOTPGroup> | ||
<InputOTPSlot index={4} /> | ||
<InputOTPSlot index={5} /> | ||
</InputOTPGroup> | ||
</InputOTP> | ||
) | ||
} | ||
|
||
export default InputOTPSeparatorDemo |
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
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,73 @@ | ||
'use client' | ||
|
||
import { cn } from '@tszhong0411/utils' | ||
import { OTPInput, OTPInputContext } from 'input-otp' | ||
import { MinusIcon } from 'lucide-react' | ||
import { useContext } from 'react' | ||
|
||
type InputOTPProps = React.ComponentProps<typeof OTPInput> | ||
|
||
const InputOTP = (props: InputOTPProps) => { | ||
const { className, containerClassName, ...rest } = props | ||
|
||
return ( | ||
<OTPInput | ||
containerClassName={cn( | ||
'flex items-center gap-2 has-[:disabled]:opacity-50', | ||
containerClassName | ||
)} | ||
className={cn('disabled:cursor-not-allowed', className)} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
type InputOTPGroupProps = React.ComponentProps<'div'> | ||
|
||
const InputOTPGroup = (props: InputOTPGroupProps) => { | ||
const { className, ...rest } = props | ||
|
||
return <div className={cn('flex items-center', className)} {...rest} /> | ||
} | ||
|
||
type InputOTPSlotProps = React.ComponentProps<'div'> & { index: number } | ||
|
||
const InputOTPSlot = (props: InputOTPSlotProps) => { | ||
const { index, className, ...rest } = props | ||
|
||
const inputOTPContext = useContext(OTPInputContext) | ||
|
||
const slot = inputOTPContext.slots[index] | ||
const { char, hasFakeCaret, isActive } = slot ?? {} | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'border-input relative flex size-9 items-center justify-center border-y border-r shadow-sm transition-all', | ||
'first:rounded-l-md first:border-l', | ||
'last:rounded-r-md', | ||
isActive && 'ring-ring z-10 ring-1', | ||
className | ||
)} | ||
{...rest} | ||
> | ||
{char} | ||
{hasFakeCaret && ( | ||
<div className='pointer-events-none absolute inset-0 flex items-center justify-center'> | ||
<div className='animate-caret-blink bg-foreground h-4 w-px duration-1000' /> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
type InputOTPSeparatorProps = React.ComponentProps<'div'> | ||
|
||
const InputOTPSeparator = (props: InputOTPSeparatorProps) => ( | ||
<div role='separator' {...props}> | ||
<MinusIcon /> | ||
</div> | ||
) | ||
|
||
export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } | ||
export { REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS, REGEXP_ONLY_DIGITS_AND_CHARS } from 'input-otp' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.