-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchBox.jsx
126 lines (108 loc) · 3.24 KB
/
SearchBox.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import styles from './SearchBox.scss';
import classnames from 'classnames';
import Input from 'components/Input';
import SearchItem from 'components/SearchItem';
function SearchItemList({ items, selectedIndex }) {
return (
<div className={styles.SearchItemList}>
{items.map((item, index) => <SearchItem key={index} text={item.text} active={selectedIndex === index} />)}
</div>
);
}
class SearchBox extends React.Component {
constructor(props) {
super(props);
this.state = {
value: '',
items: [],
searchedItems: [],
selectedItem: {},
selectedIndex: 0
};
this.initItems = this.initItems.bind(this);
this.handleValueChange = this.handleValueChange.bind(this);
this.handleArrowPress = this.handleArrowPress.bind(this);
this.moveCursorToTop = this.moveCursorToTop.bind(this);
this.moveCursorToBottom = this.moveCursorToBottom.bind(this);
}
componentDidMount() {
this.initItems();
}
initItems() {
const items = [
...this.getItems({ type: 'build' }),
...this.getItems({ type: 'run' })
];
if (items.length > 0) {
this.setState({ items, searchedItems: items, selectedItem: items[0] });
}
}
getItems({ type }) {
let items = [];
document.querySelectorAll(`#toolbar-context__child-${type}-menu li:not(.divider)`).forEach((item, index, arr) => {
if (index !== 0 && index !== arr.length - 1) {
const text = item.querySelector('a').text;
items.push({ text, item });
}
});
return items;
}
handleValueChange(e) {
const value = e.target.value;
const searchedItems = this.state.items.filter(item => item.text.includes(value));
this.setState({
value: e.target.value,
searchedItems,
selectedItem: searchedItems[0],
selectedIndex: 0
});
}
handleArrowPress(e) {
const { keyCode } = e;
switch (keyCode) {
case 38:
this.moveCursorToTop();
break;
case 40:
this.moveCursorToBottom();
break;
case 13:
this.state.selectedItem.item.click();
this.props.itemClickHandler();
break;
}
}
moveCursorToTop() {
let nextIndex = this.state.selectedIndex - 1;
if (nextIndex < 0) {
nextIndex = this.state.searchedItems.length - 1;
}
this.setState({ selectedIndex: nextIndex, selectedItem: this.state.searchedItems[nextIndex] });
}
moveCursorToBottom() {
let nextIndex = this.state.selectedIndex + 1;
if (nextIndex >= this.state.searchedItems.length) {
nextIndex = 0;
}
this.setState({ selectedIndex: nextIndex, selectedItem: this.state.searchedItems[nextIndex] });
}
render() {
return (
<div className={classnames(styles.SearchBox, this.props.className)}>
<Input className={styles.SearchBox__input} onChange={this.handleValueChange} onKeyDown={this.handleArrowPress} autoFocus />
<SearchItemList items={this.state.searchedItems} selectedIndex={this.state.selectedIndex} />
</div>
);
}
}
SearchBox.propTypes = {
className: PropTypes.string,
itemClickHandler: PropTypes.func,
};
SearchBox.defaultProps = {
className: '',
itemClickHandler: v => v
};
export default SearchBox;