index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Assets from '@src/components/Assets';
  4. export default class AnswerSelect extends Component {
  5. constructor(props) {
  6. super(props);
  7. const select = [];
  8. for (let i = 0; i < props.list.length; i += 1) {
  9. select[i] = false;
  10. }
  11. this.state = { selecting: false, select };
  12. }
  13. onChange(index) {
  14. const { onChange } = this.props;
  15. const { select } = this.state;
  16. for (let i = 0; i < select.length; i += 1) {
  17. select[i] = false;
  18. }
  19. select[index] = true;
  20. if (onChange) onChange(select);
  21. }
  22. open() {
  23. this.setState({ selecting: true });
  24. }
  25. close() {
  26. this.setState({ selecting: false });
  27. }
  28. render() {
  29. const { selecting } = this.state;
  30. const { value, list = [] } = this.props;
  31. let index = 0;
  32. for (let i = 0; i < list.length; i += 1) {
  33. if (list[i].key === value) {
  34. index = i;
  35. break;
  36. }
  37. }
  38. const title = list.length > 0 ? list[index] : '';
  39. return (
  40. <div className="answer-select">
  41. <div hidden={!selecting} className="mask" onClick={() => this.close()} />
  42. <div className="select-warp">
  43. <div className="text" onClick={() => this.open()}>
  44. {title}
  45. <Assets name="chooser_icon" />
  46. </div>
  47. <div className={`select-body ${selecting ? 'select' : ''}`}>
  48. {list.map(item => {
  49. return (
  50. <div className="select-option" onClick={() => this.close()}>
  51. {item}
  52. </div>
  53. );
  54. })}
  55. </div>
  56. </div>
  57. </div>
  58. );
  59. }
  60. }