|
1 | | -import React, {Component} from "react"; |
2 | | -import styles from "./index.styl"; |
3 | | - |
4 | | -export default class select extends Component { |
5 | | - static defaultProps = { |
6 | | - options: ["None"], |
7 | | - label: "", |
8 | | - color: "#0a86b1", |
9 | | - width: 100, |
10 | | - afterSelect: () => {} |
11 | | - |
12 | | - }; |
13 | | - |
14 | | - static propTypes = { |
15 | | - options: React.PropTypes.array.isRequired, |
16 | | - label: React.PropTypes.string, |
17 | | - color: React.PropTypes.string, |
18 | | - width: React.PropTypes.number, |
19 | | - afterSelect: React.PropTypes.func |
20 | | - }; |
21 | | - |
22 | | - state = { |
23 | | - selected: 0 |
24 | | - }; |
25 | | - |
26 | | - showLabel() { |
27 | | - if(this.props.label.length) { |
28 | | - return (<span>{this.props.label} </span>); |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - handleChange(e) { |
33 | | - this.setState({ |
34 | | - selected: e.target.value |
35 | | - }) |
36 | | - |
37 | | - return this.props.afterSelect(this.props.options[e.target.value]) |
38 | | - } |
39 | | - |
40 | | - render() { |
41 | | - var options = this.props.options; |
42 | | - var linkCSS = { |
43 | | - maxWidth: this.props.width, |
44 | | - color: this.props.color |
45 | | - }; |
46 | | - |
47 | | - return ( |
48 | | - <div className={styles.select} onChange={this.handleChange.bind(this)}> |
49 | | - {this.showLabel()} |
50 | | - <span className={styles.value + ' text-truncate'} style={linkCSS}>{options[this.state.selected]}</span> |
51 | | - <i className="icon-budicon-460"></i> |
52 | | - <select> |
53 | | - { |
54 | | - options.map((name, index) => { |
55 | | - return ( |
56 | | - <option key={index} value={index}>{name}</option> |
57 | | - ); |
58 | | - }) |
59 | | - } |
60 | | - </select> |
61 | | - </div> |
62 | | - ); |
63 | | - } |
64 | | -} |
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import styles from './index.styl'; |
| 3 | + |
| 4 | +const Select = (props) => { |
| 5 | + const { options, selected, label, color, handleChange } = props; |
| 6 | + |
| 7 | + return ( |
| 8 | + <div className={styles.select}> |
| 9 | + { label && <span>{label}</span> } |
| 10 | + <span className={`${styles.value} text-truncate`} style={{ color }}>{options[selected]}</span> |
| 11 | + <i className="icon-budicon-460" /> |
| 12 | + <select onChange={handleChange} {...props}> |
| 13 | + { |
| 14 | + options.map((name, index) => <option key={index} value={index}>{name}</option>) |
| 15 | + } |
| 16 | + </select> |
| 17 | + </div> |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +Select.defaultProps = { |
| 22 | + options: [ 'Default' ], |
| 23 | + selected: 0, |
| 24 | + label: '', |
| 25 | + handleChange: () => {} |
| 26 | +}; |
| 27 | + |
| 28 | +Select.propTypes = { |
| 29 | + options: PropTypes.arrayOf(PropTypes.string).isRequired, |
| 30 | + selected: PropTypes.number.isRequired, |
| 31 | + handleChange: PropTypes.func.isRequired, |
| 32 | + label: PropTypes.string, |
| 33 | + color: PropTypes.string |
| 34 | +}; |
| 35 | + |
| 36 | +export default Select; |
0 commit comments