Advertisement
Qpel

G5

Nov 14th, 2019
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React,{Component} from 'react';
  2. import ReactDOM from 'react-dom';
  3. import ListItem from './ListItem';
  4. class Reservation extends React.Component {
  5.   constructor(props) {
  6.     super(props);
  7.     this.state = {
  8.       isGoing: true,
  9.       numberOfGuests: 2,
  10.       fields:{isGoing:true, numberOfGuests: 2}
  11.     };
  12.     //fields={isGoing:true};
  13.  
  14.     this.handleInputChange = this.handleInputChange.bind(this);
  15.   }
  16.  
  17.   handleInputChange(event) {
  18.     const target = event.target;
  19.     const value = target.type === 'checkbox' ? target.checked.toString() : target.value;
  20.     const name = target.name;
  21.     const fields = this.state.fields;
  22.  
  23.     fields[event.target.name] = value;
  24.     console.log(fields);
  25.     this.setState({
  26.       [name]: value
  27.     });
  28.   }
  29.  
  30.   render() {
  31.     const fields = this.state.fields;
  32.     return (
  33.       <form>
  34.         <label>
  35.           Is going:
  36.           <input
  37.             name="isGoing"
  38.             type="checkbox"
  39.            
  40.             onChange={this.handleInputChange} />
  41.         </label>
  42.         <br />
  43.         <label>
  44.           Number of guests:
  45.           <input
  46.             name="numberOfGuests"
  47.             type="number"
  48.             value={this.state.numberOfGuests}
  49.             onChange={this.handleInputChange} />
  50.         </label>
  51.         {Object.keys(fields).map((item) =>
  52.         <li>{item}: {fields[item]}</li>
  53.         )
  54.         }
  55.  
  56.       </form>
  57.  
  58.      
  59.     );
  60.   }
  61. }
  62.  
  63. ReactDOM.render(
  64.   <Reservation />,
  65.   document.getElementById('app')
  66. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement