index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Button from '../Button';
  4. export default class Select 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 = [], size = 'small', theme = 'theme', excludeSelf, onChange } = 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. console.log(list);
  29. return (
  30. <div className={`select ${theme || ''}`}>
  31. <div hidden={!selecting} className="mask" onClick={() => this.close()} />
  32. <div className={`select-warp ${selecting ? 'active' : ''}`}>
  33. <Button size={size} theme={theme} radius onClick={() => this.open()}>
  34. {title} <i className={selecting ? 'up-arrow' : 'down-arrow'} />
  35. </Button>
  36. <div className={`select-body ${selecting ? 'select' : ''}`}>
  37. {list.map((item, i) => {
  38. if (excludeSelf && index === i) return null;
  39. return <div className="select-option" onClick={() => {
  40. if (onChange) onChange(item);
  41. this.close();
  42. }}>{item.title}</div>;
  43. })}
  44. </div>
  45. </div>
  46. </div>
  47. );
  48. }
  49. }