2016-11-07 9 views
0

私はここの指示に従って試してみました: http://react.tips/radio-buttons-in-reactjs/reactJSで再利用可能なラジオボタンを作成するにはどうすればよいですか?

をこのコードはほぼ動作しますが、私が間違っているかを把握するように見えることはできません。 2つのラジオ・オプションをクリックすると、選択されたラジオ・ボタンが選択を他のオプションに変更します。選択されていないラジオをクリックすると何も起こりません。私はたくさんのことを試しましたが、ラジオを期待通りに動かすことはできません。選択されていないオプションをクリックすると選択されます。ここで

は私のコードです:(*更新)

var RadioInput = React.createClass({ 
getInitialState: function() { 
    return {selectedOption: "0"}; 
}, 
handleOptionChange: function (changeEvent) { 
    this.setState({selectedOption: changeEvent.target.value}); 
    console.log(this.state.selectedOption, changeEvent.target, this.props.inputValue); 
},  
render: function() { 
    var isChecked = this.state.selectedOption === this.props.inputValue; 
    return (<p><label> 
      <input type="radio" name={this.props.inputName} value={this.props.inputValue} checked={isChecked} onChange={this.handleOptionChange}/>{this.props.labelText} 
    </label></p>); 
} 
}); 

私の希望は、この1つの汎用RadioInputで、私は、フォーム入力の束を一緒にコンパイルし、他のコンポーネント... I」と同様に、それを再利用することができるということです以下をしています....

var MediaInfo = React.createClass({ 
render: function() { 
    return (
     <div className="thumbnail"> 
      <div className="caption text-center"> 
       <h3>Media Contact Options</h3> 
       <form className="text-left"> 
        <p>Please indicate your contact preferences for media inquiries below:</p> 
        <div className="form-group"> 
         <div className="col-md-12"> 
          {this.props.fields.options.map (function (data, i) { 
           return (<RadioInput key={i} inputName={data.inputName} labelText={data.labelText} inputValue={data.inputValue} />); 
          })} 



         </div> 
        </div> 
        {this.props.fields.fields.map (function (data, i) { 
         return (<TextInput key={i} inputName={data.inputName} labelText={data.labelText} placeholderText={data.placeholderText} inputValue={data.inputValue} />); 
        })} 
       </form> 
      </div> 
     </div>    
    ) 
} 
}); 
var MemberInfo = React.createClass({ 
getInitialState: function() { 
    return { 
     contactInfo: [ {inputName:"fullName", labelText:"Display Name", placeholderText:"Enter Full Name", inputValue:"some name"}, 
         {inputName:"phoneNumber", labelText:"Phone Number", placeholderText:"Enter Phone Number", inputValue:"001-555-1234"}, 
         {inputName:"email", labelText:"Email", placeholderText:"Enter Email", inputValue:"[email protected]"}, 
         {inputName:"address", labelText:"Address", placeholderText:"Enter Address", inputValue:"123 Main St. Podunk, FL"} 
        ], 
     mediaContact: {fields: [ {inputName: "email", labelText: "Media Email", placeholderText:"Enter Medial Contact Email", inputValue:"[email protected]"}, 
         {inputName: "phone", labelText: "Media Phone", placeholderText:"Media Contact Phone Number", inputValue:"001-555-1234"}, 
         ], 
         options: [ 
          {inputName: "mediaConsent", labelText: " Yes, I would like to be contacted by the Media.", inputValue:"1"}, 
          {inputName: "mediaConsent", labelText: " No, I do not wish to be contacted by the Media.", inputValue:"0"} 
         ] 
         } 
    } 
}, 
render: function() { 
    return (
        <div className="panel panel-default"> 
         <div className="panel-heading"> 
          <h3 className="panel-title">Manage Your Contact Info</h3> </div> 
         <div className="panel-body"> 
          <div className="col-md-6"> 
           <div className="row"> 
            <div className="col-xs-12"> 
             <ContactInfo fields={this.state.contactInfo} /> 
            </div> 
           </div> 
          </div> 
          <div className="col-md-6"> 
           <div className="row"> 
            <div className="col-xs-12"> 
             <MediaInfo fields={this.state.mediaContact} /> 
            </div> 
           </div> 
          </div> 
         </div> 
        </div> 
    ); 

}, 

});

+0

ライブサンプルを表示できますか?どのようにレンダリングするかを表示しますか? – Li357

+0

コンポーネントが別のコンポーネントでどのように使用されているかを示すコードを更新しました。その全体のコンポーネントは次のようにレンダリングされます:ReactDOM.render(、document.querySelectorAll( '#member-info')[0]); –

答えて

0

私がうまく働くようで解決策を見つけたと私はもしそうなら、私を修正してください状態や小道具の正しい使用である(しかし、私はちょうどそれらのこつを得ていると考えています間違っている)。

私はそのフォーム全体の状態を維持して親コンポーネントに選択されたオプションの状態を更新するために選択したオプションの値と機能を追加しました:

var MemberInfo = React.createClass({ 
setSelectedOption: function (updatedValues) { 
    var newSelected = this.state.mediaContact; 
    newSelected.selectedOption = updatedValues; 
    this.setState({ mediaContact: newSelected }); 
}, 
getInitialState: function() { return {...  
mediaContact: {..., selectedOption: "0"}}}, 

を私はレンダリングを担当するコンポーネントにsetSelectedOptionを渡します支柱としてフォーム成分:

<MediaInfo fields={this.state.mediaContact} setSelectedOption={this.setSelectedOption} /> 

私RadioInputコンポーネントに選択された小道具を追加した状態のプロパティを使用して値を計算します。また、setSelectedOption関数を、レンダリングされた各RadioInputの小道具として、小道具として渡します。 これはおそらく理想的な実装ではありません。最も重要なこと

var RadioInput = React.createClass({ 
handleOptionChange: function (changeEvent) { 
    this.props.setSelectedOption(changeEvent.target.value); 
},  
render: function() { 
    return (<p><label> 
      <input type="radio" name={this.props.inputName} value={this.props.inputValue} checked={this.props.selected} onChange={this.handleOptionChange}/>{this.props.labelText} 
    </label></p>); 
} 
}); 

期待通りにレンダリングされたラジオ入力機能...デフォルトが選択されている:

{this.props.fields.options.map (function (data, i) { 
           return (<RadioInput key={i} 
              inputName={data.inputName} 
              labelText={data.labelText} 
              inputValue={data.inputValue} 
              setSelectedOption={this.props.setSelectedOption} 
              selected={this.props.fields.selectedOption === data.inputValue} 
              />); 
          }.bind(this))} 

は、今私のコンポーネントは、それがないIMHO、ただのonChangeハンドラがなければならない何の状態を、持っていませんユーザーが別のオプションをクリックすると選択が変わります。

以下は完全なコードです。私の目標は、reactJSを使ってブートストラップフォームを作成し、それにAPIからのデータを入力する方法を作成することでした。データを保存するコードやAPIから引き出すコードは書かれていません。私の計画は、いくつかのボタンを追加し、ボタンのためにデータを送信するonClickハンドラを使用することです。それ以外の場合は、componentDidMount関数を追加してAPIからデータを取り込む必要があります。これは誰かに役立つことを願っています。

var TextInput = React.createClass({ 
    handleChange: function (event) { 
     var updatedValues = { 
      inputName: this.props.inputName, 
      placeholderText: this.props.placeHolderText, 
      labelText:this.props.labelText, 
      inputValue:event.target.value, 
     }; 
     this.props.setContactInfo(updatedValues); 
    }, 
    render: function() { 
     return (
     <div className="form-group"> 
      <label htmlFor={this.props.inputName}>{this.props.labelText}</label> 
      <input onChange={this.handleChange} className="form-control" id={this.props.inputName} placeholder={this.props.placeholderText} value={this.props.inputValue} /> 
     </div>);  
    }, 
}); 

var RadioInput = React.createClass({ 
    handleOptionChange: function (changeEvent) { 
     this.props.setSelectedOption(changeEvent.target.value); 
    },  
    render: function() { 
     return (<p><label> 
       <input type="radio" name={this.props.inputName} value={this.props.inputValue} checked={this.props.selected} onChange={this.handleOptionChange}/>{this.props.labelText} 
     </label></p>); 
    } 
}); 

var ContactInfo = React.createClass({ 
    render: function() { 
     return (
     <div className="thumbnail"> 
      <div className="caption text-center"> 
       <h3>Member Contact Information</h3> 
       <form className="text-left" action="" method="post"> 
        <input type="hidden" name="member contact info" /> 
        {this.props.fields.map (function (data, i) { 
         return (<TextInput 
            key={i} 
            inputName={data.inputName} 
            labelText={data.labelText} 
            placeholderText={data.placeholderText} 
            inputValue={data.inputValue} 
            setContactInfo={this.props.setContactInfo} 
            />); 
        }.bind(this))} 
       </form> 
      </div> 
     </div>  
    ); 
    }, 
}); 

var MediaInfo = React.createClass({ 
    render: function() { 
     console.log(this.props.fields); 
     return (
      <div className="thumbnail"> 
       <div className="caption text-center"> 
        <h3>Media Contact Options</h3> 
        <form className="text-left"> 
         <p>Please indicate your contact preferences for media inquiries below:</p> 
         <div className="form-group"> 
          <div className="col-md-12"> 
           {this.props.fields.options.map (function (data, i) { 
            return (<RadioInput key={i} 
               inputName={data.inputName} 
               labelText={data.labelText} 
               inputValue={data.inputValue} 
               setSelectedOption={this.props.setSelectedOption} 
               selected={this.props.fields.selectedOption === data.inputValue} 
               />); 
           }.bind(this))} 
          </div> 
         </div> 
         {this.props.fields.fields.map (function (data, i) { 
          return (<TextInput 
             key={i} 
             inputName={data.inputName} 
             labelText={data.labelText} 
             placeholderText={data.placeholderText} 
             inputValue={data.inputValue} 
             setContactInfo={this.props.setContactInfo} />); 
         }.bind(this))} 
        </form> 
       </div> 
      </div>    
     ) 
    } 
}); 

var MemberInfo = React.createClass({ 
    setSelectedOption: function (updatedValues) { 
     var newSelected = this.state.mediaContact; 
     newSelected.selectedOption = updatedValues; 
     this.setState({ mediaContact: newSelected }); 
    }, 
    setContactInfo: function (updatedValues) { 
     var newContactInfo = this.state.contactInfo.map(function(data, i) {if(data.inputName == updatedValues.inputName) { return updatedValues;} return data }); 
     var newMediaInfo = this.state.mediaContact.fields.map(function(data, i) {if(data.inputName == updatedValues.inputName) { return updatedValues;} return data }); 
     var holdMediaContact = this.state.mediaContact; 
     holdMediaContact.fields = newMediaInfo; 
     this.setState({contactInfo: newContactInfo, mediaContact: holdMediaContact}); 
    }, 
    getInitialState: function() { 
     return { 
      contactInfo: [ {inputName:"fullName", labelText:"Display Name", placeholderText:"Enter Full Name", inputValue:""}, 
          {inputName:"phoneNumber", labelText:"Phone Number", placeholderText:"Enter Phone Number", inputValue:""}, 
          {inputName:"email", labelText:"Email", placeholderText:"Enter Email", inputValue:""}, 
          {inputName:"address", labelText:"Address", placeholderText:"Enter Address", inputValue:""} 
         ], 
      mediaContact: {fields: [ {inputName: "email", labelText: "Media Email", placeholderText:"Enter Medial Contact Email", inputValue:""}, 
            {inputName: "phone", labelText: "Media Phone", placeholderText:"Media Contact Phone Number", inputValue:""}, 
            ], 
          options: [ {inputName: "mediaConsent", labelText: " Yes, I would like to be contacted by the Media regarding my candidacy.", inputValue:"1"}, 
             {inputName: "mediaConsent", labelText: " No, I do not wish to be contacted by the Media.", inputValue:"0"} 
            ], 
          selectedOption: "0" 
          } 
     } 
    }, 
    render: function() { 
     return (
         <div className="panel panel-default"> 
          <div className="panel-heading"> 
           <h3 className="panel-title">Manage Your Contact Info</h3> </div> 
          <div className="panel-body"> 
           <div className="col-md-6"> 
            <div className="row"> 
             <div className="col-xs-12"> 
              <ContactInfo fields={this.state.contactInfo} setContactInfo={this.setContactInfo} /> 
             </div> 
            </div> 
           </div> 
           <div className="col-md-6"> 
            <div className="row"> 
             <div className="col-xs-12"> 
              <MediaInfo fields={this.state.mediaContact} setSelectedOption={this.setSelectedOption} setContactInfo={this.setContactInfo} /> 
             </div> 
            </div> 
           </div> 
          </div> 
         </div> 
     ); 

    }, 
}); 

ReactDOM.render(<MemberInfo />, document.querySelectorAll('#member-info')[0]); 
1

複数のラジオボタンを使用する場合は、name属性を設定する必要があります。また、ラジオボタンが1つしかない場合は、チェックボックスをオフにすることはできません(チェックボックスを使用)。

var RadioInput = React.createClass({ 
 
    getInitialState: function() { 
 
    return { selectedOption: false }; 
 
    }, 
 

 
    handleOptionChange: function(e) { 
 
    this.setState({ selectedOption: e.target.value }); 
 
    }, 
 

 
    render: function() { 
 
    var isChecked = this.state.selectedOption === this.props.inputValue; 
 
    return (
 
     <p><label> 
 
     <input 
 
      type="radio" 
 
      value={this.props.value} 
 
      name={this.props.name} 
 
      selected={isChecked} 
 
      onChange={this.handleOptionChange} /> 
 
      {this.props.value} 
 
     </label></p> 
 
    ); 
 
    } 
 
}); 
 

 
ReactDOM.render(<form> 
 
       <RadioInput name="group" value="one" /> 
 
       <RadioInput name="group" value="two" /> 
 
       </form>, document.getElementById('View'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<div id="View"></div>

+0

そのコンポーネントは、配列からデータをロードするように設計されています...更新されたコードを見てください。 –

関連する問題