-
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.
refactor: add ui component for password to follow single responsibili…
…ty principle
- Loading branch information
Showing
5 changed files
with
87 additions
and
86 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 |
---|---|---|
@@ -1,88 +1,37 @@ | ||
import type { InputHTMLAttributes } from 'react' | ||
import type { FieldErrors } from 'react-hook-form' | ||
|
||
import { forwardRef, useState } from 'react' | ||
import { FiEye, FiEyeOff } from 'react-icons/fi' | ||
import { forwardRef } from 'react' | ||
|
||
import { cn } from 'lib' | ||
|
||
type FieldProps = InputHTMLAttributes<HTMLInputElement> & { | ||
inputName: string | ||
isPasswordInput?: boolean | ||
inputPasswordPlaceholder?: string | ||
errors: FieldErrors | ||
} | ||
|
||
export const Field = forwardRef<HTMLInputElement, FieldProps>( | ||
( | ||
{ | ||
className, | ||
inputName, | ||
isPasswordInput, | ||
inputPasswordPlaceholder, | ||
errors, | ||
...props | ||
}, | ||
ref | ||
) => { | ||
const [showPassword, setShowPassword] = useState(false) | ||
|
||
return ( | ||
<> | ||
{isPasswordInput ? ( | ||
<div className='relative'> | ||
<input | ||
type={showPassword ? 'text' : 'password'} | ||
placeholder={inputPasswordPlaceholder} | ||
className={cn( | ||
`hide-password-toggle peer h-2xl w-full rounded-lg border border-brand | ||
border-opacity-40 bg-transparent px-lg pr-[35px] outline-none | ||
placeholder:opacity-40 autofill:bg-clip-text autofill:text-fill-black | ||
focus:border-opacity-100 violet:border-brand-secondary violet:border-opacity-40 | ||
violet:focus:border-opacity-100 dark:autofill:text-fill-white`, | ||
className, | ||
{ | ||
'mb-2': errors[inputName], | ||
'mb-6': !errors[inputName] | ||
} | ||
)} | ||
{...props} | ||
ref={ref} | ||
/> | ||
<button | ||
type='button' | ||
className='focus-visible:styled-outline absolute right-lg top-4 opacity-40 | ||
peer-[.text-white]:text-white' | ||
onClick={() => setShowPassword(prev => !prev)}> | ||
{showPassword ? ( | ||
<FiEyeOff className='size-lg' /> | ||
) : ( | ||
<FiEye className='size-lg' /> | ||
)} | ||
</button> | ||
</div> | ||
) : ( | ||
<input | ||
type='text' | ||
className={cn( | ||
`mb-3.5 h-2xl w-full rounded-lg border border-brand border-opacity-40 | ||
bg-transparent px-lg outline-none placeholder:opacity-40 autofill:bg-clip-text | ||
autofill:text-fill-black focus:border-opacity-100 violet:border-brand-secondary | ||
violet:border-opacity-40 violet:focus:border-opacity-100 | ||
dark:autofill:text-fill-white`, | ||
className, | ||
errors[inputName] && 'mb-2' | ||
)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
)} | ||
{errors[inputName] && ( | ||
<p className='mb-3.5 text-red-600'> | ||
{errors[inputName]?.message as string} | ||
</p> | ||
({ className, inputName, errors, ...props }, ref) => ( | ||
<> | ||
<input | ||
type='text' | ||
className={cn( | ||
`mb-3.5 h-2xl w-full rounded-lg border border-brand border-opacity-40 | ||
bg-transparent px-lg outline-none placeholder:opacity-40 autofill:bg-clip-text | ||
autofill:text-fill-black focus:border-opacity-100 violet:border-brand-secondary | ||
violet:border-opacity-40 violet:focus:border-opacity-100 | ||
dark:autofill:text-fill-white`, | ||
className, | ||
errors[inputName] && 'mb-2' | ||
)} | ||
</> | ||
) | ||
} | ||
ref={ref} | ||
{...props} | ||
/> | ||
{errors[inputName] && ( | ||
<p className='mb-3.5 text-red-600'> | ||
{errors[inputName]?.message as string} | ||
</p> | ||
)} | ||
</> | ||
) | ||
) |
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,55 @@ | ||
import type { InputHTMLAttributes } from 'react' | ||
import type { FieldErrors } from 'react-hook-form' | ||
|
||
import { forwardRef, useState } from 'react' | ||
import { FiEye, FiEyeOff } from 'react-icons/fi' | ||
|
||
import { cn } from 'lib' | ||
|
||
type PasswordFieldProps = InputHTMLAttributes<HTMLInputElement> & { | ||
errors: FieldErrors | ||
} | ||
|
||
export const PasswordField = forwardRef<HTMLInputElement, PasswordFieldProps>( | ||
({ className, errors, ...props }, ref) => { | ||
const [showPassword, setShowPassword] = useState(false) | ||
|
||
return ( | ||
<> | ||
<div className='relative'> | ||
<input | ||
type={showPassword ? 'text' : 'password'} | ||
className={cn( | ||
`hide-password-toggle peer h-2xl w-full rounded-lg border border-brand | ||
border-opacity-40 bg-transparent px-lg pr-[35px] outline-none | ||
placeholder:opacity-40 autofill:bg-clip-text autofill:text-fill-black | ||
focus:border-opacity-100 violet:border-brand-secondary violet:border-opacity-40 | ||
violet:focus:border-opacity-100 dark:autofill:text-fill-white`, | ||
className, | ||
errors?.password && 'mb-2', | ||
!errors?.password && 'mb-6' | ||
)} | ||
{...props} | ||
ref={ref} | ||
/> | ||
<button | ||
type='button' | ||
className='focus-visible:styled-outline absolute right-lg top-4 opacity-40 | ||
peer-[.text-white]:text-white' | ||
onClick={() => setShowPassword(prev => !prev)}> | ||
{showPassword ? ( | ||
<FiEyeOff className='size-lg' /> | ||
) : ( | ||
<FiEye className='size-lg' /> | ||
)} | ||
</button> | ||
</div> | ||
{errors?.password && ( | ||
<p className='mb-3.5 text-red-600'> | ||
{errors?.password?.message as string} | ||
</p> | ||
)} | ||
</> | ||
) | ||
} | ||
) |
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