Matt
Matt

Reputation: 1609

Adding multiple form fields to array in React

I have a React form that currently stores one form fields value in the items array. However, when adding more than one field, I can't get the content of the other fields to be stored in the array as well. It currently stores the value of the First Name input, but can't figure out the Last Name and Phone fields. The data is then rendered to the items array to a 3 column table, but can't get the other fields to show in their respective columns.

Contacts.js

import ContactList from "./ContactList";

class Contacts extends Component {
  constructor(props) {
    super(props);

    this.state = {
      items: []
    };

    this.addItem = this.addItem.bind(this);
    this.deleteItem = this.deleteItem.bind(this);
  }

  addItem(e) {
    if(this._inputElement.value !== "") {
      var newItem = {
        firstname: this._inputElement.value,
        lastname: this._inputElement2.value,
        phonename: this._inputElement3.value,
        key: Date.now()
      };

      this.setState((prevState) => {
        return {
          items: prevState.items.concat(newItem)
        };
      });

      this._inputElement.value = "";
      this._inputElement2.value = "";
      this._inputElement3.value = "";
    }
    console.log(this.state.items);
    e.preventDefault();
  }
  deleteItem(key) {
    var filteredItems = this.state.items.filter(function (item) {
      return (item.key !== key);
    });

    this.setState({
      items: filteredItems
    });
  }
  render () {
    return (
      <Panel>
        <Tabs onChange={this.onChange} defaultSelectedIndex={0} justified={true}>
          <Tab value="pane-1" label="Add Contact" onActive={this.onActive}>
            <Form onSubmit={this.addItem}>
              <input ref={(a) => this._inputElement = a}
                      placeholder="First Name" />
              <input ref={(a) => this._inputElement2 = a}
                      placeholder="Last Name" />
              <input ref={(a) => this._inputElement3 = a}
                      placeholder="Phone" />
              <Button variant="raised">Add</Button>
            </Form>
          </Tab>
          <Tab value="pane-2" label="List Contacts">
            <ContactList entries={this.state.items}
                       delete={this.deleteItem}/>
          </Tab>
        </Tabs>
      </Panel>
    );
  }
}

export default Contacts

Contact List

class ContactList extends Component {
  constructor(props) {
    super(props);

    this.createContact = this.createContact.bind(this);
  }

  delete(key) {
    this.props.delete(key);
  }

  createContact(item) {
    return 
      <tr key={item.key}>
       <td onClick={() => this.delete(item.key)}>{item.firstname}</td>,
       <td>{item.lastname}</td>
       <td>{item.phone}</td>
      </tr>
  }

  render() {
    var contactEntries = this.props.entries;
    var listItems = contactEntries.map(this.createContact);

    return (
      <table className="mui-table">
        <thead>
          <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Phone</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            {listItems}
          </tr>
        </tbody>
      </table>
    );
  }
};

export default ContactList;

Upvotes: 0

Views: 1526

Answers (1)

MontyGoldy
MontyGoldy

Reputation: 749

Here is the answer. You are just hitting this._inputElement ref only saving its value whereas in your form you have two more inputs. My suggestion check latest updates react updates. They don't advice you to use "REF" at all.

addItem(e) {
    if (this._inputElement.value !== "") {
      var newItem = {
        firstname: this._inputElement.value,
        lastname: this._inputElement2.value,
        phonename: this._inputElement3.value,
        key: Date.now()
      };

      this.setState(prevState => {
        return {
          items: prevState.items.concat(newItem)
        };
      });

      this._inputElement.value = "";
      this._inputElement2.value = "";
      this._inputElement3.value = "";
    }

Upvotes: 0

Related Questions