Home > Net >  React - multi step form with checkbox in child step does not set to true
React - multi step form with checkbox in child step does not set to true

Time:07-01

I am creating a multistep form in react. The Parent component has 3 child components which are the steps of the form. In the first step I have 4 text inputs and need a checkbox to allow wpp communication, the problem is that i can't set the state of the checkbox to true when is clicked. The parent component is something like this:

import React from 'react';
import FormStep0 from './formStepZero';
import FormStep1 from './formStepOne';



class VendeForm extends React.Component {
    constructor(props) {
      super(props)
      this.state = {
        currentStep: 0,
        nombre: '',
        apellido: '',
        celular: '',
        email:  '',
        username: '',
        password: '',
        checkbox: false, 
      }
    }
  
    handleChange = event => {
      const {name, value} = event.target
      this.setState({
        [name]: value
      })    
    }
     
    handleSubmit = event => {
      event.preventDefault()
      const { email, username, password, nombre, apellido, celular,checkbox } = this.state
      alert(`Your registration detail: \n 
             Email: ${email} \n 
             Username: ${username} \n
             Password: ${password} \n
             Nombre: ${nombre} \n 
             Apellido: ${apellido} \n
             Celular: ${celular} \n
             Checkbox: ${checkbox} \n`)
    }
    
    _next = () => {
      let currentStep = this.state.currentStep
      currentStep = currentStep >= 2? 3: currentStep   1
      this.setState({
        currentStep: currentStep
      })
    }
      
    _prev = () => {
      let currentStep = this.state.currentStep
      currentStep = currentStep <= 0? 0: currentStep - 1
      this.setState({
        currentStep: currentStep
      })
    }
  
  /*
  * the functions for our button
  */
  previousButton() {
    let currentStep = this.state.currentStep;
    if(currentStep > 1){
      return (
        <div className='vertical-center'>
        <button 
          className="btn btn-secondary float-right mx-2 my-2" 
          type="button" onClick={this._prev}>
        Previous
        </button>
        </div>
      )
    }
    return null;
  }
  
  nextButton(){
    let currentStep = this.state.currentStep;
    if(currentStep <3 && currentStep >=1){
      return (
        <div className='vertical-center'>
        <button 
          className="btn primary-bg-style float-left mx-2 my-2" 
          type="button" onClick={this._next}>
        Siguiente 
        </button>        
        </div> 
      )
    }
    return null;
  }
  empecemosButton(){
    let currentStep = this.state.currentStep;
    if(currentStep === 0){
      return (
        <div className='vertical-center'>
        <button 
          className="btn primary-bg-style" 
          type="button" onClick={this._next}>
        Vamos! 
        </button>
        </div>        
      )
    }
    return null;
  }
    render() {    
      return (
        <React.Fragment>     
        {/*render the form steps and pass required props in*/}
          <Step0
            currentStep={this.state.currentStep}
            handleChange={this.handleChange}
          />

        <div className="container d-flex flex-column py-2">
          
          <form onSubmit={this.handleSubmit}>

            <Step1 
              currentStep={this.state.currentStep} 
              handleChange={this.handleChange}

              nombre={this.state.nombre}
              apellido={this.state.apellido}
              celular={this.state.celular}
              email={this.state.email}
              checkbox={this.state.checkbox}
            />
            <Step2 
              currentStep={this.state.currentStep} 
              handleChange={this.handleChange}
              username={this.state.username}
            />
            <Step3 
              currentStep={this.state.currentStep} 
              handleChange={this.handleChange}
              password={this.state.password}
            />
          </form>
          <div className='prev-next-btn'>
              
            {this.previousButton()}
            
            
            {this.nextButton()}
            
          </div>
          {this.empecemosButton()}
        </div>
        </React.Fragment>
      );
    }
  }

  function Step0(props){
    if (props.currentStep !== 0) {
      return null
    }
    return(

<div className="vertical-center"> {/*<!--^--- Added class  --> */}
        ...
</div>


    )
  }
  
  function Step1(props) {
    if (props.currentStep !== 1) {
      return null
    } 
    return(
      <div className="form-group">

        <div className="vertical-center">
          <div className="container">
            <h1 className='pb-4 px-2'>Datos de Contacto</h1>
            <CircleSteps currentStep={props.currentStep} />
            <FormStep1  nombre={props.nombre}
                        apellido={props.apellido}  
                        celular={props.celular}
                        email={props.email}

                        checkbox={props.checkbox}

                        handleChange = {props.handleChange}
            />

          </div>
        </div>

      </div>
    );
  }
  
export default VendeForm;

and the child component where is the checkbox is:

import React from 'react';
import {Col, Row,Container} from 'reactstrap'


function FormStep1(props) {

  return(
        <div>

            <Container className="vende-form-container pt-3">
                <Row className='align-items-center justify-content-center'>
                    <Col md={4} sm={12}>
                        <div className="form-group">
                            <label htmlFor="nombre">Nombre</label>
                            <input
                            className="form-control"
                            id="nombre"
                            name="nombre"
                            type="text"
                            placeholder="Escribe tu nombre"
                            value={props.nombre}
                            onChange={props.handleChange}
                            />
                        </div>
                    </Col>
                    <Col md={4} sm={12} >
                        <div className="form-group">
                            <label htmlFor="apellido">Apellido</label>
                            <input
                            className="form-control"
                            id="apellido"
                            name="apellido"
                            type="text"
                            placeholder="Escribe tu apellido"
                            value={props.apellido}
                            onChange={props.handleChange}
                            />
                        </div>
                    </Col>
                </Row>
                <Row className='align-items-center justify-content-center'>
                    <Col md={4} sm={12}>
                        <div className="form-group">
                            <label htmlFor="celular">Celular</label>
                            <input
                            className="form-control"
                            id="celular"
                            name="celular"
                            type="text"
                            placeholder="Escribe tu celular"
                            value={props.celular}
                            onChange={props.handleChange}
                            />
                        </div>
                    </Col>
                    <Col md={4} sm={12}>
                        <div className="form-group">
                            <label htmlFor="email">Email</label>
                            <input
                            className="form-control"
                            id="email"
                            name="email"
                            type="text"
                            placeholder="Escribe tu email"
                            value={props.email}
                            onChange={props.handleChange}
                            />
                        </div>
                    </Col>


                </Row>

            
                <Row className='align-items-center justify-content-start'>
                    
                    {/* checkbox allowing whatsapp communication */}
                    <Col sm={{size:6, offset:2}} md={{size:3, offset:2}}>
                    <div className="form-check">
                        <input
                            className="form-check-input" 
                            id="wppCheckbox"
                            name="controlled" 
                            type="checkbox" 
                            checked={props.checkbox} 
                            onChange={props.handleChange} 
                            />
                        <label className="form-check-label" htmlFor="wppCheckbox">
                            Aceptas comunicacion via Whatsapp
                        </label>
                    </div>
                    </Col>
                </Row>   
            </Container>

        </div>
        );        

}

export default FormStep1;

The behavior that currently I have is that i click the checkbox but remains in blank, also the others fields of the form are working well and are passed to state properly

CodePudding user response:

If I understand correctly, your checkbox is handled by generic handleChange() function:

    handleChange = event => {
      const {name, value} = event.target
      this.setState({
        [name]: value
      })    
    }

Simplest thing you could try for debugging is just logging what you get in that function when you click the checkbox. Most probably in this case you are always getting the same value ("on") hence nothing changes in the state. I would suggest either use a dedicated handler, or modify this one to be sth like

    handleChange = event => {
      const {name, type, value} = event.target
      if (type === 'checkbox') {
        return this.setState({
          [name]: event.target.checked
        }) 
      }
      this.setState({
        [name]: value
      })    
    }
  • Related