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.

typeahead: Add showOnFocus option.

Aman Agrawal828e2f38999c
diff --git a/web/src/bootstrap_typeahead.ts b/web/src/bootstrap_typeahead.ts
index 8b1229b8810..90b11e8cd91 100644
--- a/web/src/bootstrap_typeahead.ts
+++ b/web/src/bootstrap_typeahead.ts
@@ -259,6 +259,9 @@ export class Typeahead<ItemType extends string | object> {
// Used to determine whether the typeahead should be shown,
// when the user clicks anywhere on the input element.
showOnClick: boolean;
+ // Used to determine whether the typeahead should be shown,
+ // when the input element gains focus.
+ showOnFocus: () => boolean;
// Used for custom situations where we want to hide the typeahead
// after selecting an option, instead of the default call to lookup().
hideAfterSelect: () => boolean;
@@ -306,6 +309,7 @@ export class Typeahead<ItemType extends string | object> {
this.shouldHighlightFirstResult = options.shouldHighlightFirstResult ?? (() => true);
this.updateElementContent = options.updateElementContent ?? true;
this.showOnClick = options.showOnClick ?? true;
+ this.showOnFocus = options.showOnFocus ?? (() => false);
this.hideAfterSelect = options.hideAfterSelect ?? (() => true);
this.hideOnEmptyAfterBackspace = options.hideOnEmptyAfterBackspace ?? false;
this.getCustomItemClassname = options.getCustomItemClassname;
@@ -674,6 +678,7 @@ export class Typeahead<ItemType extends string | object> {
.on("keypress", this.keypress.bind(this))
.on("keyup", this.keyup.bind(this))
.on("click", this.element_click.bind(this))
+ .on("focus", this.element_focus.bind(this))
.on("keydown", this.keydown.bind(this))
.on("typeahead.refreshPosition", this.refreshPosition.bind(this));
@@ -694,7 +699,7 @@ export class Typeahead<ItemType extends string | object> {
unlisten(): void {
this.hide();
this.$container.remove();
- const events = ["blur", "keydown", "keyup", "keypress", "click"];
+ const events = ["blur", "keydown", "keyup", "keypress", "click", "focus"];
for (const event of events) {
$(this.input_element.$element).off(event);
}
@@ -897,6 +902,16 @@ export class Typeahead<ItemType extends string | object> {
this.lookup(false);
}
+ element_focus(): void {
+ // The focus event also fires when we programmatically refocus the
+ // input (e.g. from blur()), so bail if the menu is already shown to
+ // avoid a redundant lookup.
+ if (this.shown || !this.showOnFocus()) {
+ return;
+ }
+ this.lookup(false);
+ }
+
click(e: JQuery.ClickEvent): void {
e.stopPropagation();
e.preventDefault();
@@ -971,6 +986,7 @@ type TypeaheadOptions<ItemType> = {
shouldHighlightFirstResult?: () => boolean;
updateElementContent?: boolean;
showOnClick?: boolean;
+ showOnFocus?: () => boolean;
hideAfterSelect?: () => boolean;
getCustomItemClassname?: (item: ItemType) => string;
};