为了账号安全,请及时绑定邮箱和手机立即绑定

初学ReactJS,写了一个RadioButtonList组件

标签:
架构

复制代码 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>React Demo</title>
 5     <meta charset="utf-8" />
 6 </head>
 7 <body>
 8     <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="Scripts/react.js"></script>
 9     <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="Scripts/JSXTransformer.js"></script>
10     <div id="container1"></div>
11     <div id="container2"></div>
12     <script type="text/jsx">
13         var RadioButton = React.createClass({
14             render: function(){
15                 return (
16                     <label htmlFor={this.props.id}>
17                         <input type="radio"
18                                id={this.props.id}
19                                name={this.props.name}
20                                value={this.props.value}
21                                checked={this.props.checked}
22                                onChange = {this.handleChange}/>
23                         {this.props.text}
24                     </label>
25                 );
26             },
27             handleChange: function(event){
28                 this.setState({selectedValue: event.target.value});
29                 if(this.props.onSelectedValueChanged){
30                     this.props.onSelectedValueChanged(event);
31                 }
32             }
33         });
34 
35         var RadioButtonList = React.createClass({
36                 render: function(){
37                     return (
38                         <span className="radioButtonList">{this.renderRadionButtons()}</span>
39                     );
40                 },
41                 renderRadionButtons: function(){
42                     return this.props.listItems.map(function(item, index){                 
43                         return (<RadioButton id={this.props.name + "_" + index} 
44                                      name={this.props.name} 
45                                      value={item.value||item}
46                                      text = {item.text||item}
47                                      checked={this.state.selectedValue == (item.value||item)}
48                                      onSelectedValueChanged = {this.onSelectedValueChanged}/>);
49                     }.bind(this));
50                 },
51                 getInitialState: function(){
52                     return {selectedValue: this.props.selectedValue};
53                 },
54                 onSelectedValueChanged: function(event){
55                     this.setState({selectedValue: event.target.value});
56                 }
57             });
58             React.render(<label for="province">Province:<RadioButtonList name="province" listItems={["Jiangsu","Zhejiang","Shanghai"]} selectedValue="Shanghai" /></label>, document.getElementById("container1")) ;
59             React.render(<label for="gender">Gender:<RadioButtonList name="gender" listItems={[{value:"M", text:"Male"}, {value:"F", text:"Female"}]} selectedValue="F" /></label>, document.getElementById("container2"))
60 </script> 
61 </body>
62 </html>复制代码

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消