Skip to content

Commit

Permalink
Feat/input focus (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickimoore authored Apr 26, 2023
1 parent 82bdd5b commit 9889a59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
useState,
ClipboardEvent,
ChangeEvent,
useRef,
useEffect,
} from 'react'
import Typography from '../Typography/Typography'
import { UiMode } from '../../constants/enums'
Expand All @@ -23,6 +25,7 @@ export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
isDisablePaste?: boolean
inputStyle?: 'primary' | 'secondary'
icon?: string
isAutoFocus?: boolean
}

const Input: FC<InputProps> = ({
Expand All @@ -40,8 +43,10 @@ const Input: FC<InputProps> = ({
isDisablePaste,
icon,
onChange,
isAutoFocus,
...props
}) => {
const inputRef = useRef<HTMLInputElement | null>(null)
const [inputType, setType] = useState<HTMLInputTypeAttribute>(type || 'text')
const isPasswordType = type === 'password'
const sanitizeInput = (e: ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -76,6 +81,20 @@ const Input: FC<InputProps> = ({
}

const togglePassword = () => setType((prev) => (prev === 'password' ? 'text' : 'password'))

useEffect(() => {
if (isAutoFocus && inputRef.current) {
const timeoutId = setTimeout(() => {
if (inputRef.current) {
inputRef.current.focus()
}
}, 200)
return () => {
clearTimeout(timeoutId)
}
}
}, [isAutoFocus, inputRef])

return (
<div className='space-y-4 w-full'>
{label && (
Expand Down Expand Up @@ -104,6 +123,7 @@ const Input: FC<InputProps> = ({
<div className='relative w-full'>
<input
{...props}
ref={inputRef}
onChange={(value) => onChange?.(sanitizeInput(value))}
onPaste={handlePaste}
type={inputType}
Expand Down
1 change: 1 addition & 0 deletions src/components/SessionAuthModal/SessionAuthModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const SessionAuthModal: FC<SessionAuthModalProps> = ({
<>
<Typography type='text-caption1'>{t('sessionAuthModal.passwordPrompt')}</Typography>
<Input
isAutoFocus={isOpen}
onKeyDown={handleKeyDown}
uiMode={mode}
type='password'
Expand Down

0 comments on commit 9889a59

Please sign in to comment.