Skip to content

Instantly share code, notes, and snippets.

@MicroBenz
Created January 3, 2018 09:04
Show Gist options
  • Save MicroBenz/80ec06013e1278bac1c7af27a667165b to your computer and use it in GitHub Desktop.
Save MicroBenz/80ec06013e1278bac1c7af27a667165b to your computer and use it in GitHub Desktop.
Krit GGWP
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