My code works properly and when I write in the password field, the text is hidden. Is there any way to add a functionality where the user has the option to view the password if he/she wants?
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<div>
<div className='main-content'>
<form className="form" noValidate autoComplete="off">
{[{ label: "Email", state: email , type: "text", function: setEmail},
{ label: "Password", state: password , type: "password", function: setPassword},
].map((item, index) => (
<div>
<TextField
id="outlined-basic"
key={index}
label={item.label}
variant="outlined"
type= {item.type}
onChange= {e => item.function(e.target.value)}
/>
<br></br><br></br>
</div>
)
)}
</form>
</div>
</div>
);
}
You can change the type of the value based on some true/false state.
// Add these variables to your component to track the state
const [showPassword, setShowPassword] = useState(false);
const handleClickShowPassword = () => setShowPassword(!showPassword);
const handleMouseDownPassword = () => setShowPassword(!showPassword);
// Then you can write your text field component like this to make the toggle work.
<TextField
label='Some label'
variant="outlined"
type={showPassword ? "text" : "password"} // <-- This is where the magic happens
onChange={someChangeHandler}
InputProps={{ // <-- This is where the toggle button is added.
endAdornment: (
<InputAdornment position="end">
<IconButton
aria-label="toggle password visibility"
onClick={handleClickShowPassword}
onMouseDown={handleMouseDownPassword}
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
)
}}
/>
In your example you use a loop to go through your field. Replacing your text field with my example would add the toggle to all the fields. Which is (probably) not what you want, so you would have to find a different way to render those fields.
// Required imports from the example.
import { TextField, InputAdornment, IconButton } from "@material-ui/core";
import Visibility from "@material-ui/icons/Visibility";
import VisibilityOff from "@material-ui/icons/VisibilityOff";
Well, I guess something like this. It won't work well with multiple password fields.
const [showPassword, setShowPassword] = useState(false);
const handleClick = () => {
setShowPassword(prev => !prev);
}
return (
<form className="form" noValidate autoComplete="off">
{[
{
label: "Email",
state: email,
type: "text",
function: setEmail
},
{
label: "Password",
state: password,
type: "password",
function: setPassword,
},
].map((item, index) => (
<div>
<TextField
InputProps={{
endAdornment: item.type ? 'password' (
<InputAdornment position="end">
<IconButton
onClick={handleClick}
edge="end"
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
) : null
}}
id="outlined-basic"
key={index}
label={item.label}
variant="outlined"
type={showPassword ? 'text' : item.type}
onChange= {e => item.function(e.target.value)}
/>
<br></br><br></br>
</div>
))
}
</form>
you can use the Input element of material UI that provides the facility to add an icon at the end of the text field and you can play with one click of the icon to hide and show password
here is how it looks:
hidden password mode
show password mode
<FormControl className={clsx(classes.margin, classes.textField)}>
<InputLabel htmlFor="standard-adornment-password">Password</InputLabel>
<Input
id="standard-adornment-password"
type={values.showPassword ? 'text' : 'password'}
value={values.password}
onChange={handleChange('password')}
endAdornment={
<InputAdornment position="end">
<IconButton
aria-label="toggle password visibility"
onClick={handleClickShowPassword}
onMouseDown={handleMouseDownPassword}
>
{values.showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
/>
</FormControl>
ref link: https://material-ui.com/components/text-fields/#InputAdornments.js
You can add a "Show" button next to the password field selecting which the type of the input changes from type=password
to type=text
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With