forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInput.js
More file actions
160 lines (141 loc) · 3.41 KB
/
SearchInput.js
File metadata and controls
160 lines (141 loc) · 3.41 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
import React, { Component } from "react";
import { isEnabled } from "devtools-config";
import Svg from "./Svg";
import classnames from "classnames";
import CloseButton from "./Button/Close";
import "./SearchInput.css";
const arrowBtn = (onClick, type, className, tooltip) => {
const props = {
onClick,
type,
className,
title: tooltip,
key: type
};
return (
<button {...props}>
<Svg name={type} />
</button>
);
};
class SearchInput extends Component {
displayName: "SearchInput";
props: {
query: string,
count: number,
placeholder: string,
summaryMsg: string,
onChange: () => void,
handleClose: () => void,
showErrorEmoji: boolean,
onKeyUp: () => void,
onKeyDown: () => void,
onFocus: () => void,
onBlur: () => void,
size: string,
handleNext: () => void,
handlePrev: () => void
};
static defaultProps = {
size: "",
showErrorEmoji: true
};
componentDidMount() {
this.$input.focus();
if (this.$input.value != "") {
this.$input.setSelectionRange(
this.$input.value.length + 1,
this.$input.value.length + 1
);
}
}
componentDidUpdate() {
this.$input.focus();
if (this.$input.value != "") {
this.$input.setSelectionRange(
this.$input.value.length + 1,
this.$input.value.length + 1
);
}
}
shouldShowErrorEmoji() {
const { count, query, showErrorEmoji } = this.props;
return count === 0 && query.trim() !== "" && !showErrorEmoji;
}
renderSvg() {
if (this.shouldShowErrorEmoji()) {
return <Svg name="sad-face" />;
}
return <Svg name="magnifying-glass" />;
}
renderArrowButtons() {
const { handleNext, handlePrev } = this.props;
return [
arrowBtn(
handleNext,
"arrow-down",
classnames("nav-btn", "next"),
L10N.getFormatStr("editor.searchResults.nextResult")
),
arrowBtn(
handlePrev,
"arrow-up",
classnames("nav-btn", "prev"),
L10N.getFormatStr("editor.searchResults.prevResult")
)
];
}
renderNav() {
if (!isEnabled("searchNav")) {
return;
}
const { count, handleNext, handlePrev } = this.props;
if ((!handleNext && !handlePrev) || (!count || count == 1)) {
return;
}
return (
<div className="search-nav-buttons">{this.renderArrowButtons()}</div>
);
}
render() {
const {
query,
placeholder,
summaryMsg,
onChange,
onKeyDown,
onKeyUp,
onFocus,
onBlur,
handleClose,
size
} = this.props;
const inputProps = {
className: classnames({
empty: this.shouldShowErrorEmoji()
}),
onChange,
onKeyDown,
onKeyUp,
onFocus,
onBlur,
placeholder,
value: query,
spellCheck: false,
ref: c => (this.$input = c)
};
return (
<div className={classnames("search-field", size)}>
{this.renderSvg()}
<input {...inputProps} />
<div className="summary">{summaryMsg || ""}</div>
{this.renderNav()}
<CloseButton handleClick={handleClose} buttonClass={size} />
</div>
);
}
}
export default SearchInput;