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.

MDLW: Render tippy tooltip once the selected items surpasses the limit.

aryanshridhar94b872828b6a
diff --git a/frontend_tests/node_tests/dropdown_list_widget.js b/frontend_tests/node_tests/dropdown_list_widget.js
index 0aa3ec698d7..a8d53d8bdb6 100644
--- a/frontend_tests/node_tests/dropdown_list_widget.js
+++ b/frontend_tests/node_tests/dropdown_list_widget.js
@@ -3,7 +3,7 @@
const {strict: assert} = require("assert");
const {$t} = require("../zjsunit/i18n");
-const {mock_esm, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, zrequire, set_global} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
@@ -13,6 +13,14 @@ mock_esm("../../static/js/list_widget", {
create: () => ({init: noop}),
});
+mock_esm("tippy.js", {
+ default: (arg) => {
+ arg._tippy = {setContent: noop, placement: noop, destroy: noop};
+ return arg._tippy;
+ },
+});
+
+set_global("document", {});
const {DropdownListWidget, MultiSelectDropdownListWidget} = zrequire("dropdown_list_widget");
// For DropdownListWidget
diff --git a/static/js/dropdown_list_widget.js b/static/js/dropdown_list_widget.js
index 3772fea9ca9..36accc0179e 100644
--- a/static/js/dropdown_list_widget.js
+++ b/static/js/dropdown_list_widget.js
@@ -1,5 +1,6 @@
import $ from "jquery";
import _ from "lodash";
+import tippy from "tippy.js";
import render_dropdown_list from "../templates/settings/dropdown_list.hbs";
@@ -303,6 +304,9 @@ MultiSelectDropdownListWidget.prototype.render_button_text = function (elem, lim
const items_selected = this.data_selected.length;
let text = "";
+ // Destroy the tooltip once the button text reloads.
+ this.destroy_tooltip();
+
if (items_selected === 0) {
this.render_default_text(elem);
return;
@@ -311,6 +315,7 @@ MultiSelectDropdownListWidget.prototype.render_button_text = function (elem, lim
text = data_selected.map((data) => data.name).toString();
} else {
text = $t({defaultMessage: "{items_selected} selected"}, {items_selected});
+ this.render_tooltip();
}
elem.text(text);
@@ -412,6 +417,27 @@ MultiSelectDropdownListWidget.prototype.remove_check_mark = function (element) {
}
};
+// Render the tooltip once the text changes to `n` selected.
+MultiSelectDropdownListWidget.prototype.render_tooltip = function () {
+ const elem = $(`#${CSS.escape(this.container_id)}`);
+ const selected_items = this.data.filter((item) => this.checked_items.includes(item.value));
+
+ tippy(elem[0], {
+ content: selected_items.map((item) => item.name).join(", "),
+ placement: "top",
+ });
+};
+
+MultiSelectDropdownListWidget.prototype.destroy_tooltip = function () {
+ const elem = $(`#${CSS.escape(this.container_id)}`);
+ const tippy_instance = elem[0]._tippy;
+ if (!tippy_instance) {
+ return;
+ }
+
+ tippy_instance.destroy();
+};
+
MultiSelectDropdownListWidget.prototype.dropdown_focus_events = function () {
// Main keydown event handler which transfers the focus from one child element
// to another.