index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. return (
  29. <div className={`select ${theme || ''}`}>
  30. <div hidden={!selecting} className="mask" onClick={() => this.close()} />
  31. <div className={`select-warp ${selecting ? 'active' : ''}`}>
  32. <Button size={size} theme={theme} radius onClick={() => this.open()}>
  33. {title} <i className={selecting ? 'up-arrow' : 'down-arrow'} />
  34. </Button>
  35. <div className={`select-body ${selecting ? 'select' : ''}`}>
  36. {list.map((item, i) => {
  37. if (excludeSelf && index === i) return null;
  38. return <div className="select-option" onClick={() => {
  39. if (onChange) onChange(item);
  40. this.close();
  41. }}>{item.title}</div>;
  42. })}
  43. </div>
  44. </div>
  45. </div>
  46. );
  47. }
  48. }