我想切换输入字段的显示和隐藏密码。
它可以处理单个输入,但我不知道如何处理多个输入字段
我想在点击输入图标,它将切换为显示/隐藏密码
它应该为每个输入域单独工作。
例如,如果我点击当前密码输入字段图标,那么它将只显示/隐藏此输入字段的密码。
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>
);
};发布于 2021-06-23 23:25:17
你能做这样的事吗?
const Input = (props) => {
const [showPassword, setShowPassword] = React.useState(false)
return (
<div>
<input type={showPassword ? "text" : "password"} {...props} />
<button onMouseDown={(e) => {
e.preventDefault()
setShowPassword(!showPassword)
}}>{showPassword ? 'Hide' : 'Show'} password</button>
</div>
)
}
const reducer = (state, changes) => {
return {
...state,
...changes
}
}
const App = () => {
const [values, setValues] = React.useReducer(reducer, {
currentPassword: "",
newPassword: "",
confirmPassword: ""
});
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>
<label htmlFor="current-password">Current password</label>
<Input id="password-input" onChange={({target}) => {
return setValues({currentPassword: target.value})
}} />
<label htmlFor="new-password">New password</label>
<Input id="new-password" onChange={({target}) => {
return setValues({newPassword: target.value})
}} />
<label htmlFor="confirm-password">Confirm password</label>
<Input id="confirm-password" onChange={({target}) => {
return setValues({confirmPassword: target.value})
}} />
<h4>Form content</h4>
<ul>
<li>currentPassword : {values.currentPassword}</li>
<li>newPassword : {values.newPassword}</li>
<li>confirmPassword : {values.confirmPassword}</li>
</ul>
</div>
);
};
ReactDOM.render(<App />, document.querySelector("#app"))<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>
发布于 2021-06-23 23:32:49
因为您正在使用一个boolean来检查3个字段password。要进行修复,只需更新showPassword以存储string,如下所示:
const handleClickShowPassword = (fieldName) => {
setValues({ ...values, showPassword: fieldName === values.showPassword ? "" : fieldName});
};
...
type={values.showPassword === "currentPassword" ? "text" : "password"}
onClick={() => handleClickShowPassword("currentPassword")}
{values.showPassword === "currentPassword" ? <Visibility /> : <VisibilityOff />}
...
...
type={values.showPassword === "newPassword" ? "text" : "password"}
onClick={() => handleClickShowPassword("newPassword")}
{values.showPassword === "newPassword" ? <Visibility /> : <VisibilityOff />}
...
...
type={values.showPassword === "confirmPassword" ? "text" : "password"}
onClick={() => handleClickShowPassword("confirmPassword")}
{values.showPassword === "confirmPassword" ? <Visibility /> : <VisibilityOff />}
...发布于 2021-06-24 01:12:24
我想出了这个。如果有帮助,请告诉我
const App = () => {
const [values, setValues] = React.useState({
currentPassword:{show:false,password:'' "},
newPassword:{show:false,password:'' "},
confirmPassword{show:false,password:'' "}
});
const handleClickShowPassword = (prop) =>{
Input = values[props]
setValues({ ...values, [prop]: {show:!Input.show, password:Input.password} });
};
const handlePasswordChange = (prop,value) => {
Input = values[props]
setValues({ ...values, [prop]: {show:Input.show,
password:value} });
};
return (
<div
style={{
marginLeft: "30%",
}}
>
<h4>Change your password</h4>
<InputLabel htmlFor="standard-adornment-password">
Current password
</InputLabel>
<Input
type={values.currentPassword.show ? "text" : "password"}
onChange={({target})=>handlePasswordChange("currentPassword",target.value)}
value={values.currentPassword.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.currentPassword.show ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
<InputLabel htmlFor="standard-adornment-password">
New password
</InputLabel>
<Input
type={values.newPassword.show ? "text" : "password"}
onChange={({target})=>handlePasswordChange("newPassword",target.value)}
value={values.newPassword.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.newPassword.show ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
<InputLabel htmlFor="standard-adornment-password">
Confirm password
</InputLabel>
<Input
type={values.confirmPassword.show ? "text" : "password"}
onChange={({target})=>handlePasswordChange("confirmPassword",target.value)}
value={values.confirmPassword.password}
endAdornment={
<InputAdornment position="end">
<IconButton
onClick={handleClickShowPassword}
>
{values.confirmPassword.show ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
/>
</div>
);
};https://stackoverflow.com/questions/68102309
复制相似问题