使用简单的网络应用。用户单击编辑按钮,编辑区域将填充数据。我能够预填充输入文本,但我无法根据道具检查无线电输入。这是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')
);
答案 0 :(得分:2)
使用checked
代替defaultChecked
prop来获取复选框元素:
<input type="radio" checked={this.props.radioAttr.checked} ...
来自React docs:
defaultValue和defaultChecked道具仅在初始渲染期间使用。如果需要在后续渲染中更新值,则需要使用