Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple checkbox in redux-form

I would like to ask, here's the scenario. I have this multiple checkbox but my problem is whenever I tick one checkbox, all of the 4 checkboxes are selected. And also why is it the value of checkbox is just true or false.Here's my checkbox:

<div className="checkbox">
    <label><Field name="investor_stage" component="input" type="checkbox" value="Seed" /> Seed</label>
</div>
<div className="checkbox">
    <label><Field name="investor_stage" component="input" type="checkbox" value="Early Stages" /> Early Stages</label>
</div>
<div className="checkbox">
    <label><Field name="investor_stage" component="input" type="checkbox" value="Formative Stages" /> Formative Stages</label>
</div>
<div className="checkbox">
    <label><Field name="investor_stage" component="input" type="checkbox" value=" Later Stages" /> Later Stages</label>
</div>
like image 959
Sydney Loteria Avatar asked Mar 16 '17 13:03

Sydney Loteria


2 Answers

For people like me who are new to redux and react may find the original code mentioned here confusing. I modified and converted it to an ES6 class. I also Removed bootstrap, validation and made it easy to debug.

Here is the modified code

import React from 'react';

class CheckboxGroup extends React.Component {

    checkboxGroup() {
        let {label, required, options, input, meta} = this.props;

        return options.map((option, index) => {
            return (
            <div className="checkbox" key={index}>
                <label>
                    <input type="checkbox"
                           name={`${input.name}[${index}]`}
                           value={option.name}
                           checked={input.value.indexOf(option.name) !== -1}
                           onChange={(event) => {
                               const newValue = [...input.value];
                               if (event.target.checked) {
                                   newValue.push(option.name);
                               } else {
                                   newValue.splice(newValue.indexOf(option.name), 1);
                               }

                               return input.onChange(newValue);
                           }}/>
                    {option.name}
                </label>
            </div>)
        });
    }

    render() {
        return (
            <div>
                {this.checkboxGroup()}
            </div>
        )
    }
}


export default CheckboxGroup;

Usage:

let optionsList = [{id: 1, name: 'Optoin1'}, {id: 2, name: 'Option 2'}, {id: 3, name: 'Option 3'}]     
<Field name="roles" component={CheckboxGroup} options={optionsList} /> 
like image 110
Aftab Naveed Avatar answered Sep 24 '22 17:09

Aftab Naveed


Please give different field names for each checkbox. (name="investor_stage").The problem is all the field names are same.

like image 27
Silambarasan N Avatar answered Sep 24 '22 17:09

Silambarasan N