Skip to main content

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.

Also Read :  Sort associative array by specific key in PHP

        <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.

Also Read :  Get Drop Down List of TimeZones using PHP

    <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 :

Also Read :  Laravel 5.6 Server Side Validation Example

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.

Web Developer

Hi, I’m Web developer, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]