Skip to content

Commit 0b346b4

Browse files
darkwingjasonLaster
authored andcommitted
Ensure SearchInput retains value when open and clears when closed (firefox-devtools#5399)
1 parent 4fc2923 commit 0b346b4

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/components/Editor/SearchBar.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ type State = {
5151
query: string,
5252
selectedResultIndex: number,
5353
count: number,
54-
index: number
54+
index: number,
55+
inputFocused: boolean
5556
};
5657

5758
type Props = {
@@ -77,7 +78,8 @@ class SearchBar extends Component<Props, State> {
7778
query: props.query,
7879
selectedResultIndex: 0,
7980
count: 0,
80-
index: -1
81+
index: -1,
82+
inputFocused: false
8183
};
8284
}
8385

@@ -136,13 +138,13 @@ class SearchBar extends Component<Props, State> {
136138

137139
closeSearch = (e: SyntheticEvent<HTMLElement>) => {
138140
const { editor, searchOn } = this.props;
139-
140141
if (editor && searchOn) {
141142
this.clearSearch();
142143
this.props.closeFileSearch(editor);
143144
e.stopPropagation();
144145
e.preventDefault();
145146
}
147+
this.setState({ query: "", inputFocused: false });
146148
};
147149

148150
toggleSearch = (e: SyntheticKeyboardEvent<HTMLElement>) => {
@@ -155,10 +157,13 @@ class SearchBar extends Component<Props, State> {
155157
}
156158

157159
if (this.props.searchOn && editor) {
158-
const selection = editor.codeMirror.getSelection();
159-
this.setState({ query: selection });
160-
if (selection !== "") {
161-
this.doSearch(selection);
160+
const query = editor.codeMirror.getSelection() || this.state.query;
161+
162+
if (query !== "") {
163+
this.setState({ query, inputFocused: true });
164+
this.doSearch(query);
165+
} else {
166+
this.setState({ query: "", inputFocused: true });
162167
}
163168
}
164169
};
@@ -203,6 +208,10 @@ class SearchBar extends Component<Props, State> {
203208
return this.doSearch(e.target.value);
204209
};
205210

211+
onBlur = (e: SyntheticFocusEvent<HTMLElement>) => {
212+
this.setState({ inputFocused: false });
213+
};
214+
206215
onKeyDown = (e: SyntheticKeyboardEvent<HTMLElement>) => {
207216
if (e.key !== "Enter" && e.key !== "F3") {
208217
return;
@@ -299,11 +308,13 @@ class SearchBar extends Component<Props, State> {
299308
placeholder={L10N.getStr("sourceSearch.search.placeholder")}
300309
summaryMsg={this.buildSummaryMsg()}
301310
onChange={this.onChange}
311+
onBlur={this.onBlur}
302312
showErrorEmoji={this.shouldShowErrorEmoji()}
303313
onKeyDown={this.onKeyDown}
304314
handleNext={e => this.traverseResults(e, false)}
305315
handlePrev={e => this.traverseResults(e, true)}
306316
handleClose={this.closeSearch}
317+
shouldFocus={this.state.inputFocused}
307318
/>
308319
<div className="search-bottom-bar">{this.renderSearchModifiers()}</div>
309320
</div>

src/components/Editor/tests/__snapshots__/SearchBar.spec.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ exports[`SearchBar should render 1`] = `
1010
handleNext={[Function]}
1111
handlePrev={[Function]}
1212
hasPrefix={false}
13+
onBlur={[Function]}
1314
onChange={[Function]}
1415
onKeyDown={[Function]}
1516
placeholder="Search in file…"
1617
query=""
1718
selectedItemId=""
19+
shouldFocus={false}
1820
showErrorEmoji={false}
1921
size=""
2022
summaryMsg=""
@@ -66,11 +68,13 @@ exports[`showErrorEmoji false if no query + no results 1`] = `
6668
handleNext={[Function]}
6769
handlePrev={[Function]}
6870
hasPrefix={false}
71+
onBlur={[Function]}
6972
onChange={[Function]}
7073
onKeyDown={[Function]}
7174
placeholder="Search in file…"
7275
query=""
7376
selectedItemId=""
77+
shouldFocus={false}
7478
showErrorEmoji={false}
7579
size=""
7680
summaryMsg=""
@@ -120,11 +124,13 @@ exports[`showErrorEmoji false if query + results 1`] = `
120124
handleNext={[Function]}
121125
handlePrev={[Function]}
122126
hasPrefix={false}
127+
onBlur={[Function]}
123128
onChange={[Function]}
124129
onKeyDown={[Function]}
125130
placeholder="Search in file…"
126131
query="test"
127132
selectedItemId=""
133+
shouldFocus={false}
128134
showErrorEmoji={false}
129135
size=""
130136
summaryMsg="-NaN of 10 results"
@@ -174,11 +180,13 @@ exports[`showErrorEmoji true if query + no results 1`] = `
174180
handleNext={[Function]}
175181
handlePrev={[Function]}
176182
hasPrefix={false}
183+
onBlur={[Function]}
177184
onChange={[Function]}
178185
onKeyDown={[Function]}
179186
placeholder="Search in file…"
180187
query="test"
181188
selectedItemId=""
189+
shouldFocus={false}
182190
showErrorEmoji={true}
183191
size=""
184192
summaryMsg="No results"

src/components/shared/SearchInput.js

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const arrowBtn = (onClick, type, className, tooltip) => {
2929
type Props = {
3030
count: number,
3131
expanded: boolean,
32+
shouldFocus?: boolean,
3233
handleClose: (e: SyntheticMouseEvent<HTMLDivElement>) => void,
3334
handleNext?: (e: SyntheticMouseEvent<HTMLButtonElement>) => void,
3435
handlePrev?: (e: SyntheticMouseEvent<HTMLButtonElement>) => void,
@@ -57,6 +58,16 @@ class SearchInput extends Component<Props> {
5758
};
5859

5960
componentDidMount() {
61+
this.setFocus();
62+
}
63+
64+
componentDidUpdate(prevProps: Props) {
65+
if (this.props.shouldFocus && !prevProps.shouldFocus) {
66+
this.setFocus();
67+
}
68+
}
69+
70+
setFocus() {
6071
if (this.$input) {
6172
const input = this.$input;
6273
input.focus();

0 commit comments

Comments
 (0)