-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (65 loc) · 1.91 KB
/
index.js
File metadata and controls
69 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { Component, PropTypes } from 'react'
import Select from 'react-select'
import CSSModules from 'react-css-modules'
import 'react-select/dist/react-select.min.css'
import styles from './index.styl'
@CSSModules(styles, { allowMultiple: true, errorWhenNotFound: false })
export default class extends Component {
static propTypes = {
inputWidth: PropTypes.string,
labelWidth: PropTypes.string,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
options: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element
]),
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
])
})),
onChange: PropTypes.func,
disabled: PropTypes.bool,
required: PropTypes.bool
}
static defaultProps = {
inputWidth: '200px'
}
handleChange (v) {
if (!this.props.multi && Array.isArray(v)) { // 防止传回错误的value
return false
}
let e = {}
e.target = {}
if (v) {
e.target.value = v.value || v.map(o => o.value)
} else {
e.target.value = ''
}
this.props.onChange(e)
}
render () {
return (
<div styleName='Select' style={Object.assign({}, this.props.style)}>
<label>
{
this.props.label
? <span styleName={this.props.required ? 'label required' : 'label'} style={Object.assign({}, {width: this.props.labelWidth})}>{this.props.label}</span>
: <span></span>
}
<div style={Object.assign({}, {width: this.props.inputWidth})}>
<Select
searchable={false}
clearable={false}
{...this.props}
onChange={this.props.onChange ? this.handleChange.bind(this) : null} />
</div>
</label>
</div>
)
}
}