forked from fraserxu/react-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (158 loc) · 5.19 KB
/
index.js
File metadata and controls
181 lines (158 loc) · 5.19 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
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
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: props.value || {
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 && newProps.value !== this.state.selected) {
this.setState({selected: newProps.value})
} else if (!newProps.value) {
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
})
}
}
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) {
const classes = {
[`${this.props.baseClassName}-option`]: true,
[option.className]: !!option.className,
'is-selected': option === this.state.selected
}
const optionClass = classNames(classes)
let value = option.value
if (typeof value === 'undefined') {
value = option.label || option
}
let label = option.label || option.value || option
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 })
}
}
}
}
render () {
const { baseClassName, controlClassName, placeholderClassName, menuClassName, arrowClassName, 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
})
const menuClass = classNames({
[`${baseClassName}-menu`]: true,
[menuClassName]: !!menuClassName,
'render-closed': this.props.renderClosed
})
const arrowClass = classNames({
[`${baseClassName}-arrow`]: true,
[arrowClassName]: !!arrowClassName
})
const value = (<div className={placeholderClass}>{placeHolderValue}</div>)
const menu = this.state.isOpen || this.props.renderClosed
? <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}
<span className={arrowClass} />
</div>
{menu}
</div>
)
}
}
Dropdown.defaultProps = { baseClassName: 'Dropdown' }
export default Dropdown