1
0

index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import RadioItem from '../RadioItem';
  4. export default class Answer extends Component {
  5. constructor(props) {
  6. super(props);
  7. const single = [];
  8. const double = [];
  9. switch (props.type) {
  10. case 'single':
  11. for (let i = 0; i < props.list.length; i += 1) {
  12. single.push(false);
  13. }
  14. break;
  15. case 'double':
  16. for (let i = 0; i < props.list.length; i += 1) {
  17. double.push([false, false]);
  18. }
  19. break;
  20. default:
  21. break;
  22. }
  23. this.state = { single, double };
  24. }
  25. onChangeSingle(index) {
  26. const { single } = this.state;
  27. for (let i = 0; i < single.length; i += 1) {
  28. single[i] = false;
  29. }
  30. single[index] = true;
  31. this.setState({ single });
  32. this.onChange(single);
  33. }
  34. onChangeDouble(index, column) {
  35. const { direction } = this.props;
  36. let { double } = this.state;
  37. switch (direction) {
  38. case 'landscape':
  39. double[index] = double[index].map(() => false);
  40. break;
  41. case 'portrait':
  42. double = double.map(row => {
  43. row[column] = false;
  44. return row;
  45. });
  46. break;
  47. default:
  48. break;
  49. }
  50. double[index][column] = true;
  51. this.setState({ double });
  52. this.onChange(double);
  53. }
  54. onChange(value) {
  55. const { onChange } = this.props;
  56. if (onChange) onChange(value);
  57. }
  58. render() {
  59. return <div className="answer">{this.renderDetail()}</div>;
  60. }
  61. renderDetail() {
  62. const { type } = this.props;
  63. switch (type) {
  64. case 'single':
  65. return this.renderList();
  66. case 'double':
  67. return this.renderTable();
  68. default:
  69. return <div />;
  70. }
  71. }
  72. renderList() {
  73. const { list = [] } = this.props;
  74. const { single = [] } = this.state;
  75. return (
  76. <div className="list">
  77. {list.map((item, index) => {
  78. return (
  79. <div className="item">
  80. <RadioItem checked={single[index]} onClick={() => this.onChangeSingle(index)} />
  81. <div className="text">{item}</div>
  82. </div>
  83. );
  84. })}
  85. </div>
  86. );
  87. }
  88. renderTable() {
  89. const { list = [], first, second } = this.props;
  90. const { double = [] } = this.state;
  91. return (
  92. <table border="1" bordercolor="#d8d8d8">
  93. <thead>
  94. <tr className="bg">
  95. <th align="center" width="100" className="t-c">
  96. {first}
  97. </th>
  98. <th align="center" width="100" className="t-c">
  99. {second}
  100. </th>
  101. <th />
  102. </tr>
  103. </thead>
  104. <tbody>
  105. {list.map((item, index) => {
  106. return (
  107. <tr>
  108. <td align="center" className="bg">
  109. <RadioItem checked={double[index][0]} onClick={() => this.onChangeDouble(index, 0)} />
  110. </td>
  111. <td align="center" className="bg">
  112. <RadioItem checked={double[index][1]} onClick={() => this.onChangeDouble(index, 1)} />
  113. </td>
  114. <td>{item}</td>
  115. </tr>
  116. );
  117. })}
  118. </tbody>
  119. </table>
  120. );
  121. }
  122. }