12

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>
7
  • ReduxForm not supporting checkbox group. This might help you as workaround. Commented Mar 16, 2017 at 14:08
  • Hi @JyothiBabuAraja do you have an example how to implement it. I'm quite in new react :D. Thank you! Commented Mar 17, 2017 at 2:48
  • Check this and an example Commented Mar 17, 2017 at 4:29
  • Yeah, I did try that but there's an error it looks like it is using the v5.5.3 of redux-form and I'm using v6.5.0 Commented Mar 17, 2017 at 4:59
  • I am also on voyage of solving this problem :-), but yet to find a concrete answer. Commented Mar 17, 2017 at 21:32

3 Answers 3

20

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} /> 
Sign up to request clarification or add additional context in comments.

6 Comments

Many thanks @aftab-naveed ! What does the optionsList object look like ? I try to implement it without success.
let optionList = {id: 1, name: 'Optoin1', id: 2, name: 'Option 2'}
Good but, touch event doesn't work for me, it is always false. After the first form submit, touched event start firing. const {options, name, input, meta: { touched, error }} = this.props; const hasError = touched && error;
Works great; for some reason I had to make a change to newValue: const newValue = [...input.value] || []
do i need jQuery for this?
|
1

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

Comments

0

Adding some style to @Aftab Naveed I wrapped my checkboxes in a label with these classnames instead and they ended up real pretty and easier clicked:

<label key={option.name} className="form-check-label" style={ {"fontSize": "1.5em"} }>
          <input ... />
            <span>{option.name}</span>
          </label>

i did not use bootstrap, i think its a redux form thing with className "form-check-label"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.