This repository was archived by the owner on Mar 21, 2019. It is now read-only.
forked from dowjones/react-dropdown-tree-select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
237 lines (204 loc) · 6.99 KB
/
Copy pathindex.js
File metadata and controls
237 lines (204 loc) · 6.99 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*!
* React Dropdown Tree Select
* A lightweight, fast and highly customizable tree select component.
* Hrusikesh Panda <hrusikesh.panda@dowjones.com>
* Copyright (c) 2017 Dow Jones, Inc. <support@dowjones.com> (http://dowjones.com)
* license MIT
* see https://github.com/dowjones/react-dropdown-tree-select
*/
import cn from 'classnames/bind'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { isOutsideClick } from './utils'
import Input from './input'
import Tree from './tree'
import TreeManager from './tree-manager'
import styles from './index.css'
const cx = cn.bind(styles)
class DropdownTreeSelect extends Component {
static propTypes = {
data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
clearSearchOnChange: PropTypes.bool,
keepTreeOnSearch: PropTypes.bool,
placeholderText: PropTypes.string,
showDropdown: PropTypes.bool,
className: PropTypes.string,
onChange: PropTypes.func,
onAction: PropTypes.func,
onNodeToggle: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
simpleSelect: PropTypes.bool,
noMatchesText: PropTypes.string,
showPartiallySelected: PropTypes.bool,
disabled: PropTypes.bool,
readOnly: PropTypes.bool
}
static defaultProps = {
onFocus: () => {},
onBlur: () => {},
onChange: () => {}
}
constructor(props) {
super(props)
this.state = {
showDropdown: this.props.showDropdown || false,
searchModeOn: false
}
}
createList = (tree, simple, showPartial) => {
this.treeManager = new TreeManager(tree, simple, showPartial)
return this.treeManager.tree
}
resetSearchState = () => {
// clear the search criteria and avoid react controlled/uncontrolled warning
this.searchInput.value = ''
return {
tree: this.treeManager.restoreNodes(), // restore the tree to its pre-search state
searchModeOn: false,
allNodesHidden: false
}
}
componentWillMount() {
const tree = this.createList(this.props.data, this.props.simpleSelect, this.props.showPartiallySelected)
const tags = this.treeManager.getTags()
this.setState({ tree, tags })
}
componentWillUnmount() {
document.removeEventListener('click', this.handleOutsideClick, false)
}
componentWillReceiveProps(nextProps) {
const tree = this.createList(nextProps.data, nextProps.simpleSelect, nextProps.showPartiallySelected)
const tags = this.treeManager.getTags()
this.setState({ tree, tags })
}
handleClick = () => {
this.setState(prevState => {
// keep dropdown active when typing in search box
const showDropdown = this.keepDropdownActive || !prevState.showDropdown
// register event listeners only if there is a state change
if (showDropdown !== prevState.showDropdown) {
if (showDropdown) {
document.addEventListener('click', this.handleOutsideClick, false)
} else {
document.removeEventListener('click', this.handleOutsideClick, false)
}
}
if (showDropdown) this.props.onFocus()
else this.props.onBlur()
return !showDropdown ? { showDropdown, ...this.resetSearchState() } : { showDropdown }
})
}
handleOutsideClick = e => {
if (!isOutsideClick(e, this.props.className)) {
return
}
this.handleClick()
}
onInputChange = value => {
const { allNodesHidden, tree } = this.treeManager.filterTree(value, this.props.keepTreeOnSearch)
const searchModeOn = value.length > 0
this.setState({
tree,
searchModeOn,
allNodesHidden
})
}
onTagRemove = id => {
this.onCheckboxChange(id, false)
}
onNodeToggle = id => {
this.treeManager.toggleNodeExpandState(id)
const tree = this.state.searchModeOn ? this.treeManager.matchTree : this.treeManager.tree
this.setState({ tree })
typeof this.props.onNodeToggle === 'function' && this.props.onNodeToggle(this.treeManager.getNodeById(id))
}
onCheckboxChange = (id, checked) => {
this.treeManager.setNodeCheckedState(id, checked)
let tags = this.treeManager.getTags()
const showDropdown = this.props.simpleSelect ? false : this.state.showDropdown
if (!tags.length) {
this.treeManager.restoreDefaultValues()
tags = this.treeManager.getTags()
}
const tree = this.state.searchModeOn ? this.treeManager.matchTree : this.treeManager.tree
const nextState = {
tree,
tags,
showDropdown
}
if (this.props.simpleSelect || this.props.clearSearchOnChange) {
Object.assign(nextState, this.resetSearchState())
}
if (this.props.simpleSelect) {
document.removeEventListener('click', this.handleOutsideClick, false)
}
this.setState(nextState)
this.props.onChange(this.treeManager.getNodeById(id), tags)
}
onAction = (actionId, nodeId) => {
typeof this.props.onAction === 'function' && this.props.onAction(actionId, this.treeManager.getNodeById(nodeId))
}
onInputFocus = () => {
this.keepDropdownActive = true
}
onInputBlur = () => {
this.keepDropdownActive = false
}
render() {
const dropdownTriggerClassname = cx({
'dropdown-trigger': true,
arrow: true,
disabled: this.props.disabled,
readOnly: this.props.readOnly,
top: this.state.showDropdown,
bottom: !this.state.showDropdown
})
return (
<div
className={cx(this.props.className, 'react-dropdown-tree-select')}
ref={node => {
this.node = node
}}
>
<div className="dropdown">
<a className={dropdownTriggerClassname} onClick={!this.props.disabled && this.handleClick}>
<Input
inputRef={el => {
this.searchInput = el
}}
tags={this.state.tags}
placeholderText={this.props.placeholderText}
onInputChange={this.onInputChange}
onFocus={this.onInputFocus}
onBlur={this.onInputBlur}
onTagRemove={this.onTagRemove}
disabled={this.props.disabled}
readOnly={this.props.readOnly}
/>
</a>
{this.state.showDropdown && (
<div className={cx('dropdown-content')}>
{this.state.allNodesHidden ? (
<span className="no-matches">{this.props.noMatchesText || 'No matches found'}</span>
) : (
<Tree
data={this.state.tree}
keepTreeOnSearch={this.props.keepTreeOnSearch}
searchModeOn={this.state.searchModeOn}
onAction={this.onAction}
onCheckboxChange={this.onCheckboxChange}
onNodeToggle={this.onNodeToggle}
simpleSelect={this.props.simpleSelect}
showPartiallySelected={this.props.showPartiallySelected}
readOnly={this.props.readOnly}
/>
)}
</div>
)}
</div>
</div>
)
}
}
export default DropdownTreeSelect