index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. this.state = { selecting: false };
  8. }
  9. componentWillMount() {}
  10. componentWillUnmount() {}
  11. open() {
  12. this.setState({ selecting: true });
  13. }
  14. close() {
  15. this.setState({ selecting: false });
  16. }
  17. render() {
  18. const { selecting } = this.state;
  19. const { value, list = [] } = this.props;
  20. let index = 0;
  21. for (let i = 0; i < list.length; i += 1) {
  22. if (list[i].key === value) {
  23. index = i;
  24. break;
  25. }
  26. }
  27. const title = list.length > 0 ? list[index].title : '';
  28. return (
  29. <div className="answer-select">
  30. <div hidden={!selecting} className="mask" onClick={() => this.close()} />
  31. <div className="select-warp">
  32. <div className="text" onClick={() => this.open()}>
  33. {title}
  34. <Assets name="chooser_icon" />
  35. </div>
  36. <div className={`select-body ${selecting ? 'select' : ''}`}>
  37. {list.map(item => {
  38. return (
  39. <div className="select-option" onClick={() => this.close()}>
  40. {item.title}
  41. </div>
  42. );
  43. })}
  44. </div>
  45. </div>
  46. </div>
  47. );
  48. }
  49. }