Skip to content

Commit

Permalink
feat: add key down listener to login/sign up on enter key
Browse files Browse the repository at this point in the history
  • Loading branch information
yunruu authored and Daviancold committed Nov 9, 2024
1 parent 335efca commit f09194d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 13 additions & 1 deletion frontend/components/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export default function Login() {
}
}

const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>): void => {
if (e.key === 'Enter') {
onLogin()
}
}

return (
<>
<InputField
Expand All @@ -87,14 +93,20 @@ export default function Login() {
error={formErrors.loginPassword}
className="w-full py-3 px-3 border bg-[#EFEFEF] rounded-[5px]"
page="auth"
handleKeyDown={handleKeyDown}
/>

{isLoading ? (
<Button disabled variant="primary" className="w-full text-md mt-5 h-[42px]">
<Loader2 className="mr-2 h-6 w-6 animate-spin" />
</Button>
) : (
<Button onClick={onLogin} variant="primary" className="w-full text-md mt-5 h-[42px]">
<Button
onClick={onLogin}
variant="primary"
className="w-full text-md mt-5 h-[42px]"
onKeyDown={handleKeyDown}
>
Login
</Button>
)}
Expand Down
14 changes: 13 additions & 1 deletion frontend/components/auth/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export default function Signup({ handleSignUpSuccessful }: { handleSignUpSuccess
}
}

const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>): void => {
if (e.key === 'Enter') {
onSignup()
}
}

return (
<>
<InputField
Expand Down Expand Up @@ -102,14 +108,20 @@ export default function Signup({ handleSignUpSuccessful }: { handleSignUpSuccess
error={formErrors.confirmPassword}
className="w-full py-3 px-3 border bg-[#EFEFEF] rounded-[5px]"
page="auth"
handleKeyDown={handleKeyDown}
/>

{isLoading ? (
<Button disabled variant="primary" className="w-full text-md mt-5 h-[42px]">
<Loader2 className="mr-2 h-6 w-6 animate-spin" />
</Button>
) : (
<Button onClick={onSignup} variant="primary" className="w-full text-md mt-5 h-[42px]">
<Button
onClick={onSignup}
variant="primary"
className="w-full text-md mt-5 h-[42px]"
onKeyDown={handleKeyDown}
>
Sign Up
</Button>
)}
Expand Down
2 changes: 2 additions & 0 deletions frontend/components/customs/custom-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface InputFieldProps {
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void
className?: string
page?: string
handleKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void
}

interface OptionsFieldProps {
Expand Down Expand Up @@ -43,6 +44,7 @@ export const InputField = (props: InputFieldProps): JSX.Element => {
? props.className
: 'mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-purple-500 focus:border-purple-500 sm:text-sm'
}
onKeyDown={props.handleKeyDown}
/>
{props.icon && (
<span
Expand Down

0 comments on commit f09194d

Please sign in to comment.