• 初学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 src="Scripts/react.js"></script>
     9     <script src="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>

  • 相关阅读:
    Explicitly configure spring.jpa.open-in-view to disable this warning
    [LeetCode] 982. Triples with Bitwise AND Equal To Zero 按位与为零的三元组
    [LeetCode] 981. Time Based Key-Value Store 基于时间的键值存储
    [LeetCode] 980. Unique Paths III 不同的路径之三
    [LeetCode] 979. Distribute Coins in Binary Tree 在二叉树中分配硬币
    [LeetCode] 978. Longest Turbulent Subarray 最长湍流子数组
    [LeetCode] 976. Largest Perimeter Triangle 最大周长的三角形
    [LeetCode] 977. Squares of a Sorted Array 有序数组的平方值
    [LeetCode] 975. Odd Even Jump 奇偶跳跃
    [LeetCode] 974. Subarray Sums Divisible by K 子数组数字之和可被K整除
  • 原文地址:https://www.cnblogs.com/artech/p/react-demo-radio-button-list.html
Copyright © 2020-2023  润新知