Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

search: Fix `hideAfterSelect` logic for search typeahead.

Pratik Chanda9bc1d8bdbff5
diff --git a/web/src/search.ts b/web/src/search.ts
index 45acecdbf92..4a8b10078df 100644
--- a/web/src/search.ts
+++ b/web/src/search.ts
@@ -229,9 +229,16 @@ export function initialize(opts: {on_narrow_search: OnNarrowSearch}): void {
return get_search_bar_text() !== "";
},
hideAfterSelect(): boolean {
- const search_bar_text = get_search_bar_text();
- const text_terms = Filter.parse(search_bar_text);
- return text_terms.at(-1)?.operator === "search";
+ const last_term = Filter.parse(get_search_bar_text()).at(-1);
+ // An empty search bar means the selected term was complete
+ // and has been converted to a pill, so hide the typeahead.
+ if (last_term === undefined) {
+ return true;
+ }
+ // Keep the typeahead open when only a bare operator was
+ // selected (e.g. "channel:") so the user can continue
+ // typing the operand.
+ return last_term.operand !== "";
},
matcher(_query: string) {
return () => true;
diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjs
index 2077e72838a..17913edeacf 100644
--- a/web/tests/search.test.cjs
+++ b/web/tests/search.test.cjs
@@ -215,6 +215,27 @@ run_test("initialize", ({override, override_rewire, mock_template}) => {
assert.equal(opts.updater(`channel:${verona_stream_id}`), "");
assert.ok(input_pill_displayed);
}
+
+ {
+ /* Test hideAfterSelect */
+ // An empty search bar means the selected term became a pill,
+ // so the typeahead should hide.
+ $search_query_box.text("");
+ assert.ok(opts.hideAfterSelect());
+
+ // A bare operator still needs an operand, so keep the
+ // typeahead open.
+ $search_query_box.text("channel:");
+ assert.ok(!opts.hideAfterSelect());
+
+ // A complete operator term is done, so hide the typeahead.
+ $search_query_box.text("channel:Verona");
+ assert.ok(opts.hideAfterSelect());
+
+ // A plain search term is also complete, so hide.
+ $search_query_box.text("ver");
+ assert.ok(opts.hideAfterSelect());
+ }
}
$search_query_box.text("test string");