Last active
August 15, 2018 17:34
-
-
Save bartimaeus/62c0a9e4ea6d7d6ddba6ea41ffa5e331 to your computer and use it in GitHub Desktop.
Lesson 02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./index.css"; | |
import React, { Component } from "react"; | |
import FaPlay from "react-icons/lib/fa/play"; | |
import FaPause from "react-icons/lib/fa/pause"; | |
import FaForward from "react-icons/lib/fa/forward"; | |
import FaBackward from "react-icons/lib/fa/backward"; | |
class RadioGroup extends Component { | |
state = { | |
value: null | |
}; | |
onSelect = value => { | |
this.setState({ value }); | |
}; | |
render() { | |
const children = React.Children.map(this.props.children, child => { | |
return React.cloneElement(child, { | |
onSelect: this.onSelect, | |
activeValue: this.state.value | |
}); | |
}); | |
// const children = this.props.children.map(child => { | |
// return React.cloneElement(child, { | |
// onSelect: this.onSelect, | |
// activeValue: this.state.value | |
// }); | |
// }); | |
return ( | |
<fieldset className="radio-group"> | |
<legend>{this.props.legend}</legend> | |
{children} | |
</fieldset> | |
); | |
} | |
} | |
class RadioButton extends Component { | |
render() { | |
const { activeValue, onSelect, value } = this.props; | |
const isActive = activeValue === value; | |
const className = "radio-button " + (isActive ? "active" : ""); | |
return ( | |
<button className={className} onClick={() => onSelect(value)}> | |
{this.props.children} | |
</button> | |
); | |
} | |
} | |
class App extends Component { | |
render() { | |
return ( | |
<div> | |
<RadioGroup legend="Radio Group"> | |
<RadioButton value="back"> | |
<FaBackward /> | |
</RadioButton> | |
<RadioButton value="play"> | |
<FaPlay /> | |
</RadioButton> | |
<RadioButton value="pause"> | |
<FaPause /> | |
</RadioButton> | |
<RadioButton value="forward"> | |
<FaForward /> | |
</RadioButton> | |
</RadioGroup> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment