【发布时间】:2021-06-23 14:58:04
【问题描述】:
我想切换输入字段的显示和隐藏密码。
它可以在单个输入上工作,但我不知道如何为多个输入字段做到这一点
我希望在点击输入图标时它会切换显示/隐藏密码
它应该为每个输入字段单独工作。
例如,如果我单击当前密码输入字段图标,那么它将仅显示/隐藏此输入字段的密码。
const App = () => {
const [values, setValues] = React.useState({
password: "",
showPassword: false,
});
const handleClickShowPassword = () => {
setValues({ ...values, showPassword: !values.showPassword });
};
const handlePasswordChange = (prop) => (event) => {
setValues({ ...values, [prop]: event.target.value });
};
return (
<div
style={{
marginLeft: "30%",
}}
>
<h4>Change your password</h4>
<InputLabel htmlFor="standard-adornment-password">
Current password
</InputLabel>
<Input
type={values.showPassword ? "text" : "password"}
onChange={handlePasswordChange("password")}
value={values.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
<InputLabel htmlFor="standard-adornment-password">
New password
</InputLabel>
<Input
type={values.showPassword ? "text" : "password"}
onChange={handlePasswordChange("password")}
value={values.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
<InputLabel htmlFor="standard-adornment-password">
Confirm password
</InputLabel>
<Input
type={values.showPassword ? "text" : "password"}
onChange={handlePasswordChange("password")}
value={values.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
/>
</div>
);
};
【问题讨论】:
标签: javascript reactjs react-hooks material-ui