2017-04-14 211 views
0

如何顯示上點擊按鈕或單選按鈕

import React, { Component } from 'react'; 
 

 

 
class ImageStyle extends Component { 
 

 
    constructor(props) { 
 
     super(props); 
 

 
     this.state = { 
 
      title: '', 
 
      url: '', 
 
      summary: '' 
 
     }; 
 

 
     this.handleInputChange = this.handleInputChange.bind(this); 
 
    } 
 

 
    handleInputChange(event) { 
 

 
     this.setState({ 
 
      [event.target.name]: event.target.value 
 
     }); 
 

 
    } 
 

 
    render() { 
 
    
 
    return ( 
 
     <div> 
 
     <div> 
 
      <h1 className="row px-2">Image Style Notification</h1> 
 
      <hr /> 
 
      <div className="row px-1 py-2 animated fadeIn"> 
 
    
 
       <div className="px-1" style={{ width:50 + '%' }}><br /> 
 

 
        <div className="mb-1"> 
 
         <input type="text" 
 
         className="form-control" 
 
         placeholder="Title" 
 
         name="title" 
 
         value={this.state.title} 
 
         onChange={this.handleInputChange} 
 
         /> 
 
        </div> 
 

 
        <div className="mb-1"> 
 
         <textarea 
 
         className="form-control" 
 
         placeholder="Image URL" 
 
         name="url" 
 
         value={this.state.url} 
 
         onChange={this.handleInputChange} 
 
         /> 
 
        </div> 
 

 
        <div className="mb-1"> 
 
         <textarea 
 
         className="form-control" 
 
         placeholder="Summary" 
 
         name="summary" 
 
         value={this.state.summary} 
 
         onChange={this.handleInputChange} 
 
         /> 
 
        </div> 
 

 
        <br /> 
 

 
        <div className="row px-2" > 
 
         <div> 
 
         <button className="nav-link btn btn-block btn-info" onClick={this.handleClick} >Save</button> 
 
         </div> 
 
         <div className="px-1"> 
 
         <button className="nav-link btn btn-block btn-danger"> Cancel</button> 
 
         </div> 
 
        </div><br /> 
 

 
       </div> 
 
       <div><br /> 
 
        <div className="android"> 
 
         <table className="table table-clear android-width"> 
 
         <tbody> 
 
         <tr> 
 
          <td> 
 
          <img src={this.state.url} alt="Notification Image" className="img-width-android"/> 
 
          </td> 
 
          <td className="img-text-css"> 
 
          <h4><strong> 
 
           {this.state.title} 
 
          </strong></h4><br /><br /><br /><br /> 
 
          {this.state.summary}<br /> 
 
          </td> 
 
         </tr> 
 
         </tbody> 
 
        </table> 
 
        </div> 
 

 
        <div className="mobile"> 
 
         <table className="table table-clear img-css"> 
 
         <tbody> 
 
         <tr> 
 
          <td> 
 
          <img src={this.state.url} alt="Notification Image" className="img-width-css"/> 
 
          </td> 
 
          <td className="img-text-css"> 
 
          <h4><strong> 
 
           {this.state.title} 
 
          </strong></h4><br /><br /><br /><br /> 
 
          {this.state.summary}<br /> 
 
          </td> 
 
         </tr> 
 
         </tbody> 
 
        </table> 
 
        </div> 
 

 

 
       </div> 
 
      </div> 
 
    </div> 
 
    </div> 
 
    ) 
 
    
 
    } 
 
} 
 

 
export default ImageStyle;

我做了兩個組成部分顯示爲Android和iOS組件按我的CSS被添加爲安卓Android和移動的IOS 現在我想要的是添加一個按鈕或單選按鈕名爲IOS和Android 和單擊按鈕相關組件將僅顯示

回答

1

您可以爲兩個佈局創建單獨的組件,然後創建單選按鈕,整個值你將保持在狀態和德在有條件的情況下,您可以渲染正確的組件。在此行

checkRadio =(e) =>{ 
    if(e.target.checked) { 
    this.setState({layout: e.target.value}); 
    } 
} 

render() { 

     return ( 
      <div> 
      <div> 
       <h1 className="row px-2">Image Style Notification</h1> 
       <hr /> 
       <div className="row px-1 py-2 animated fadeIn"> 

        <div className="px-1" style={{ width:50 + '%' }}><br /> 

         <div className="mb-1"> 
          <input type="text" 
          className="form-control" 
          placeholder="Title" 
          name="title" 
          value={this.state.title} 
          onChange={this.handleInputChange} 
          /> 
         </div> 

         <div className="mb-1"> 
          <textarea 
          className="form-control" 
          placeholder="Image URL" 
          name="url" 
          value={this.state.url} 
          onChange={this.handleInputChange} 
          /> 
         </div> 

         <div className="mb-1"> 
          <textarea 
          className="form-control" 
          placeholder="Summary" 
          name="summary" 
          value={this.state.summary} 
          onChange={this.handleInputChange} 
          /> 
         </div> 

         <br /> 

         <div className="row px-2" > 
          <div> 
          <button className="nav-link btn btn-block btn-info" onClick={this.handleClick} >Save</button> 
          </div> 
          <div className="px-1"> 
          <button className="nav-link btn btn-block btn-danger"> Cancel</button> 
          </div> 
         </div><br /> 

        </div> 
        <div><br /> 
         <input type="radio" name="layout" value="mobile" onChange={this.checkRadio}/> 
         <input type="radio" name="layout" value="android" onChange={this.checkRadio}/> 
         {(this.state.layout === "mobile")? <Android url={this.state.url} summary={this.props.summary} title={this.state.title}/>: <Mobile url={this.state.url} summary={this.props.summary} title={this.state.title}/>} 





        </div> 
       </div> 
     </div> 
     </div> 
     ) 

     } 


    class Android extends React.Component { 
     constructor(props) { 
      super(props); 
     } 

     render() { 
      return (
       <div className="android"> 
          <table className="table table-clear android-width"> 
          <tbody> 
          <tr> 
           <td> 
           <img src={this.props.url} alt="Notification Image" className="img-width-android"/> 
           </td> 
           <td className="img-text-css"> 
           <h4><strong> 
            {this.props.title} 
           </strong></h4><br /><br /><br /><br /> 
           {this.props.summary}<br /> 
           </td> 
          </tr> 
          </tbody> 
         </table> 
         </div> 
      ) 
     } 
    } 


    class Mobile extends React.Component { 
     constructor(props) { 
      super(props); 
     } 

     render() { 
      return (
       <div className="mobile"> 
          <table className="table table-clear img-css"> 
          <tbody> 
          <tr> 
           <td> 
           <img src={this.props.url} alt="Notification Image" className="img-width-css"/> 
           </td> 
           <td className="img-text-css"> 
           <h4><strong> 
            {this.props.title} 
           </strong></h4><br /><br /><br /><br /> 
           {this.props.summary}<br /> 
           </td> 
          </tr> 
          </tbody> 
         </table> 
         </div> 
      ) 
     } 
    } 
+0

語法錯誤,也有這 類沒有單選按鈕的Android擴展組件{ – Piyush

+0

的Piyush,我給您解決問題的方法。這不是一個確切的解決方案。另外我認爲語法錯誤是一些可以由你糾正的問題。由於我沒有模擬代碼這樣的錯誤來 –

+0

我刪除了錯誤 它只顯示ios部分根據您提供的片段 – Piyush