-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3d6b0c
commit fb97893
Showing
3 changed files
with
79 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
import ChildrenWithProps from './ChildrenWithProps' | ||
|
||
class App extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<ChildrenWithProps /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default App |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react' | ||
|
||
const RadioIcon = ({ isSelected }) => { | ||
return ( | ||
<div | ||
style={{ | ||
borderColor: isSelected ? 'green' : '#ccc', | ||
borderSize: '2px', | ||
borderStyle: 'solid', | ||
borderRadius: 14, | ||
height: 14, | ||
width: 14, | ||
display: 'inline-block', | ||
cursor: 'pointer', | ||
background: isSelected ? 'rgb(0,0,0,0.05)' : '', | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
class RadioOption extends React.Component { | ||
render() { | ||
return ( | ||
<div onClick={this.props.handleClick}> | ||
<RadioIcon isSelected={this.props.isSelected} />{this.props.children} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
class RadioGroup extends React.Component { | ||
render() { | ||
const childrenWithProps = React.Children.map(this.props.children, child => { | ||
return React.cloneElement(child, { | ||
handleClick: ()=> this.props.handleSelect(child.props.value), | ||
isSelected: child.props.value === this.props.selected | ||
}) | ||
}) | ||
return <div>{childrenWithProps}</div> | ||
} | ||
} | ||
|
||
class ChildrenWithProps extends React.Component { | ||
state = { selectedRadio: 'md' } | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Selected: {this.state.selectedRadio}</h1> | ||
<RadioGroup handleSelect={this.handleSelect} selected={this.state.selectedRadio}> | ||
<RadioOption value="xs">Extra Smalls</RadioOption> | ||
<RadioOption value="sm">Small</RadioOption> | ||
<RadioOption value="md">Medium</RadioOption> | ||
<RadioOption value="lg">Large</RadioOption> | ||
</RadioGroup> | ||
</div> | ||
) | ||
} | ||
|
||
handleSelect = (v) => { | ||
this.setState({ selectedRadio: v }) | ||
} | ||
} | ||
|
||
export default ChildrenWithProps |
This file contains 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