Guest User

Untitled

a guest
Apr 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // Questions Component.
  2.  
  3. import React, { Component } from 'react';
  4. import PropTypes from 'prop-types';
  5.  
  6. import Question from './Question';
  7.  
  8. export default () => (
  9. questions.map((question, index) => (<Question question={question} key={index}/>))
  10. );
  11.  
  12. // Question Component.
  13.  
  14. import React, { Component } from 'react';
  15. import PropTypes from 'prop-types';
  16.  
  17. const pollDetailsStyle = {
  18. padding: 10,
  19. border: '1px solid #eed3d7',
  20. borderRadius: 10,
  21. marginBottom: 5
  22. };
  23.  
  24. class Question extends Component {
  25. constructor(props) {
  26. super(props);
  27.  
  28. this.state = {
  29. selectedOption: null
  30. };
  31. this.handleChange = this.handleChange.bind(this);
  32. }
  33.  
  34. handleChange(id) {
  35. this.setState({
  36. selectedOption: id
  37. });
  38. };
  39.  
  40. render() {
  41. const { question, options, questionId } = this.props.question;
  42.  
  43. const optionsDisplay = options.map(({ text, id }, optionIndex) => (
  44. <p key={optionIndex}>
  45. <input
  46. type="radio"
  47. onChange={() => this.handleChange(id)}
  48. checked={this.state.selectedOption === id}
  49. />
  50. {text}
  51. </p>
  52. ));
  53.  
  54. return (
  55. <div>
  56. <div style={pollDetailsStyle}>
  57. <p style={{ fontWeight: 'bold' }}>
  58. {question}
  59. </p>
  60. <form>
  61. {optionsDisplay}
  62. </form>
  63. </div>
  64. </div>
  65. );
  66. }
  67. }
  68.  
  69. export default Question;
Add Comment
Please, Sign In to add comment