import React, { Component } from 'react'; import './index.less'; import RadioItem from '../RadioItem'; export default class Answer extends Component { constructor(props) { super(props); const single = []; const double = []; switch (props.type) { case 'single': for (let i = 0; i < props.list.length; i += 1) { single.push(false); } break; case 'double': for (let i = 0; i < props.list.length; i += 1) { double.push([false, false]); } break; default: break; } this.state = { single, double }; } onChangeSingle(index) { const { single } = this.state; for (let i = 0; i < single.length; i += 1) { single[i] = false; } single[index] = true; this.setState({ single }); this.onChange(single); } onChangeDouble(index, column) { const { direction } = this.props; let { double } = this.state; switch (direction) { case 'landscape': double[index] = double[index].map(() => false); break; case 'portrait': double = double.map(row => { row[column] = false; return row; }); break; default: break; } double[index][column] = true; this.setState({ double }); this.onChange(double); } onChange(value) { const { onChange } = this.props; if (onChange) onChange(value); } render() { return
{this.renderDetail()}
; } renderDetail() { const { type } = this.props; switch (type) { case 'single': return this.renderList(); case 'double': return this.renderTable(); default: return
; } } renderList() { const { list = [] } = this.props; const { single = [] } = this.state; return (
{list.map((item, index) => { return (
this.onChangeSingle(index)} />
{item}
); })}
); } renderTable() { const { list = [], first, second } = this.props; const { double = [] } = this.state; return ( {list.map((item, index) => { return ( ); })}
{first} {second}
this.onChangeDouble(index, 0)} /> this.onChangeDouble(index, 1)} /> {item}
); } }