简体   繁体   English

如何通过单击 Reactjs 中的按钮将 FormControl 从禁用切换为启用

[英]How can I toggle a FormControl from disabled to enabled with a button click in Reactjs

I'm new to React and just started working with state so please bare with me.我是 React 的新手,刚开始使用 state,所以请与我一起工作。 I also found a lot of similar questions but nothing that really addresses the following:我还发现了很多类似的问题,但没有真正解决以下问题:

I have a reactstrap form that has disabled FormControls.我有一个禁用 FormControls 的 reactstrap 表单。 When the user clicks the 'edit' button the FormControls need to be enabled.当用户单击“编辑”按钮时,需要启用 FormControls。 According to my knowledge, this should be simple with JSX but nothing I've tried has worked.据我所知,使用 JSX 应该很简单,但我尝试过的任何方法都没有奏效。 It might just be a stupid implementation error on my side.对我来说,这可能只是一个愚蠢的实施错误。

 class UserProfile extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      editToggle: false,
      userName: "Star Lord",
      accTier: "Premium",
      credit: "95 855 651", 
      vehicle: "The Milano",
      contact: "Echoe Golf Oscar 12.465Ghz"
    }
    
    // This binding is necessary to make `this` work in the callback
    this.editInputToggle = this.editInputToggle.bind(this);
  }

  editInputToggle() {
    this.setState(prevState => ({
      editToggle: !prevState.editToggle
    }));
  }

  onValueChange = (e) => {
    this.setState({ fullName: e.target.value });
};

  render() {
    const { editToggle } = this.state;

    // We need give the components function a return statement.
    return (
      /* The Component can only return one parent element,
      while parent elements can have many children */
      <div className='profileDetails'>
        {/* Bootstrap Form is used for the form
              Bootstrap Button is used for the edit button */}
        <Form id='formProfile'>
        
          <Form.Group className="mb-3" controlId="formProfileDetails">
            <Form.Label>US3RNAME:</Form.Label>
            <Form.Control type="text" value={this.state.userName}
            onChange={this.onValueChange}  className='user-info' />
            <Form.Label>ACC0uNT TI3R:</Form.Label>
            <Form.Control disabled type="text" value={this.state.accTier} onChange={this.onValueChange} className='user-info' />
            <Form.Label>CR3DiT:</Form.Label>
            <Form.Control disabled type="text" value={this.state.credit} onChange={this.onValueChange} className='user-info' />
            <Form.Label>R3GiSTERED VEHiCL3:</Form.Label>
            <Form.Control disabled type="text" value={this.state.vehicle} onChange={this.onValueChange} className='user-info' />
            <Form.Label>C0NTACT D3TAiLS:</Form.Label>
            <Form.Control disabled type="text" value={this.state.contact} onChange={this.onValueChange} className='user-info' />
          </Form.Group>

          <Button variant="light" size="sm" className='p-2 m-4 headerBtn' onClick={ () => this.editInputToggle(editToggle) }>
            Edit
          </Button>
        </Form>
      </div>
    );
  } 
}

What do I need to do to change "disabled" to ""?我需要做什么才能将“禁用”更改为“”?

For enabling the FormControl,要启用 FormControl,

<Form.Control disabled={!this.state.editToggle} type="text" value={this.state.accTier} onChange={this.onValueChange} className='user-info' />

Try replacing "disabled" with this line: disabled={this.state.editToggle?尝试用以下行替换“禁用”: disabled={this.state.editToggle? "true": "false"} “真假”}

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

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