簡體   English   中英

reactjs根據道具選擇無線電輸入

[英]reactjs select radio input based on the props

在一個簡單的Web應用程序上工作。 用戶單擊“編輯”按鈕,編輯區域將填充數據。 我能夠預填輸入文本,但是我無法根據道具檢查無線電輸入。 這是jsfiddle的鏈接

https://jsfiddle.net/aas312/sqqwagco/

var data = [{value:15,locName:"A"},{value:30,locName:"B"}];
//Radiogroup component
var RadioElement = React.createClass({
    handleClick:function(){
        this.props.handleClick();
    },
    render:function(){
        return (
            <div>
                <input type="radio" name={this.props.group} value={this.props.radioAttr.value} 
                       defaultChecked={this.props.radioAttr.checked} 
                       onClick={this.handleClick}
                       />
                {this.props.radioAttr.label}
            </div>
        );    
    }
});

var RadioSet = React.createClass({    
    render: function () {       
        var radioElements = [];
        this.props.radios.forEach(function (r) {       
            radioElements.push(<RadioElement group={this.props.group} radioAttr={r}
                                             handleClick={this.props.handleClick}
                                             />);
        }.bind(this));        
        return (
            <div>{radioElements}</div>           
        );
    }
});
//

var LocRow = React.createClass({
    handleClick:function(){         
        this.props.handleEditClick(this.props.location);
  },
    render:function(){
    return (
        <tr>
        <td>{this.props.location.locName}</td>
        <td>{this.props.location.value}</td>
        <td><button onClick={this.handleClick}>Edit</button></td>
      </tr>
    )
  }
});
var LocTable = React.createClass({
    render:function(){
   var locRows = [];
   this.props.locs.forEach(function(loc){
      locRows.push(<LocRow location={loc} handleEditClick={this.props.handleEditClick}/>)
   }.bind(this));
    return (
        <div>
        <table>
            {locRows}
        </table>
      </div>
    );
  }
});
var EditName = React.createClass({
  handleChange:function() {
        var modLoc = this.props.loc;
    modLoc.locName = React.findDOMNode(this.refs.locRef).value;
    this.props.handleDataChange(modLoc);
  },
    render:function(){
    return (        
        <input type="text" value={this.props.loc.locName} ref="locRef" onChange={this.handleChange}/>
    );
  }
});

var LocValueEdit = React.createClass({
   handleRadioClick:function(){

   },
    render: function () {
        var locValueOptions = [
            {
                label: "15 M",
                value: 15,
                checked: false
            },
            {
                label: "30 M",
                value: 30,
                checked: false
            },
            {
                label: "60 M",
                value: 60,
                checked: false
            }
        ];

        locValueOptions.forEach(function (c) {
            if (c.value === this.props.locValue) {
                c.checked = true;
            }
        }.bind(this));
        return (

             <div>
                Delivery is disabled on app before regular hours closes.<br />
                <RadioSet group="locValue"
                    radios={locValueOptions}
                    handleClick={this.handleRadioClick}
                    />
            </div>
        );
    }
});

var EditLoc = React.createClass({
    render:function(){
    return (
        <div>
        <h3>Edit Data</h3>
        <EditName loc = {this.props.loc} handleDataChange={this.props.handleDataChange}/>
        <LocValueEdit locValue={this.props.loc.value}/>
        <button>Update</button>
      </div>
    );
  }
});

var App = React.createClass({
    getInitialState:function(){
    return {editingLoc:{locName:"",locValue:0}}
  },
    handleEditClick:function(loc){
    this.setState({editingLoc:loc});
  },
  handleDataChange:function(loc){
    alert(loc.locName);
  },
  render: function() {
    return (
                <div>
            <LocTable locs={this.props.data} handleEditClick={this.handleEditClick}/>
                    <EditLoc loc = {this.state.editingLoc}  handleDataChange={this.handleDataChange}/>
          </div>
    );
  }
});

ReactDOM.render(
  <App data={data} />,
  document.getElementById('container')
);

對復選框元素使用checked代替defaultChecked prop:

<input type="radio" checked={this.props.radioAttr.checked} ...

從React docs

defaultValue和defaultChecked道具僅在初始渲染期間使用。 如果您需要在后續渲染中更新值,則需要使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM