index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { placeholder, value, list = [], size = 'basic', 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 && index >= 0 ? list[index].title : placeholder;
  28. return (
  29. <div className={`select ${theme || ''} ${size}`}>
  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 ? 'selected' : ''}`}>
  36. {list.map((item, i) => {
  37. if (excludeSelf && index === i) return null;
  38. return (
  39. <div
  40. className="select-option"
  41. onClick={() => {
  42. if (onChange) onChange(item);
  43. this.close();
  44. }}
  45. >
  46. {item.title}
  47. </div>
  48. );
  49. })}
  50. </div>
  51. </div>
  52. </div>
  53. );
  54. }
  55. }