|
17 | 17 | // - Arrow right, arrow down should select the next option |
18 | 18 | // - Arrow left, arrow up should select the previous option |
19 | 19 | //////////////////////////////////////////////////////////////////////////////// |
20 | | -importReactfrom"react"; |
| 20 | +importReact,{useState}from"react"; |
21 | 21 | importReactDOMfrom"react-dom"; |
22 | | -importPropTypesfrom"prop-types"; |
23 | 22 |
|
24 | | -classRadioGroupextendsReact.Component{ |
25 | | -staticpropTypes={ |
26 | | -defaultValue: PropTypes.string |
27 | | -}; |
| 23 | +functionRadioGroup({ children, defaultValue }){ |
| 24 | +const[value,setValue]=useState(defaultValue); |
28 | 25 |
|
29 | | -state={value: this.props.defaultValue}; |
30 | | - |
31 | | -select(value){ |
32 | | -this.setState({ value },()=>{ |
33 | | -this.props.onChange(this.state.value); |
34 | | -}); |
35 | | -} |
36 | | - |
37 | | -render(){ |
38 | | -constchildren=React.Children.map(this.props.children,child=> |
39 | | -React.cloneElement(child,{ |
40 | | -isSelected: child.props.value===this.state.value, |
41 | | -onClick: ()=>this.select(child.props.value) |
42 | | -}) |
43 | | -); |
44 | | - |
45 | | -return<div>{children}</div>; |
46 | | -} |
| 26 | +return( |
| 27 | +<div> |
| 28 | +{React.Children.map(children,child=>{ |
| 29 | +returnReact.cloneElement(child,{ |
| 30 | +selectedValue: value, |
| 31 | + setValue |
| 32 | +}); |
| 33 | +})} |
| 34 | +</div> |
| 35 | +); |
47 | 36 | } |
48 | 37 |
|
49 | | -classRadioOptionextendsReact.Component{ |
50 | | -staticpropTypes={ |
51 | | -value: PropTypes.string |
52 | | -}; |
53 | | - |
54 | | -render(){ |
55 | | -return( |
56 | | -<divonClick={this.props.onClick}> |
57 | | -<RadioIconisSelected={this.props.isSelected}/>{" "} |
58 | | -{this.props.children} |
59 | | -</div> |
60 | | -); |
61 | | -} |
| 38 | +functionRadioOption({ children, value, selectedValue, setValue }){ |
| 39 | +return( |
| 40 | +<divonClick={()=>setValue(value)}> |
| 41 | +<RadioIconisSelected={value===selectedValue}/>{children} |
| 42 | +</div> |
| 43 | +); |
62 | 44 | } |
63 | 45 |
|
64 | | -classRadioIconextendsReact.Component{ |
65 | | -staticpropTypes={ |
66 | | -isSelected: PropTypes.bool.isRequired |
67 | | -}; |
68 | | - |
69 | | -render(){ |
70 | | -return( |
71 | | -<div |
72 | | -style={{ |
73 | | -borderColor: "#ccc", |
74 | | -borderSize: "3px", |
75 | | -borderStyle: this.props.isSelected ? "inset" : "outset", |
76 | | -height: 16, |
77 | | -width: 16, |
78 | | -display: "inline-block", |
79 | | -cursor: "pointer", |
80 | | -background: this.props.isSelected ? "rgba(0, 0, 0, 0.05)" : "" |
81 | | -}} |
82 | | -/> |
83 | | -); |
84 | | -} |
| 46 | +functionRadioIcon({ isSelected }){ |
| 47 | +return( |
| 48 | +<div |
| 49 | +style={{ |
| 50 | +borderColor: "#ccc", |
| 51 | +borderWidth: 3, |
| 52 | +borderStyle: isSelected ? "inset" : "outset", |
| 53 | +height: 16, |
| 54 | +width: 16, |
| 55 | +display: "inline-block", |
| 56 | +cursor: "pointer", |
| 57 | +background: isSelected ? "rgba(0, 0, 0, 0.05)" : "" |
| 58 | +}} |
| 59 | +/> |
| 60 | +); |
85 | 61 | } |
86 | 62 |
|
87 | | -classAppextendsReact.Component{ |
88 | | -state={ |
89 | | -radioValue: "fm" |
90 | | -}; |
91 | | - |
92 | | -render(){ |
93 | | -return( |
94 | | -<div> |
95 | | -<h1>♬ It's about time that we all turned off the radio ♫</h1> |
96 | | - |
97 | | -<h2>Radio Value: {this.state.radioValue}</h2> |
| 63 | +functionApp(){ |
| 64 | +return( |
| 65 | +<div> |
| 66 | +<h1>♬ It's about time that we all turned off the radio ♫</h1> |
98 | 67 |
|
99 | | -<RadioGroup |
100 | | -defaultValue={this.state.radioValue} |
101 | | -onChange={radioValue=>this.setState({ radioValue })} |
102 | | -> |
103 | | -<RadioOptionvalue="am">AM</RadioOption> |
104 | | -<RadioOptionvalue="fm">FM</RadioOption> |
105 | | -<RadioOptionvalue="tape">Tape</RadioOption> |
106 | | -<RadioOptionvalue="aux">Aux</RadioOption> |
107 | | -</RadioGroup> |
108 | | -</div> |
109 | | -); |
110 | | -} |
| 68 | +<RadioGroupdefaultValue="fm"> |
| 69 | +<RadioOptionvalue="am">AM</RadioOption> |
| 70 | +<RadioOptionvalue="fm">FM</RadioOption> |
| 71 | +<RadioOptionvalue="tape">Tape</RadioOption> |
| 72 | +<RadioOptionvalue="aux">Aux</RadioOption> |
| 73 | +</RadioGroup> |
| 74 | +</div> |
| 75 | +); |
111 | 76 | } |
112 | 77 |
|
113 | 78 | ReactDOM.render(<App/>,document.getElementById("app")); |
0 commit comments