React封装RadioGroup

 1 class RadioGroup extends React.Component {
 2     getRadioComponent(item, index) {
 3         return <div className="custom-radio" key={this.props.name + '-' + index}>
 4             <input id={item.id} type="radio" name={this.props.name} value={item.value} checked={this.props.checkedValue == item.value} disabled={item.disabled} onChange={this.props.checkChanged} />
 5             <label htmlFor={item.id} disabled={item.disabled}>{item.text}</label>
 6         </div>
 7     }
 8 
 9     render() {
10         let
11             className = this.props.isHorizontal ? 'horizontal' : 'normal',
12             items = this.props.items || [];
13         return <div id={this.props.id} className={className}>
14             {
15                 items.map((item, index) => {
16                     if (this.props.isHorizontal)
17                         return this.getRadioComponent(item, index);
18                     else
19                         return <div key={index}>{this.getRadioComponent(item, index)}</div>;
20                 })
21             }
22         </div>
23     }
24 }
25 
26 export default RadioGroup;

使用:

 1 const type = {
 2         Auto: 0,
 3         Use: 1
 4 }
 5 this.state = {
 6     defaultCheckedValue: type.Use,
 7     radioOptions: [
 8         {
 9             id: "id-auto",
10             value: type.Auto,
11             text: 'auto'
12                 {
13             id: "id-use",
14             value: type.Use,
15             text: 'use'
16         }],
17 }
18 
19 onActionSelectedChange(e, args) {
20         this.setState(Object.assign(this.state, {
21             defaultCheckedValue: parseInt(e.currentTarget.value),
22         }));
23     }
1 <RadioGroup
2     id={"id"}
3     name={"dqhan-name"}
4     isHorizontal={true}
5     items={this.state.radioOptions}
6     checkedValue={this.state.defaultCheckedValue}
7     checkChanged={this.handleRadioChange}
8 />

 

转载于:https://www.cnblogs.com/moran1992/p/9390646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值