index.js 1.52 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './index.scss'
zhanghaozhe committed
3 4
import { html } from "@/utils"
import classnames from 'classnames'
zhanghaozhe committed
5 6 7 8


class Question extends Component {

zhanghaozhe committed
9 10 11 12
  handleSelect = option => {
    const {selectAnswer} = this.props
    if (selectAnswer) {
      selectAnswer(this.props.question, option.id)
zhanghaozhe committed
13
      this.setState({
zhanghaozhe committed
14
        selectedId: option.id,
zhanghaozhe committed
15 16 17 18 19
      })
    }
  }

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

export default Question;