首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在React中切换显示和隐藏多个字段密码

如何在React中切换显示和隐藏多个字段密码
EN

Stack Overflow用户
提问于 2021-06-23 22:58:04
回答 3查看 942关注 0票数 0

我想切换输入字段的显示和隐藏密码。

它可以处理单个输入,但我不知道如何处理多个输入字段

我想在点击输入图标,它将切换为显示/隐藏密码

它应该为每个输入域单独工作。

例如,如果我点击当前密码输入字段图标,那么它将只显示/隐藏此输入字段的密码。

代码语言:javascript
复制
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>
  );
};
EN

回答 3

Stack Overflow用户

发布于 2021-06-23 23:25:17

你能做这样的事吗?

代码语言:javascript
复制
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"))
代码语言:javascript
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2021-06-23 23:32:49

因为您正在使用一个boolean来检查3个字段password。要进行修复,只需更新showPassword以存储string,如下所示:

代码语言:javascript
复制
  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 />}
...
票数 0
EN

Stack Overflow用户

发布于 2021-06-24 01:12:24

我想出了这个。如果有帮助,请告诉我

代码语言:javascript
复制
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>
  );
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68102309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档