Guest User

Untitled

a guest
Jan 9th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class ParentComp extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = { choice: [] };
  5. }
  6. render() {
  7. return (
  8. <RadioGroup
  9. onChange={value => this.setState({ choice: value })}
  10. value={this.state.choice}
  11. />
  12. }
  13. }
  14.  
  15. class RadioGroup extends React.Component {
  16. onAdd = () => {
  17. const { value } = this.props;
  18. this.props.onChange([...value, 'NEW CHOICE!!!!!!!!']);
  19. }
  20. onRemove = (idx) => {
  21. const { value } = this.props;
  22. this.props.onChange([
  23. ...value.slice(0, idx),
  24. ...value.slice(idx + 1)
  25. ]);
  26. }
  27. onTypeChoice = (newText, idx) => {
  28. const { value } = this.props;
  29. this.props.onChange([
  30. ...value.slice(0, idx),
  31. newText,
  32. ...value.slice(idx + 1)
  33. ]);
  34. }
  35. render() {
  36. const { value } = this.props;
  37. return (
  38. <div>
  39. <ul>
  40. {value.map((item, idx) => (
  41. <li>
  42. <input type="text" value={item} onChange={e => this.onTypeChoice(e.target.value, idx)} />
  43. <a className="delete" onClick={() => this.onRemove(idx)}>Delete</a>
  44. </li>
  45. ))}
  46. </ul>
  47. <a className="add" onClick={this.onAdd}>Add</a>
  48. </div>
  49. )
  50. }
  51. }
Add Comment
Please, Sign In to add comment