mohsen zarei
mohsen zarei

Reputation: 15

how to change this state with radio buttons

I have a few selectable buttons that I want to change by selecting any of these state radio buttons I want to use as a parameter to send information to the api

import React ,{Component} from 'react';
import axios from "axios/index";

export default class Test extends Component {

    constructor(props) {
        super(props);
        this.state = {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    }

this is my axios codes

componentDidMount(){
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

and this is my buttons

render() {
    return (
        <div className="container-fluid">
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Justice" />
                        options1-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Credit" />
                        options1-2
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="New" />
                        options2-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="LowPerformance" />
                        options2-2
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="Old" />
                        options2-3
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Unregistered" autoComplete="off" />
                        options3-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Registered" />
                        options3-2
                    </label>
                </div>
            </div>
        </div>
    );
}

Please help. I would like to click on any of these buttons to add a class to its lable and each of these groups is just an option.

Upvotes: 0

Views: 122

Answers (2)

ravibagul91
ravibagul91

Reputation: 20765

You should provide the value and onChange props to your radio input.

<div className="row RegisterOptions">
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Justice" value="Justice" onChange={this.changeHandle}/>
            options1-1
        </label>
    </div>
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Credit" value="Credit" onChange={this.changeHandle}/>
            options1-2
        </label>
    </div>
</div>

Here your value will change as per radio input, but changeHandle will remain same for all the radio input's.

Your changeHandle should be this,

changeHandle = (e) => {
   this.setState({
     [e.target.name]: e.target.value
   })
}

Demo


You can then use values from state for your axios call. You should make a separate function for your API call

callAPI = () => {
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: this.state.TypeStatus,
            OperatingStatus: this.state.OperatingStatus,
            RegistrationStatus: this.state.RegistrationStatus,
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

Now this function can be called from anywhere like on the click of submit button,

<button onClick={this.callAPI}>Submit</button>

or from componentDidMount,

componentDidMount(){
   this.callAPI();
}

Upvotes: 1

Ashif Zafar
Ashif Zafar

Reputation: 637

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      TypeStatus: '',
      OperatingStatus: '',
      RegistrationStatus: '',
    };
  }

  // handleStateClick = () =>

  render() {
    return (
      <div>
        <button onClick={() => this.setState({ TypeStatus: " TypeStatus clicked" }, console.log(this.state.TypeStatus))}>TypeStatus</button>
        <button onClick={() => this.setState({ OperatingStatus: " OperatingStatus clicked" }, console.log(this.state.OperatingStatus))}>OperatingStatus</button>
        <button onClick={() => this.setState({ RegistrationStatus: " RegistrationStatus clicked" }, console.log(this.state.RegistrationStatus))}>RegistrationStatus</button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

https://stackblitz.com/edit/react-ahytle?file=index.js

Upvotes: 0

Related Questions