0

I am trying to create a way for a user to multiply their form.

A user may add several "Name"/"Number" at one time. But they will only add it under one "Area"

How do I create a form where I can "duplicate" the input fields?

It will, in a way, simply "copy" the form so you can enter the data again.

This screenshot should explain:

enter image description here

I've also put it in a code pen:

http://codepen.io/yarnball/pen/qaQomG?editors=1010

var Postapi = React.createClass({
  getInitialState: function() {
  return {
    area:'',
    name: '',
    number: '',
  };
  },
  onChange(e) { 
    this.setState({[e.target.name]: e.target.value})
              },

  handleSubmit: function(e) {
     e.preventDefault();
     return fetch('http://localhost:8000/api/Data/', {
     method: 'POST',
     body: JSON.stringify({
         area: this.state.area,
         info:[
           {name:this.state.name,
           taglevel:this.state.number}
          ],
      })
      })
      .then(function(res){ return res.json(); })
      .then(function(data){ alert( JSON.stringify( data ) ) })
  },

  render: function() {
    return (
      <form onSubmit={this.handleSubmit}>
      <input
        name="area"
        type="text"
        placeholder="Area"
        onChange={this.onChange}
      />
      <input
        name="name"
        type="text"
        placeholder="Name"
        onChange={this.onChange}
      />
      <input
        name="number"
        type="text"
        placeholder="Number"
        onChange={this.onChange}
      />
       <button type="submit">Submit</button>
      </form>
    );
  }
});
3
  • But what is your question? Commented Oct 23, 2016 at 13:41
  • Basically I want to create a form where it can duplicate the input fields and fill the body of the fetch accordingly. I created a screenshot to hopefully make it clear. Commented Oct 23, 2016 at 14:15
  • 1
    you should try redux-form if you are not planning to write yet another lib. Commented Oct 23, 2016 at 17:34

1 Answer 1

1

Working pen: http://codepen.io/pranesh-r/pen/qagmxX?editors=1010

  • You need state to maintain the number of input block to be displayed. When the user clicks add more, increment the state and render the n number of input blocks. This solves your first problem.

Handler

addMore(){
 this.setState({
   formItems: this.state.formItems + 1
 })
},

HTML

<button type="button" onClick={this.addMore}>Add More</button>

Dynamic content

const fi = <div>
        <input
          name="name"
          type="text"
          placeholder="Name"
          onChange={this.onChange}
        />
        <input
          name="number"
          type="text"
          placeholder="Number"
          onChange={this.onChange}
        />
      </div>
      const formItem = [];
      for (var i= 0; i< this.state.formItems; i++){
        formItem.push(fi)
      }

Use the formItem in the render.

  • To serialize the form, I've used jQuery .serialize(). If you don't wanna use jQuery you need to maintain the values of the input in the state for the each inputs. And during submit, get the value from the state and pass it to the server. This solves your second problem.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks- this helps! I'm not sure whether I should use jQuery (never used it) but ideally I just need to pass the values to my server like in my fetch. How do I do the post of the multiple fields?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.