123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import React, { Component } from 'react';
- import './index.less';
- import Button from '../Button';
- export default class Select extends Component {
- constructor(props) {
- super(props);
- this.state = { selecting: false };
- }
- componentWillMount() { }
- componentWillUnmount() { }
- open() {
- this.setState({ selecting: true });
- }
- close() {
- this.setState({ selecting: false });
- }
- render() {
- const { selecting } = this.state;
- const { value, list = [], size = 'small', theme = 'theme', excludeSelf, onChange } = this.props;
- let index = 0;
- for (let i = 0; i < list.length; i += 1) {
- if (list[i].key === value) {
- index = i;
- break;
- }
- }
- const title = list.length > 0 ? list[index].title : '';
- return (
- <div className={`select ${theme || ''}`}>
- <div hidden={!selecting} className="mask" onClick={() => this.close()} />
- <div className={`select-warp ${selecting ? 'active' : ''}`}>
- <Button size={size} theme={theme} radius onClick={() => this.open()}>
- {title} <i className={selecting ? 'up-arrow' : 'down-arrow'} />
- </Button>
- <div className={`select-body ${selecting ? 'select' : ''}`}>
- {list.map((item, i) => {
- if (excludeSelf && index === i) return null;
- return <div className="select-option" onClick={() => {
- if (onChange) onChange(item);
- this.close();
- }}>{item.title}</div>;
- })}
- </div>
- </div>
- </div>
- );
- }
- }
|