Forms 反应JS无线电输入状态
使用React管理无线电状态和复选框的正确方法是什么 在某些情况下,表单会呈现为部分完成,因此在第一次加载时会预先选择一些单选框和复选框 我有下面的代码片段,无法使其按预期工作Forms 反应JS无线电输入状态,forms,reactjs,radio-button,react-jsx,Forms,Reactjs,Radio Button,React Jsx,使用React管理无线电状态和复选框的正确方法是什么 在某些情况下,表单会呈现为部分完成,因此在第一次加载时会预先选择一些单选框和复选框 我有下面的代码片段,无法使其按预期工作 var formData = { "id": 13951, "webform_id": 1070, "page": 0, "type": "radios", "name": "What industry are you in?", "tooltip": "", "weight": 0,
var formData = {
"id": 13951,
"webform_id": 1070,
"page": 0,
"type": "radios",
"name": "What industry are you in?",
"tooltip": "",
"weight": 0,
"is_required": 1,
"default_value": "",
"validation": "",
"allow_other_option": 0,
"other_option_text": "",
"mapped_question_id": "a295189e-d8b4-11e6-b2c5-022a69d30eef",
"created_at": "2017-04-07 18:40:39",
"updated_at": "2017-04-07 18:40:39",
"option_conditional_from": null,
"default_value_querystring_key": "",
"deleted_at": null,
"is_auto_save": 0,
"is_component_number_hidden": 0,
"is_component_inline": 0,
"enable_confirm_validation": 0,
"confirm_validation_text": null,
"additional_options": "",
"url_mapping": "",
"webformcomponentoptions": [
{
"id": 13888,
"webform_component_id": 13951,
"key": "Hospitality",
"value": "Hospitality",
"created_at": "2017-04-07 18:40:39",
"updated_at": "2017-04-07 18:40:39",
"group": "",
"selected" : false
},
{
"id": 13889,
"webform_component_id": 13951,
"key": "Retail",
"value": "Retail",
"created_at": "2017-04-07 18:40:39",
"updated_at": "2017-04-07 18:40:39",
"group": "",
"selected" : false
},
{
"id": 13890,
"webform_component_id": 13951,
"key": "Other",
"value": "Other",
"created_at": "2017-04-07 18:40:39",
"updated_at": "2017-04-07 18:40:39",
"group": "",
"selected" : false
}
]
}
class WebformApp extends React.Component {
render() {
return (
<form>
<label>{this.props.webform.name}</label>
<div className="group-wrapper">
<Radio radio={this.props.webform.webformcomponentoptions} />
</div>
</form>
)
}
}
class Radio extends React.Component {
render() {
var options = [];
this.props.radio.forEach(function(radio, i) {
options.push(<Option option={radio} key={radio.id} index={i} />);
})
return (
<div>{options}</div>
)
}
}
class Option extends React.Component {
constructor(props) {
super(props);
this.handleOptionChange = this.handleOptionChange.bind(this);
this.state = {selectedIndex: null};
}
handleOptionChange(e) {
this.setState({selectedIndex: this.props.index}, function() {
});
}
render() {
const selectedIndex = this.state.selectedIndex;
return (
<div>
<input type="radio"
value={this.props.option.value}
name={this.props.option.webform_component_id}
id={this.props.option.id}
checked={selectedIndex === this.props.index}
onChange={this.handleOptionChange} />
<label htmlFor={this.props.option.id}>{this.props.option.key}</label>
</div>
)
}
}
ReactDOM.render(
<WebformApp webform={formData} />,
document.getElementById('app')
);
最重要的是让无线电组件处理状态,并跟踪所选选项 此外,我将通过使用map而不是forEach来简化,并为返回。为了简单起见,使用选项值来跟踪选定的状态而不是索引,并模仿React的select组件,允许使用默认值属性,而不是设置每个选项的选定属性(您似乎没有使用) 最后,为了秩序起见,将Radio:s Radio道具重命名为IMO更正确的选项。因此,请注意,我还没有测试过:
class WebformApp extends React.Component {
render() {
return (
<form>
<label>{this.props.webform.name}</label>
<div className="group-wrapper">
<Radio options={this.props.webform.webformcomponentoptions} value={this.props.webform.value} />
</div>
</form>
)
}
}
class Radio extends React.Component {
constructor (props) {
super(props)
this.handleOptionChange = this.handleOptionChange.bind(this)
this.state = {value: this.props.value}
}
render() {
return this.props.options.map(this.getOption)
}
handleOptionChange (e) {
this.setState({value: e.target.value})
}
getOption (option) {
return (
<div>
<input type='radio'
value={option.value}
name={option.webform_component_id}
id={option.id}
key={option.id}
checked={this.state.value === option.value}
onChange={this.handleOptionChange} />
<label htmlFor={option.id}>{option.key}</label>
</div>
)
}
}
ReactDOM.render(
<WebformApp webform={formData} />,
document.getElementById('app')
);
非常感谢你的意见,莱纳斯。你让我走上了正确的道路,我通过以下方式解决了我的问题: var formData={ 身份证号码:13951, 网络表单id:1070, 页码:0, 类型:收音机, 姓名:你从事什么行业?, 工具提示:, 重量:0,, 是否需要:1, 默认值:, 验证:, 允许其他选项:0, 其他选项文本:, 映射问题id:a295189e-d8b4-11e6-b2c5-022a69d30eef, 创建时间:2017-04-07 18:40:39, 更新时间:2017-04-07 18:40:39, 选项\u条件\u from:null, 默认值\u查询字符串\u键:, 已删除位置:null, 是否自动保存:0, 组件编号是否隐藏:0, _组件_是否内联:0, 启用\u确认\u验证:0, 确认\验证\文本:空, 其他选项:, url_映射:, webformcomponentoptions:[ { 身份证号码:13888, 网络表单组件id:13951, 关键:热情好客, 价值观:热情好客, 创建时间:2017-04-07 18:40:39, 更新时间:2017-04-07 18:40:39, 组:, 所选:false }, { 身份证号码:13889, 网络表单组件id:13951, 重点:零售, 价值:零售, 创建时间:2017-04-07 18:40:39, 更新时间:2017-04-07 18:40:39, 组:, 所选:false }, { 身份证号码:13890, 网络表单组件id:13951, 关键:其他,, 价值:其他, 创建时间:2017-04-07 18:40:39, 更新时间:2017-04-07 18:40:39, 组:, 所选:false } ] } 类WebformApp扩展了React.Component{ 渲染{ 回来 {this.props.webform.name} } }; 类。组件{ 构造器{ 超级作物; this.state={selectedOption:'Other'}; } HandLeoptionChangeEvent{ 这是我的国家{ 已选择选项:changeEvent.target.value } }; 渲染道具{ 回来 {props.index} {props.option.key} }; 渲染{ 回来 {this.props.radio.mapfunctionradio{ var selected=this.state.selectedOption==radio.value; 返回此.handleoptionchange}/>; },这个} }; }; ReactDOM.render , document.getElementById'app' ;