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
175 lines (154 loc) · 4.06 KB
/
SearchInput.js
File metadata and controls
175 lines (154 loc) · 4.06 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
/* 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/>. */
// @flow
import React, { Component } from "react";
import Svg from "./Svg";
import classnames from "classnames";
import CloseButton from "./Button/Close";
import "./SearchInput.css";
const arrowBtn = (onClick, type, className, tooltip) => {
const props = {
className,
key: type,
onClick,
title: tooltip,
type
};
return (
<button {...props}>
<Svg name={type} />
</button>
);
};
type Props = {
count: number,
expanded: boolean,
handleClose: (e: SyntheticMouseEvent<HTMLDivElement>) => void,
handleNext?: (e: SyntheticMouseEvent<HTMLButtonElement>) => void,
handlePrev?: (e: SyntheticMouseEvent<HTMLButtonElement>) => void,
hasPrefix?: boolean,
onBlur?: (e: SyntheticFocusEvent<HTMLInputElement>) => void,
onChange: (e: SyntheticInputEvent<HTMLInputElement>) => void,
onFocus?: (e: SyntheticFocusEvent<HTMLInputElement>) => void,
onKeyDown: (e: SyntheticKeyboardEvent<HTMLInputElement>) => void,
onKeyUp?: (e: SyntheticKeyboardEvent<HTMLInputElement>) => void,
placeholder: string,
query: string,
selectedItemId?: string,
shouldFocus?: boolean,
showErrorEmoji: boolean,
size: string,
summaryMsg: string
};
class SearchInput extends Component<Props> {
displayName: "SearchInput";
$input: ?HTMLInputElement;
static defaultProps = {
expanded: false,
hasPrefix: false,
selectedItemId: "",
size: ""
};
componentDidMount() {
this.setFocus();
}
componentDidUpdate(prevProps: Props) {
if (this.props.shouldFocus && !prevProps.shouldFocus) {
this.setFocus();
}
}
setFocus() {
if (this.$input) {
const input = this.$input;
input.focus();
if (!input.value) {
return;
}
// omit prefix @:# from being selected
const selectStartPos = this.props.hasPrefix ? 1 : 0;
input.setSelectionRange(selectStartPos, input.value.length + 1);
}
}
renderSvg() {
const svgName = this.props.showErrorEmoji ? "sad-face" : "magnifying-glass";
return <Svg name={svgName} />;
}
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() {
const { count, handleNext, handlePrev } = this.props;
if ((!handleNext && !handlePrev) || (!count || count == 1)) {
return;
}
return (
<div className="search-nav-buttons">{this.renderArrowButtons()}</div>
);
}
render() {
const {
expanded,
handleClose,
onBlur,
onChange,
onFocus,
onKeyDown,
onKeyUp,
placeholder,
query,
selectedItemId,
showErrorEmoji,
size,
summaryMsg
} = this.props;
const inputProps = {
className: classnames({
empty: showErrorEmoji
}),
onChange,
onKeyDown,
onKeyUp,
onFocus,
onBlur,
"aria-autocomplete": "list",
"aria-controls": "result-list",
"aria-activedescendant":
expanded && selectedItemId ? `${selectedItemId}-title` : "",
placeholder,
value: query,
spellCheck: false,
ref: c => (this.$input = c)
};
return (
<div
className={classnames("search-field", size)}
role="combobox"
aria-haspopup="listbox"
aria-owns="result-list"
aria-expanded={expanded}
>
{this.renderSvg()}
<input {...inputProps} />
{summaryMsg && <div className="summary">{summaryMsg}</div>}
{this.renderNav()}
<CloseButton handleClick={handleClose} buttonClass={size} />
</div>
);
}
}
export default SearchInput;