【发布时间】:2019-01-26 10:59:40
【问题描述】:
我正在尝试在 ReactJS 中构建一个 Radiobutton-Group,就像这样:
我也在使用 Bootstrap 自定义单选按钮。
首先我制作了这个组件:
import React from "react";
import ReactDOM from 'react-dom';
import PropTypes from "prop-types";
import FormsStatic from "../FormsStatic";
class IPMRadiobutton extends React.Component {
constructor(props) {
super(props);
}
render() {
const uniqueId = FormsStatic.guid();
return (
<div className="custom-control custom-radio">
<input
type="radio"
className="custom-control-input"
id={"others-" + uniqueId}
checked={this.props.checked}
onChange={this.props.onChange}
disabled={this.props.readOnly}
/>
<label className="custom-control-label" style={{ 'fontWeight': 'normal' }} htmlFor={"others-" + uniqueId}>
<p>{this.props.description ? this.props.description : ""}</p>
</label>
</div>
);
}
}
IPMRadiobutton.propTypes = {
className: PropTypes.string,
onChange: PropTypes.func,
readOnly: PropTypes.bool,
description: PropTypes.string,
checked: PropTypes.bool
}
export default IPMRadiobutton;
现在我想像这样使用这个单选按钮:
<IPMRadiobutton {...this.props.formControlProps} checked={variable1} description="H-Akte" name="Akte" onChange={() => { console.log("Do i have to change the variable1 and variable2 here?") }} />
<IPMRadiobutton {...this.props.formControlProps} checked={variable2} description="U-Akte" name="Akte" onChange={() => { console.log("Do i have to change the variable1 and variable2 here?") }} />
我的问题是每个单选按钮都应该绑定到一个变量(或者更确切地说更改“onChange-event”中其他单选按钮的所有变量)。所以如果我检查第一个 Radiobutton,variable1 应该设置为 true,variable2 应该设置为 false。
如何使用我的组件完成这项任务?
或者有没有更好的方法来做到这一点(就像一个完整的其他概念)?
【问题讨论】:
标签: javascript reactjs radio