Advertisement
shopnilSS

Form Handling in Uncontrolled way

Jun 2nd, 2021 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2.  
  3.  
  4. class Uncontrolled extends Component {
  5.     constructor(){
  6.         super()
  7.         //store all ref from input field
  8.         this.name = React.createRef() // --------- (2)
  9.         this.sex = React.createRef()
  10.         this.country = React.createRef()
  11.         this.hobby = []
  12.         this.playing = React.createRef()
  13.         this.programming = React.createRef()
  14.     } //------------ (3)
  15.  
  16.    
  17.     //submit handler
  18.     submitHandler = (e) => {
  19.         e.preventDefault() //ignore auto page load during submission ------------(6)
  20.         let {name, sex, country, hobby, programming, playing} = this //get the ref from input  --------(7)
  21.         let nameInput = name.current.value , sexInput = sex.current.value, countryInput = country.current.value, hobbyInput = hobby //store the value what we got from ref -------- (8)
  22.        
  23.         //hobby select part
  24.         if(playing.current.checked){ //playing part
  25.              let isAvailble = hobby.find(data => data == playing.current.value)
  26.              if(!isAvailble){
  27.                 hobby.push(playing.current.value)
  28.              }
  29.         }else if(playing.current.checked == false){
  30.             let index = hobby.findIndex(ele => ele == playing.current.value)
  31.             hobby.splice(index, 1)
  32.         } //------------- (9)
  33.         if(programming.current.checked){ //programming part
  34.              let isAvailble = hobby.find(data => data == programming.current.value)
  35.              if(!isAvailble){
  36.                 hobby.push(programming.current.value)
  37.              }
  38.         }else if(programming.current.checked == false){
  39.             let index = hobby.findIndex(ele => ele == programming.current.value)
  40.             hobby.splice(index, 1)
  41.         } // ------------- (10)
  42.  
  43.         console.log(`name = ${nameInput} sex = ${sexInput} country = ${countryInput} hobby = ${hobbyInput}`);
  44.        
  45.     } //----------------- (5)
  46.     render() {
  47.         return (
  48.             <div>
  49.                 <form>
  50.                     {/* name  */}
  51.                     <div className="form-group">
  52.                         <label htmlFor="exampleInputEmail1">Email address</label>
  53.                         <input type="text" className="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" ref = {this.name}></input> {/*---------- (1) */}
  54.                     </div>
  55.  
  56.                     {/* sex  */}
  57.                     <label htmlFor="sex">Sex</label>
  58.                     <div className="form-check">
  59.                         <input className="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="male" ref = {this.sex}></input>
  60.                         <label className="form-check-label" htmlFor="exampleRadios1">
  61.                             Male
  62.                         </label>
  63.                         </div>
  64.                         <div className="form-check">
  65.                         <input className="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="female" ref = {this.sex}></input>
  66.                         <label className="form-check-label" htmlFor="exampleRadios2">
  67.                             Female
  68.                         </label>
  69.                     </div>
  70.  
  71.                     {/* country  */}
  72.                      <div className="form-group">
  73.                         <label htmlFor="exampleFormControlSelect1">Example select</label>
  74.                         <select className="form-control" id="exampleFormControlSelect1" ref = {this.country}>
  75.                             <option value = "bangladesh">Bangladesh</option>
  76.                             <option value = 'india'>India</option>
  77.                         </select>
  78.                     </div>
  79.  
  80.                     {/* hobby */}
  81.                     <label htmlFor="hobby">Hobby</label>
  82.                     <div className="form-check">
  83.                         <input type="checkbox" className="form-check-input" id="exampleCheck1" ref = {this.programming} value = "programming"></input>
  84.                         <label className="form-check-label" htmlFor="exampleCheck1">Programming</label>
  85.                     </div>
  86.                     <div className="form-check">
  87.                         <input type="checkbox" className="form-check-input" id="exampleCheck1" ref = {this.playing} value = "playing"></input>
  88.                         <label className="form-check-label" htmlFor="exampleCheck1">Playing</label>
  89.                     </div>
  90.  
  91.                     {/* submit button  */}
  92.                     <button type="submit" className="btn btn-primary" onClick = {this.submitHandler}>Submit</button> {/* --------------- (4) */}
  93.                     </form>
  94.             </div>
  95.         )
  96.     }
  97. }
  98.  
  99. export default Uncontrolled
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement