forked from fraserxu/react-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (190 loc) · 6.17 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import classNames from 'classnames'
const DEFAULT_PLACEHOLDER_STRING = 'Select...'
class Dropdown extends Component {
constructor (props) {
super(props)
this.state = {
selected: this.parseValue(props.value, props.options) || {
label: typeof props.placeholder === 'undefined' ? DEFAULT_PLACEHOLDER_STRING : props.placeholder,
value: ''
},
isOpen: false
}
this.mounted = true
this.handleDocumentClick = this.handleDocumentClick.bind(this)
this.fireChangeEvent = this.fireChangeEvent.bind(this)
}
componentWillReceiveProps (newProps) {
if (newProps.value) {
var selected = this.parseValue(newProps.value, newProps.options)
if (selected !== this.state.selected) {
this.setState({selected: selected})
}
} else {
this.setState({selected: {
label: typeof newProps.placeholder === 'undefined' ? DEFAULT_PLACEHOLDER_STRING : newProps.placeholder,
value: ''
}})
}
}
componentDidMount () {
document.addEventListener('click', this.handleDocumentClick, false)
document.addEventListener('touchend', this.handleDocumentClick, false)
}
componentWillUnmount () {
this.mounted = false
document.removeEventListener('click', this.handleDocumentClick, false)
document.removeEventListener('touchend', this.handleDocumentClick, false)
}
handleMouseDown (event) {
if (this.props.onFocus && typeof this.props.onFocus === 'function') {
this.props.onFocus(this.state.isOpen)
}
if (event.type === 'mousedown' && event.button !== 0) return
event.stopPropagation()
event.preventDefault()
if (!this.props.disabled) {
this.setState({
isOpen: !this.state.isOpen
})
}
}
parseValue (value, options) {
let option
if (typeof value === 'string') {
for (var i = 0, num = options.length; i < num; i++) {
if (options[i].type === 'group') {
const match = options[i].items.filter(item => item.value === value)
if (match.length) {
option = match[0]
}
} else if (typeof options[i].value !== 'undefined' && options[i].value === value) {
option = options[i]
}
}
}
return option || value
}
setValue (value, label) {
let newState = {
selected: {
value,
label},
isOpen: false
}
this.fireChangeEvent(newState)
this.setState(newState)
}
fireChangeEvent (newState) {
if (newState.selected !== this.state.selected && this.props.onChange) {
this.props.onChange(newState.selected)
}
}
renderOption (option) {
let value = option.value
if (typeof value === 'undefined') {
value = option.label || option
}
let label = option.label || option.value || option
const classes = {
[`${this.props.baseClassName}-option`]: true,
[option.className]: !!option.className,
'is-selected': value === this.state.selected.value || value === this.state.selected
}
const optionClass = classNames(classes)
return (
<div
key={value}
className={optionClass}
onMouseDown={this.setValue.bind(this, value, label)}
onClick={this.setValue.bind(this, value, label)}>
{label}
</div>
)
}
buildMenu () {
let { options, baseClassName } = this.props
let ops = options.map((option) => {
if (option.type === 'group') {
let groupTitle = (<div className={`${baseClassName}-title`}>
{option.name}
</div>)
let _options = option.items.map((item) => this.renderOption(item))
return (
<div className={`${baseClassName}-group`} key={option.name}>
{groupTitle}
{_options}
</div>
)
} else {
return this.renderOption(option)
}
})
return ops.length ? ops : <div className={`${baseClassName}-noresults`}>
No options found
</div>
}
handleDocumentClick (event) {
if (this.mounted) {
if (!ReactDOM.findDOMNode(this).contains(event.target)) {
if (this.state.isOpen) {
this.setState({ isOpen: false })
}
}
}
}
isValueSelected () {
return typeof this.state.selected === 'string' || this.state.selected.value !== ''
}
render () {
const { baseClassName, controlClassName, placeholderClassName, menuClassName, arrowClassName, arrowClosed, arrowOpen, className } = this.props
const disabledClass = this.props.disabled ? 'Dropdown-disabled' : ''
const placeHolderValue = typeof this.state.selected === 'string' ? this.state.selected : this.state.selected.label
const dropdownClass = classNames({
[`${baseClassName}-root`]: true,
[className]: !!className,
'is-open': this.state.isOpen
})
const controlClass = classNames({
[`${baseClassName}-control`]: true,
[controlClassName]: !!controlClassName,
[disabledClass]: !!disabledClass
})
const placeholderClass = classNames({
[`${baseClassName}-placeholder`]: true,
[placeholderClassName]: !!placeholderClassName,
'is-selected': this.isValueSelected()
})
const menuClass = classNames({
[`${baseClassName}-menu`]: true,
[menuClassName]: !!menuClassName
})
const arrowClass = classNames({
[`${baseClassName}-arrow`]: true,
[arrowClassName]: !!arrowClassName
})
const value = (<div className={placeholderClass}>
{placeHolderValue}
</div>)
const menu = this.state.isOpen ? <div className={menuClass}>
{this.buildMenu()}
</div> : null
return (
<div className={dropdownClass}>
<div className={controlClass} onMouseDown={this.handleMouseDown.bind(this)} onTouchEnd={this.handleMouseDown.bind(this)}>
{value}
<div className={`${baseClassName}-arrow-wrapper`}>
{arrowOpen && arrowClosed
? this.state.isOpen ? arrowOpen : arrowClosed
: <span className={arrowClass} />}
</div>
</div>
{menu}
</div>
)
}
}
Dropdown.defaultProps = { baseClassName: 'Dropdown' }
export default Dropdown