index.js 1.55 KB
Newer Older
zhanghaozhe committed
1 2 3
import React, { Component } from "react"
import "./index.scss"
import classnames from "classnames"
zhanghaozhe committed
4 5

class Question extends Component {
zhanghaozhe committed
6 7
  handleSelect = (option) => {
    const { selectAnswer } = this.props
zhanghaozhe committed
8 9
    if (selectAnswer) {
      selectAnswer(this.props.question, option.id)
zhanghaozhe committed
10
      this.setState({
zhanghaozhe committed
11
        selectedId: option.id,
zhanghaozhe committed
12 13 14 15 16
      })
    }
  }

  render() {
zhanghaozhe committed
17
    const { question, answer, activeIndex, category } = this.props
zhanghaozhe committed
18
    return (
zhanghaozhe committed
19
      <div className={"question-container"}>
zhanghaozhe committed
20 21 22
        {
          <div className="question">
            {activeIndex !== undefined && `${activeIndex + 1}.`}
zhanghaozhe committed
23 24
            {category && <span className={"category"}>{category}</span>}&nbsp;
            <span dangerouslySetInnerHTML={{ __html: question.ques }}></span>
zhanghaozhe committed
25 26
          </div>
        }
zhanghaozhe committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        <ul className={"options"}>
          {!!question.options.length &&
            question.options.map((item, index) => {
              return (
                <li
                  key={item.id}
                  className={classnames({
                    active: answer && answer[question.id] === item.id,
                    error: item.user_select && !item.is_ans,
                    correct: item.is_ans,
                  })}
                  onClick={this.handleSelect.bind(this, item)}
                >
                  <div className={"letter"}>
                    {String.fromCharCode(65 + index)}
                  </div>
                  <div>{item.des}</div>
                </li>
              )
            })}
zhanghaozhe committed
47 48
        </ul>
      </div>
zhanghaozhe committed
49
    )
zhanghaozhe committed
50 51 52
  }
}

zhanghaozhe committed
53
export default Question