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.

components: Open tab when pressing "Enter" on a focussed tab.

Sahil Batraba9e461bf357
diff --git a/web/src/components.ts b/web/src/components.ts
index f9cd36f828a..a294379714d 100644
--- a/web/src/components.ts
+++ b/web/src/components.ts
@@ -1,7 +1,9 @@
import $ from "jquery";
+import assert from "minimalistic-assert";
import * as blueslip from "./blueslip.ts";
import * as keydown_util from "./keydown_util.ts";
+import * as ui_util from "./ui_util.ts";
/* USAGE:
Toggle x = components.toggle({
@@ -132,6 +134,11 @@ export function toggle(opts: {
handlers: {
ArrowLeft: maybe_go_left,
ArrowRight: maybe_go_right,
+ Enter(e?: JQuery.KeyDownEvent) {
+ assert(e !== undefined);
+ ui_util.convert_enter_to_click(e);
+ return true;
+ },
},
});
diff --git a/web/src/keydown_util.ts b/web/src/keydown_util.ts
index b053601071c..f3a2eb8d08f 100644
--- a/web/src/keydown_util.ts
+++ b/web/src/keydown_util.ts
@@ -9,7 +9,7 @@ export const vim_right = "l";
export function handle(opts: {
$elem: JQuery;
- handlers: Record<string, (() => boolean) | undefined>;
+ handlers: Record<string, ((e?: JQuery.KeyDownEvent) => boolean) | undefined>;
}): void {
opts.$elem.on("keydown", (e) => {
if (e.altKey || e.ctrlKey || e.shiftKey) {
@@ -22,7 +22,7 @@ export function handle(opts: {
return;
}
- const handled = handler();
+ const handled = handler(e);
if (handled) {
e.preventDefault();
e.stopPropagation();
diff --git a/web/tests/components.test.cjs b/web/tests/components.test.cjs
index 398e13afc17..70ef95939e4 100644
--- a/web/tests/components.test.cjs
+++ b/web/tests/components.test.cjs
@@ -3,10 +3,12 @@
const assert = require("node:assert/strict");
const {$t} = require("./lib/i18n.cjs");
-const {mock_jquery, zrequire} = require("./lib/namespace.cjs");
+const {mock_esm, mock_jquery, zrequire} = require("./lib/namespace.cjs");
const {run_test, noop} = require("./lib/test.cjs");
const blueslip = require("./lib/zblueslip.cjs");
+const ui_util = mock_esm("../src/ui_util");
+
let env;
function make_tab(i) {
@@ -53,6 +55,9 @@ function make_tab(i) {
if (type === "focus") {
env.focused_tab = i;
}
+ if (type === "click") {
+ env.click_f.call(env.tabs[i]);
+ }
};
env.tabs.push($self);
@@ -170,6 +175,7 @@ const components = zrequire("components");
const LEFT_KEY = {key: "ArrowLeft", preventDefault: noop, stopPropagation: noop};
const RIGHT_KEY = {key: "ArrowRight", preventDefault: noop, stopPropagation: noop};
+const ENTER_KEY = {key: "Enter", preventDefault: noop, stopPropagation: noop};
run_test("basics", () => {
env = {
@@ -271,6 +277,13 @@ run_test("basics", () => {
env.keydown_f.call(env.tabs[env.focused_tab], LEFT_KEY);
assert.equal(widget.value(), "translated: Keyboard shortcuts");
+ ui_util.convert_enter_to_click = () => {
+ env.tabs[2].trigger("click");
+ };
+ callback_args = undefined;
+ env.keydown_f.call(env.tabs[2], ENTER_KEY);
+ assert.equal(widget.value(), "translated: Search filters");
+
widget.enable_tab("message-formatting");
callback_args = undefined;