React-基于计数的动态输入字段

时间:2019-05-30 06:54:24

标签: reactjs

我想基于用户输入的计数创建文本字段。我经历了许多相关问题,找不到想要的结果。

例如:如果用户输入5作为输入。我想创建5个文本框来输入5个人的姓名和手机。

我该如何实现?

TextField获取计数

  <GridItem xs={12} sm={12} md={12}>
                      <TextField
                        id="sp_Inhouse"
                        label="Number of Pilots Available "
                        type="number"
                        fullWidth
                        className={classes.textField}
                        value={this.state.sp_Inhousecount}
                        error={!!this.state.sp_InhouseError}
                        helperText={this.state.sp_InhouseError}
                        onChange={this.handleChangeInhouse}
                        margin="normal"
                        required
                      />
                    </GridItem>

尝试基于按钮单击添加文本框。工作正常。但我希望得到解释的结果。

                    <GridItem xs={12} sm={12} md={12}>
                       <Button color="primary" onClick={this.handleCreatePilots}>
                      <AddIcon />  Add Pilots
                      </Button>
                      {this.state.inhouse.map((index) => {
                        return (
                          <div key={index}>
                          <TextField
                            id="sp_Name"

                            label="Name "
                            type="number"
                            fullWidth
                          />
 <TextField
                            id="sp_Mobile"

                            label="Name "
                            type="number"
                            fullWidth
                          />
                          </div>
                        )
                      })
                      }
                    </GridItem>
 handleCreatePilots=()=>{
    this.setState({
      inhouse:[...this.state.inhouse,'']
    })
  }

2 个答案:

答案 0 :(得分:2)

Here is the solution. Other refactor may be required.

Working link https://jsfiddle.net/ogmv3wpu/1/

class Hello extends React.Component {
 constructor() {
  super();
  this.state= {
   inputSize: 0
  }
 }
 
 handleOnChange(value){
 this.setState({
 inputSize: value.target.value
 });
 }
 
 renderInputs(value){
  const inputs=[];
  for(let i=0; i<value; i++){
    inputs.push(<div key={i}><input  value={Name}}type="text" name="quantity"/></div>)
  }
  return inputs;
 }
 
  render() {
  console.log(this.state.inputSize);
    return (<div>
    <input type="number" name="quantity" min="0" max="99999" onChange={(value)=>this.handleOnChange(value)}/>
    <div>
    {this.renderInputs(this.state.inputSize)}
    </div>
    </div>
    )
  }
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

答案 1 :(得分:1)

浏览用户输入的号码。

df["1.234"]

渲染输入

const inputs = [];

for (let i = 1; i <= this.state.total; i++) {
  inputs.push(
    <input name={`input-${i}`} onChange={this.onChange} />
  )
}

render() {
  return (
   ....
    {inputs}
  ...
  )
}