Created
January 3, 2018 09:04
-
-
Save MicroBenz/80ec06013e1278bac1c7af27a667165b to your computer and use it in GitHub Desktop.
Krit GGWP
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
class ParentComp extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { choice: [] }; | |
} | |
render() { | |
return ( | |
<RadioGroup | |
onChange={value => this.setState({ choice: value })} | |
value={this.state.choice} | |
/> | |
} | |
} | |
class RadioGroup extends React.Component { | |
onAdd = () => { | |
const { value } = this.props; | |
this.props.onChange([...value, 'NEW CHOICE!!!!!!!!']); | |
} | |
onRemove = (idx) => { | |
const { value } = this.props; | |
this.props.onChange([ | |
...value.slice(0, idx), | |
...value.slice(idx + 1) | |
]); | |
} | |
onTypeChoice = (newText, idx) => { | |
const { value } = this.props; | |
this.props.onChange([ | |
...value.slice(0, idx), | |
newText, | |
...value.slice(idx + 1) | |
]); | |
} | |
render() { | |
const { value } = this.props; | |
return ( | |
<div> | |
<ul> | |
{value.map((item, idx) => ( | |
<li> | |
<input type="text" value={item} onChange={e => this.onTypeChoice(e.target.value, idx)} /> | |
<a className="delete" onClick={() => this.onRemove(idx)}>Delete</a> | |
</li> | |
))} | |
</ul> | |
<a className="add" onClick={this.onAdd}>Add</a> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment