简体   繁体   English

使用 React Hooks 单击编辑按钮时禁用输入字段

[英]Disable input field when click edit button using React Hooks

I don't understand what I'm doing wrong in here.我不明白我在这里做错了什么。 The scenario is that I am on the user profile details and I want to edit the information but one input must be disable at all the time.场景是我在用户配置文件详细信息上,我想编辑信息,但必须始终禁用一个输入。 When I'm loading the page the input it is actually disabled but as soon as I click the edit button it is becoming editable like the rest of the other input.当我加载页面时,输入实际上被禁用,但只要我单击编辑按钮,它就会像其他输入的 rest 一样变得可编辑。 Any suggestion on how to make it works, please?请就如何使其工作提出任何建议?

const [disabled, setDisabled] = useState(false);

const handleClickEditMember = () => {
  Actions.enableMemberEdit();
  setDisabled(!disabled);
};

<Card className={clsx(classes.root, className)}>
  <CardHeader
    action={
      <Button
        color="primary"
        id="edit-member-button"
        onClick={() => handleClickEditMember()}
        size="small"
        variant="contained"
      >
        {t('members.edit')}
      </Button>
    }
    title={member.companyName}
  />
  <Divider />
  <CardContent className={classes.content}>
    <CardHeader title={t('members.company_profile')} />
    <Grid container spacing={3}>
      <Grid item xs={6}>
        <TextField
          className={classes.textField}
          fullWidth
          id="companyName"
          InputProps={{
            readOnly: true,
          }}
          label={t('members.company_name')}
          margin="dense"
          name="companyName"
          placeholder={t('members.company_name')}
          value={member.companyName}
          variant="outlined"
        />
      </Grid>
      <Grid item xs={3}>
        <TextField
          className={classes.textField}
          fullWidth
          id="Id"
          InputProps={{
            readOnly: true,
          }}
          disabled={!disabled}
          label={t('members.id')}
          margin="dense"
          name="Id"
          placeholder={t('members.id')}
          value={member.id}
          variant="outlined"
        />
      </Grid>
    </Grid>
<TextField
    className={classes.textField}
    fullWidth
    id="Id"
    InputProps={{
        readOnly: true,
    }}
    **disabled**
    label={t('members.id')}
    margin="dense"
    name="Id"
    placeholder={t('members.id')}
    value={member.id}
    variant="outlined"
/>

Just disable the textfield.只需禁用文本字段。 Don't assign any value to disabled property不要为禁用属性分配任何值

You're updating the state everytime in the handleClickEditMember() by doing setDisabled(;disabled);您每次在handleClickEditMember()中通过执行setDisabled(;disabled); which will eventually set to true as its false initially.最终将设置为 true,因为它最初是 false。

So you'll have to所以你必须

  • Remove that piece of code and the state update won't be triggered again!去掉那段代码,state更新就不会再触发了!
  • And For the ID input field just use disabled={disabled}对于 ID 输入字段,只需使用disabled={disabled}

Just resolved by doing this:只需这样做即可解决:

InputProps={{
  readOnly: memberId ? true : false,
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM