I am learning react and am stuck on an issue. I have 3 components, TransactionsDetails, TransactionForm and TransactionsTable.
TransactionsDetails TransactionForm TransactionsTable The records are saved in TransactionsDetails's state.records and are displayed in TransactionsTable. I am trying to add, edit, update, delete records. I have successfully added the add and delete functionality but am stuck on the edit and update. So, in the table rows, i have edit and delete button for each row. When clicked on edit, it should load the record data in the form and when clicked in save/submit, it should update the record.
var TransactionForm = React.createClass({ getInitialState: function(){ return { date: '', amount: '' }; }, handleChange: function(e){ switch(e.target.name){ case 'date': this.setState({date: e.target.value}); break; case 'amount': this.setState({amount: e.target.value}); break; } }, handleSubmit: function(e){ e.preventDefault(); this.props.handleNewRecord(this.state); this.setState(this.getInitialState()); }, render: function(){ return ( <form onSubmit={this.handleSubmit} className="form-horizontal"> <div className="form-group"> <label for="date" className="col-sm-2 control-label">Date: </label> <div className="col-sm-10"> <input type='text' name='date' className="form-control" value={this.state.date} onChange={this.handleChange}/> </div> </div> <div className="form-group"> <label for="amount" className="col-sm-2 control-label">Amount: </label> <div className="col-sm-10"> <input type='integer' name='amount' className="form-control" value={this.state.amount} onChange={this.handleChange}/> </div> </div> <div className="form-group"> <div className="col-sm-offset-2 col-sm-10"> <button className="btn btn-primary" type='submit'>Submit</button> </div> </div> </form> ); } }); var TransactionRow = React.createClass({ handleDelete: function(e){ e.preventDefault(); this.props.handleDeleteRecord(this.props.record); }, handleEdit: function(e){ e.preventDefault(); this.props.handleEditRecord(this.props.record); }, render: function(){ return ( <tr> <td>{this.props.record.date}</td> <td>{this.props.record.amount}</td> <td> <button className="btn btn-primary" onClick={this.handleEdit}>Edit</button> <button className="btn btn-danger" onClick={this.handleDelete}>Delete</button> </td> </tr> ); } }); var TransactionsTable = React.createClass({ render: function(){ var rows = []; this.props.records.map(function(record, index){ rows.push(<TransactionRow key={index} record={record} handleDeleteRecord={this.props.handleDeleteRecord} handleEditRecord={this.props.handleEditRecord}/>); }.bind(this)); return ( <table className="table table-striped table-hover table-bordered"> <thead> <tr> <th> Date </th> <th> Amount </th> <th> Actions </th> </tr> </thead> <tbody> { rows } </tbody> </table> ); } }); var TransactionsDetails = React.createClass({ getInitialState: function(){ return { records: [ { date: '1-6-2016', amount: 1000}, { date: '2-6-2015', amount: -400} ] }; }, addRecord: function(record){ var records = this.state.records; records.push(record); this.setState({ records: records }); }, deleteRecord: function(record){ var records = this.state.records; var index = records.indexOf(record); records.splice(index, 1); this.setState({ records: records }); }, editRecord: function(record){ if(record){ return record; } else { return {}; } }, render: function(){ return ( <div className="container"> <div className='row'> <div className="col-md-4"> <div className="well"> <TransactionForm handleNewRecord={this.addRecord} record={this.editRecord()} /> </div> </div> </div> <div className='row'> <div className="col-md-offset-2 col-md-8"> <div className="well"> <TransactionsTable records={this.state.records} handleDeleteRecord={this.deleteRecord} handleEditRecord={this.editRecord}/> </div> </div> </div> </div> ); } });