import React, { Component } from 'react'; import './index.less'; import Assets from '@src/components/Assets'; function getKey(type, index, selected, answer) { return `${(selected[type] || [])[index] ? 'selected' : ''} ${(answer[type] || [])[index] ? 'true' : 'false'}`; } export default class AnswerSelect extends Component { constructor(props) { super(props); const select = []; for (let i = 0; i < props.list.length; i += 1) { select[i] = false; } let index = -1; const { selected = {}, answer = {}, type } = props; let result = true; (selected[type] || []).forEach((row, i) => { if (row) index = i; if (row !== answer[type][i]) result = false; }); this.state = { selecting: false, select, index, result }; } onChange(index) { const { onChange } = this.props; const { select } = this.state; for (let i = 0; i < select.length; i += 1) { select[i] = false; } select[index] = true; if (onChange) onChange(select); this.close(); this.setState({ index }); } open() { this.setState({ selecting: true }); } close() { this.setState({ selecting: false }); } render() { const { selecting, index, result } = this.state; const { list = [], show, selected = {}, answer = {}, type, fix } = this.props; const title = list.length > 0 && index >= 0 ? list[index] : ' '; return (