2017-03-16 2 views
3

私は質問したいと思います。私はこの複数のチェックボックスを持っていますが、私の問題は1つのチェックボックスをチェックすると、4つのチェックボックスのすべてが選択されます。 Reduxのに慣れていないと、元のコードを見つけることが反応し、私のような人のために複数のチェックボックスがレピュックス形式

<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> 
+0

'ReduxForm'チェックボックスグループをサポートしていません。 [This](https://github.com/erikras/redux-form/issues/1037#issuecomment-243003954)が回避策として役立つかもしれません。 –

+0

こんにちは@JyothiBabuArajaあなたはそれを実装する方法の例がありますか?私は全く新しい反応をしている:D。ありがとうございました! –

+0

[こちら](https://github.com/erikras/redux-form/issues/635#issuecomment-239056316)と[例](https://github.com/erikras/redux-form/issues/)を確認してください。 635#issuecomment-270785911) –

答えて

6

が混乱hereを述べた:そしてまた、なぜそれが、チェックボックスの値がちょうど真であるかfalse.Hereの私のチェックボックスです。それを変更してES6クラスに変換しました。私はまた、ブートストラップ、検証を削除し、デバッグを容易にしました。ここ

修正されたコード

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; 

使用である:

let optionsList = [{id: 1, name: 'Optoin1'}, {id: 2, name: 'Option 2'}, {id: 3, name: 'Option 3'}]  
<Field name="roles" component={CheckboxGroup} options={optionsList} /> 
+0

多くのおかげで@ aftab-naveed! optionsListオブジェクトの外観はどうですか?私は成功することなくそれを実装しようとします。 – Gaius

+0

let optionList = {id:1、name: 'Optoin1'、id:2、name: 'Option 2'} –

+1

良いですが、タッチイベントは私にとってはうまくいかず、常にfalseです。最初のフォーム提出の後、タッチイベントが開始されます。 const {オプション、名前、入力、メタ:{タッチ、エラー}} = this.props; const hasError = touched && error; – TariqN

関連する問題