-
-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathSearch.jsx
92 lines (76 loc) · 1.87 KB
/
Search.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
// TODO: switch to a more modern debounce package once we drop Node.js 10 support
import debounce from 'lodash.debounce';
import s from './Search.css';
import Button from './Button';
import PureComponent from '../lib/PureComponent';
export default class Search extends PureComponent {
componentDidMount() {
if (this.props.autofocus) {
this.focus();
}
}
componentWillUnmount() {
this.handleValueChange.cancel();
}
render() {
const {label, query} = this.props;
return (
<div className={s.container}>
<div className={s.label}>
{label}:
</div>
<div className={s.row}>
<input ref={this.saveInputNode}
className={s.input}
type="text"
value={query}
placeholder="Enter regexp"
onInput={this.handleValueChange}
onBlur={this.handleInputBlur}
onKeyDown={this.handleKeyDown}/>
<Button className={s.clear} onClick={this.handleClearClick}>x</Button>
</div>
</div>
);
}
handleValueChange = debounce((event) => {
this.informChange(event.target.value);
}, 400)
handleInputBlur = () => {
this.handleValueChange.flush();
}
handleClearClick = () => {
this.clear();
this.focus();
}
handleKeyDown = event => {
let handled = true;
switch (event.key) {
case 'Escape':
this.clear();
break;
case 'Enter':
this.handleValueChange.flush();
break;
default:
handled = false;
}
if (handled) {
event.stopPropagation();
}
}
focus() {
if (this.input) {
this.input.focus();
}
}
clear() {
this.handleValueChange.cancel();
this.informChange('');
this.input.value = '';
}
informChange(value) {
this.props.onQueryChange(value);
}
saveInputNode = node => this.input = node;
}