How to Select React Js Check All Uncheck All Checkboxes

Today, We want to share with you How to Select React Js Check All Uncheck All Checkboxes.In this post we will show you React input checkbox select all component, hear for Multiple checkbox handling by React JS we will give you demo and example for implement.In this post, we will learn about Reactjs Check All Uncheck All checkboxes with an example.

How to Select React Js Check All Uncheck All Checkboxes

There are the Following The simple About How to Select React Js Check All Uncheck All Checkboxes Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to get checked and unchecked checkbox value in React Js, so uncheck select all checkbox if one checkbox is unchecked for this example is following below.

Step 1: Include Reactjs library

At first we need to include Reactjs Latest library file.

        <title>React js: Select All or DeSelect All</title>
        <script crossorigin src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
        <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
        <script src="https://unpkg.com/[email protected]/babel.min.js"></script>

Step 2: JavaScript/Reactjs Source Code:

This file All the Data contains the following JavaScript/Reactjs Source codes.

Read Also:  SQL SELECT DISTINCT Statement

        <script type="text/babel">
            function CheckBox({name, value, tick, onCheck}) {
                return (
                    <label>
                        <input
                            name={name}
                            type="checkbox"
                            value={value}
                            checked={tick || false}
                            onChange={onCheck}
                        />
                        {value}
                    </label>
                );
            }

            function CheckBoxList ({options, isSelectedAll, onCheck}) {
                const checkBoxOptions = (
                    <div className="checkbox-list">
                        {options.map((option, index) => {
                            return (
                                <CheckBox key={index} name={option.name} value={option.value} tick={option.checked} onCheck={(e) => onCheck(option.value, e.target.checked)} />
                            );
                        })}
                    </div>
                );

                return (
                    <div className="checkbox-list">
                        <CheckBox name="select-all" value="ALL" tick={isSelectedAll} onCheck={(e) => onCheck('all', e.target.checked)} />
                        {checkBoxOptions}
                    </div>
                );
            }

            class CityList extends React.Component {
                constructor(props) {
                    super(props);

                    this.state = {
                        isSelectedAll: false,
                        checkList: [
                            {
                                name: "color",
                                value: "red",
                                checked: false,
                            },
                            {
                                name: "color",
                                value: "green",
                                checked: false,
                            },
                            {
                                name: "color",
                                value: "blue",
                                checked: false,
                            }
                        ]
                    };
                }

                onCheckBoxChange(checkName, isSelected) {
                    let isAllChecked = (checkName === 'all' && isSelected);
                    let isAllUnChecked = (checkName === 'all' && !isSelected);
                    const checked = isSelected;

                    const checkList = this.state.checkList.map((color, index) => {
                        if(isAllChecked || color.value === checkName) {
                            return Object.assign({}, color, {
                                checked,
                            });
                        } else if (isAllUnChecked) {
                            return Object.assign({}, color, {
                                checked: false,
                            });
                        }

                        return color;
                    });

                    let isSelectedAll = (checkList.findIndex((item) => item.checked === false) === -1) || isAllChecked;

                    this.setState({
                        checkList,
                        isSelectedAll,
                    });

                }

                render() {
                    return (<CheckBoxList options={this.state.checkList} isSelectedAll={this.state.isSelectedAll} onCheck={this.onCheckBoxChange.bind(this)} />);
                }
            }

            ReactDOM.render(
                <CityList />,
                document.getElementById('root')
            );
        </script>

Step 3: HTML Source Code:

This file contains the following HTML Markup Source codes.

Read Also:  PHP Laravel Interview Questions and Answers

    <body>
        <div id="root"></div>
    </body>

How to Select React Js Check All Uncheck All Checkboxes
How to Select React Js Check All Uncheck All Checkboxes

Full Source Code

Following CSS Source codes are used for How to implement select/deselect all checkbox using React?.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>React: Select All or DeSelect All</title>
        <script crossorigin src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
        <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
        <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
        <script type="text/babel">
            function CheckBox({name, value, tick, onCheck}) {
                return (
                    <label>
                        <input
                            name={name}
                            type="checkbox"
                            value={value}
                            checked={tick || false}
                            onChange={onCheck}
                        />
                        {value}
                    </label>
                );
            }

            function CheckBoxList ({options, isSelectedAll, onCheck}) {
                const checkBoxOptions = (
                    <div className="checkbox-list">
                        {options.map((option, index) => {
                            return (
                                <CheckBox key={index} name={option.name} value={option.value} tick={option.checked} onCheck={(e) => onCheck(option.value, e.target.checked)} />
                            );
                        })}
                    </div>
                );

                return (
                    <div className="checkbox-list">
                        <CheckBox name="select-all" value="ALL" tick={isSelectedAll} onCheck={(e) => onCheck('all', e.target.checked)} />
                        {checkBoxOptions}
                    </div>
                );
            }

            class CityList extends React.Component {
                constructor(props) {
                    super(props);

                    this.state = {
                        isSelectedAll: false,
                        checkList: [
                            {
                                name: "color",
                                value: "red",
                                checked: false,
                            },
                            {
                                name: "color",
                                value: "green",
                                checked: false,
                            },
                            {
                                name: "color",
                                value: "blue",
                                checked: false,
                            }
                        ]
                    };
                }

                onCheckBoxChange(checkName, isSelected) {
                    let isAllChecked = (checkName === 'all' && isSelected);
                    let isAllUnChecked = (checkName === 'all' && !isSelected);
                    const checked = isSelected;

                    const checkList = this.state.checkList.map((color, index) => {
                        if(isAllChecked || color.value === checkName) {
                            return Object.assign({}, color, {
                                checked,
                            });
                        } else if (isAllUnChecked) {
                            return Object.assign({}, color, {
                                checked: false,
                            });
                        }

                        return color;
                    });

                    let isCheckedAll = (checkList.findIndex((item) => item.checked === false) === -1) || isAllChecked;

                    this.setState({
                        checkList,
                        isCheckedAll,
                    });

                }

                render() {
                    return (<CheckBoxList options={this.state.checkList} isCheckedAll={this.state.isCheckedAll} onCheck={this.onCheckBoxChange.bind(this)} />);
                }
            }

            ReactDOM.render(
                <CityList />,
                document.getElementById('root')
            );
        </script>
    </head>
    <body>
        <div id="root"></div>
    </body>
</html>

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  PHP Dynamically generate QR code Library

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to Select React Js Check All Uncheck All Checkboxes.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP MySQLi Database Connection
  2. Read Also:  Base64 Encode Decode string using Javascript
  3. Read Also:  Laravel Framework version Types
  4. Read Also:  Laravel 6.2 jQuery AJAX CRUD Tutorial
  5. Read Also:  PHP MySQLi Insert Update Delete Query Builder
  6. Read Also:  Angular js Introduction to Looping Statements
  7. Read Also:  How To Change Bootstrap Glyphicon Color?
  8. Read Also:  SQL SELECT DISTINCT Statement
  9. Read Also:  Laravel Framework version Types
  10. Read Also:  How To Change Bootstrap Glyphicon Color?
CLOSEX