allow sign in with enter key + create key listener hook
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React, { useState } from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Button, CircularProgress, DialogActions, DialogContent, Typography } from '@mui/material';
|
||||
import { useKeyPress } from 'src/hooks/useKeyPress';
|
||||
import { PasswordInput } from './textfields';
|
||||
|
||||
export const ConfirmPassword = ({
|
||||
@@ -15,6 +16,14 @@ export const ConfirmPassword = ({
|
||||
}) => {
|
||||
const [value, setValue] = useState('');
|
||||
|
||||
const enterKeyPressed = useKeyPress('Enter');
|
||||
|
||||
useEffect(() => {
|
||||
if (enterKeyPressed && !!value.length && !isLoading) {
|
||||
onConfirm(value);
|
||||
}
|
||||
}, [enterKeyPressed]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<DialogContent>
|
||||
@@ -27,6 +36,7 @@ export const ConfirmPassword = ({
|
||||
onUpdatePassword={(pswrd) => setValue(pswrd)}
|
||||
placeholder="Confirm password"
|
||||
autoFocus
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</DialogContent>
|
||||
<DialogActions sx={{ p: 3, pt: 0 }}>
|
||||
|
||||
@@ -39,8 +39,9 @@ export const PasswordInput: React.FC<{
|
||||
label?: string;
|
||||
placeholder?: string;
|
||||
autoFocus?: boolean;
|
||||
disabled?: boolean;
|
||||
onUpdatePassword: (password: string) => void;
|
||||
}> = ({ password, label, placeholder, error, autoFocus, onUpdatePassword }) => {
|
||||
}> = ({ password, label, placeholder, error, autoFocus, disabled, onUpdatePassword }) => {
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
|
||||
return (
|
||||
@@ -54,6 +55,7 @@ export const PasswordInput: React.FC<{
|
||||
onChange={(e) => onUpdatePassword(e.target.value)}
|
||||
type={showPassword ? 'input' : 'password'}
|
||||
autoFocus={autoFocus}
|
||||
disabled={disabled}
|
||||
InputProps={{
|
||||
endAdornment: (
|
||||
<IconButton onClick={() => setShowPassword((show) => !show)}>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
export function useKeyPress(targetKey: string): boolean {
|
||||
// State for keeping track of whether key is pressed
|
||||
const [keyPressed, setKeyPressed] = useState(false);
|
||||
// If pressed key is our target key then set to true
|
||||
function downHandler({ key }: { key: string }): void {
|
||||
if (key === targetKey) {
|
||||
setKeyPressed(true);
|
||||
}
|
||||
}
|
||||
// If released key is our target key then set to false
|
||||
const upHandler = ({ key }: { key: string }): void => {
|
||||
if (key === targetKey) {
|
||||
setKeyPressed(false);
|
||||
}
|
||||
};
|
||||
// Add event listeners
|
||||
useEffect(() => {
|
||||
window.addEventListener('keydown', downHandler);
|
||||
window.addEventListener('keyup', upHandler);
|
||||
// Remove event listeners on cleanup
|
||||
return () => {
|
||||
window.removeEventListener('keydown', downHandler);
|
||||
window.removeEventListener('keyup', upHandler);
|
||||
};
|
||||
}, []); // Empty array ensures that effect is only run on mount and unmount
|
||||
return keyPressed;
|
||||
}
|
||||
@@ -31,27 +31,24 @@ export const SignInMnemonic = () => {
|
||||
<Stack spacing={2} alignItems="center" minWidth="50%">
|
||||
<Subtitle subtitle="Enter a mnemonic to sign in" />
|
||||
<FormControl fullWidth>
|
||||
<Stack spacing={2}>
|
||||
<MnemonicInput mnemonic={mnemonic} onUpdateMnemonic={(mnc) => setMnemonic(mnc)} error={error} />
|
||||
<Button
|
||||
variant="contained"
|
||||
size="large"
|
||||
fullWidth
|
||||
onClick={() => logIn({ type: 'mnemonic', value: mnemonic })}
|
||||
>
|
||||
Sign in with mnemonic
|
||||
</Button>
|
||||
<Box display="flex" justifyContent={passwordExists ? 'center' : 'space-between'}>
|
||||
<Button color="inherit" onClick={() => handlePageChange('/existing-account')}>
|
||||
Back
|
||||
<form onSubmit={() => logIn({ type: 'mnemonic', value: mnemonic })}>
|
||||
<Stack spacing={2}>
|
||||
<MnemonicInput mnemonic={mnemonic} onUpdateMnemonic={(mnc) => setMnemonic(mnc)} error={error} />
|
||||
<Button variant="contained" size="large" fullWidth type="submit">
|
||||
Sign in with mnemonic
|
||||
</Button>
|
||||
{!passwordExists && (
|
||||
<Button color="info" onClick={() => handlePageChange('/confirm-mnemonic')}>
|
||||
Create a password
|
||||
<Box display="flex" justifyContent={passwordExists ? 'center' : 'space-between'}>
|
||||
<Button color="inherit" onClick={() => handlePageChange('/existing-account')}>
|
||||
Back
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
</Stack>
|
||||
{!passwordExists && (
|
||||
<Button color="info" onClick={() => handlePageChange('/confirm-mnemonic')}>
|
||||
Create a password
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
</Stack>
|
||||
</form>
|
||||
</FormControl>
|
||||
</Stack>
|
||||
);
|
||||
|
||||
@@ -14,46 +14,48 @@ export const SignInPassword = () => {
|
||||
<Stack spacing={2} alignItems="center" minWidth="50%">
|
||||
<Subtitle subtitle="Enter a password to sign in" />
|
||||
<FormControl fullWidth>
|
||||
<Stack spacing={2}>
|
||||
<PasswordInput
|
||||
label="Enter password"
|
||||
password={password}
|
||||
onUpdatePassword={(pswd) => setPassword(pswd)}
|
||||
error={error}
|
||||
autoFocus
|
||||
/>
|
||||
<Button
|
||||
variant="contained"
|
||||
size="large"
|
||||
fullWidth
|
||||
onClick={() => logIn({ type: 'password', value: password })}
|
||||
>
|
||||
Sign in with password
|
||||
</Button>
|
||||
<Box display="flex" justifyContent="space-between">
|
||||
<form onSubmit={() => logIn({ type: 'password', value: password })}>
|
||||
<Stack spacing={2}>
|
||||
<PasswordInput
|
||||
label="Enter password"
|
||||
password={password}
|
||||
onUpdatePassword={(pswd) => setPassword(pswd)}
|
||||
error={error}
|
||||
autoFocus
|
||||
/>
|
||||
<Button
|
||||
color="inherit"
|
||||
disableElevation
|
||||
onClick={() => {
|
||||
setError(undefined);
|
||||
navigate('/existing-account');
|
||||
}}
|
||||
variant="contained"
|
||||
size="large"
|
||||
fullWidth
|
||||
onClick={() => logIn({ type: 'password', value: password })}
|
||||
>
|
||||
Back
|
||||
Sign in with password
|
||||
</Button>
|
||||
<Box display="flex" justifyContent="space-between">
|
||||
<Button
|
||||
color="inherit"
|
||||
disableElevation
|
||||
onClick={() => {
|
||||
setError(undefined);
|
||||
navigate('/existing-account');
|
||||
}}
|
||||
>
|
||||
Back
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
color="info"
|
||||
onClick={() => {
|
||||
setError(undefined);
|
||||
navigate('/sign-in-mnemonic');
|
||||
}}
|
||||
size="small"
|
||||
>
|
||||
Forgotten password?
|
||||
</Button>
|
||||
</Box>
|
||||
</Stack>
|
||||
<Button
|
||||
color="info"
|
||||
onClick={() => {
|
||||
setError(undefined);
|
||||
navigate('/sign-in-mnemonic');
|
||||
}}
|
||||
size="small"
|
||||
>
|
||||
Forgotten password?
|
||||
</Button>
|
||||
</Box>
|
||||
</Stack>
|
||||
</form>
|
||||
</FormControl>
|
||||
</Stack>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user