Skip to content

Commit

Permalink
Add view password toggle to login screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Campbell committed Nov 15, 2020
1 parent 3334517 commit 27d51fa
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/components/LoginForm/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useState } from "react";
import { Box, Paper, TextField, Button, Avatar, Typography, useTheme, makeStyles } from "@material-ui/core";
import { Box, Paper, TextField, Button, Avatar, Typography, InputAdornment, IconButton, useTheme, makeStyles } from "@material-ui/core";
import Visibility from '@material-ui/icons/Visibility';
import VisibilityOff from '@material-ui/icons/VisibilityOff';
import { Redirect } from "react-router-dom";
import { Alert } from '@material-ui/lab';
import { useLogin, useLoginSetter, useTokenSetter } from "../AuthProvider/";
Expand All @@ -9,6 +11,7 @@ export default function LoginForm() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState(false);
const [showPassword, setShowPassword] = useState(false);

const handleUsernameChange = (event) => setUsername(event.target.value);
const handlePasswordChange = (event) => setPassword(event.target.value);
Expand Down Expand Up @@ -74,6 +77,20 @@ export default function LoginForm() {
);
}

const ViewPasswordToggle = _ => {
return (
<InputAdornment position="end">
<IconButton
aria-label="Toggle password visibility."
onClick={ _ => setShowPassword(!showPassword) }
onMouseDown={ (event) => event.preventDefault() }
>
{ showPassword ? <Visibility /> : <VisibilityOff /> }
</IconButton>
</InputAdornment>
);
}

return (
<Box classes={{ root: classes.box_root }}>
<form onSubmit={handleSubmit}>
Expand All @@ -99,7 +116,7 @@ export default function LoginForm() {
variant="outlined"
/>
<TextField
type="password"
type={ showPassword ? "text" : "password"}
id="password"
name="password"
label="Password"
Expand All @@ -108,6 +125,9 @@ export default function LoginForm() {
onChange={handlePasswordChange}
margin="normal"
variant="outlined"
InputProps={{
endAdornment: <ViewPasswordToggle />
}}
/>
<Button
color="primary"
Expand Down

0 comments on commit 27d51fa

Please sign in to comment.