-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathSearch.jsx
More file actions
106 lines (86 loc) · 2.19 KB
/
Search.jsx
File metadata and controls
106 lines (86 loc) · 2.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
// TODO: switch to a more modern debounce package once we drop Node.js 10 support
import debounce from "debounce";
import PropTypes from "prop-types";
import PureComponent from "../lib/PureComponent.jsx";
import Button from "./Button.jsx";
import * as styles from "./Search.css";
export default class Search extends PureComponent {
static propTypes = {
className: PropTypes.string,
label: PropTypes.string.isRequired,
query: PropTypes.string.isRequired,
autofocus: PropTypes.bool,
onQueryChange: PropTypes.func.isRequired,
};
componentDidMount() {
if (this.props.autofocus) {
this.focus();
}
}
componentWillUnmount() {
this.handleValueChange.clear();
}
render() {
const { label, query } = this.props;
return (
<div className={styles.container}>
<div className={styles.label}>{label}:</div>
<div className={styles.row}>
<input
ref={this.saveInputNode}
className={styles.input}
type="text"
value={query}
placeholder="Enter regexp"
onInput={this.handleValueChange}
onBlur={this.handleInputBlur}
onKeyDown={this.handleKeyDown}
/>
<Button className={styles.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.clear();
this.informChange("");
this.input.value = "";
}
informChange(value) {
this.props.onQueryChange(value);
}
saveInputNode = (node) => (this.input = node);
}