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
141 lines (121 loc) · 3.89 KB
/
index.js
File metadata and controls
141 lines (121 loc) · 3.89 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
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import classNames from 'classnames'
class Dropdown extends Component {
constructor (props) {
super(props)
this.state = {
selected: props.value || {
label: props.placeholder || 'Select...',
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.placeholder) {
this.setState({selected: { label: 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 (event.type === 'mousedown' && event.button !== 0) return
event.stopPropagation()
event.preventDefault()
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) {
let optionClass = classNames({
[`${this.props.baseClassName}-option`]: true,
'is-selected': option === this.state.selected
})
let value = option.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)) {
this.setState({ isOpen: false })
}
}
}
render () {
const { baseClassName } = this.props
const placeHolderValue = typeof this.state.selected === 'string' ? this.state.selected : this.state.selected.label
let value = (<div className={`${baseClassName}-placeholder`}>{placeHolderValue}</div>)
let menu = this.state.isOpen ? <div className={`${baseClassName}-menu`}>{this.buildMenu()}</div> : null
let dropdownClass = classNames({
[`${baseClassName}-root`]: true,
'is-open': this.state.isOpen
})
return (
<div className={dropdownClass}>
<div className={`${baseClassName}-control`} onMouseDown={this.handleMouseDown.bind(this)} onTouchEnd={this.handleMouseDown.bind(this)}>
{value}
<span className={`${baseClassName}-arrow`} />
</div>
{menu}
</div>
)
}
}
Dropdown.defaultProps = { baseClassName: 'Dropdown' }
export default Dropdown