有条件地在 React 中输入所需的输入

Pav*_*ndu 3 forms required reactjs

在我的 React 应用程序中的表单中,如果类别设置为滚动,我想制作一组必需的输入(长度和规格) 。我该怎么做?提前致谢。

 <Form.Group as={Col}>
   <label>Category</label>
   <Form.Control
     as="select"
     name="category"
     defaultValue={this.state.category}
     onChange={this.catControl}
   >
    <option>printed</option>
    <option>roll</option>
  </Form.Control>
</Form.Group>

<Form.Row>
 <Form.Group as={Col}>
   <label>Length(cm)</label>
   //required if category is set to "roll". How can I do that?
   <Form.Control name="length" defaultValue={this.state.length} />
 </Form.Group>

 <Form.Group as={Col}>
   <label>Gauge(mm)</label>
   <Form.Control name="gauge" defaultValue={this.state.gauge} />
 </Form.Group>
</Form.Row>
Run Code Online (Sandbox Code Playgroud)

Dee*_*pak 5

在 html 部分,您可以执行以下操作:

 <Form.Control name="length" defaultValue={this.state.length} required={ this.state.category==='roll'}/>
Run Code Online (Sandbox Code Playgroud)