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.

input_pill: Use e.key instead of deprecated e.keyCode.

Priyank Patele5005cf4ccba
diff --git a/frontend_tests/node_tests/input_pill.js b/frontend_tests/node_tests/input_pill.js
index ab05fa68969..58f2d87ea9e 100644
--- a/frontend_tests/node_tests/input_pill.js
+++ b/frontend_tests/node_tests/input_pill.js
@@ -237,13 +237,10 @@ run_test("arrows on pills", () => {
function test_key(c) {
key_handler({
- charCode: c,
+ key: c,
});
}
- const LEFT_ARROW = 37;
- const RIGHT_ARROW = 39;
-
let prev_focused = false;
let next_focused = false;
@@ -269,10 +266,10 @@ run_test("arrows on pills", () => {
// We use the same stub to test both arrows, since we don't
// actually cause any real state changes here. We stub out
// the only interaction, which is to move the focus.
- test_key(LEFT_ARROW);
+ test_key("ArrowLeft");
assert(prev_focused);
- test_key(RIGHT_ARROW);
+ test_key("ArrowRight");
assert(next_focused);
});
@@ -284,7 +281,6 @@ run_test("left arrow on input", () => {
const widget = input_pill.create(config);
widget.appendValue("blue,red");
- const LEFT_ARROW = 37;
const key_handler = container.get_on_handler("keydown", ".input");
let last_pill_focused = false;
@@ -300,7 +296,7 @@ run_test("left arrow on input", () => {
});
key_handler({
- keyCode: LEFT_ARROW,
+ key: "ArrowLeft",
});
assert(last_pill_focused);
@@ -318,13 +314,12 @@ run_test("comma", () => {
assert.deepEqual(widget.items(), [items.blue, items.red]);
- const COMMA = 188;
const key_handler = container.get_on_handler("keydown", ".input");
pill_input.text(" yel");
key_handler({
- keyCode: COMMA,
+ key: ",",
preventDefault: noop,
});
@@ -333,7 +328,7 @@ run_test("comma", () => {
pill_input.text(" yellow");
key_handler({
- keyCode: COMMA,
+ key: ",",
preventDefault: noop,
});
@@ -351,11 +346,10 @@ run_test("Enter key with text", () => {
assert.deepEqual(widget.items(), [items.blue, items.red]);
- const ENTER = 13;
const key_handler = container.get_on_handler("keydown", ".input");
key_handler({
- keyCode: ENTER,
+ key: "Enter",
preventDefault: noop,
stopPropagation: noop,
target: {
@@ -425,11 +419,10 @@ run_test("insert_remove", (override) => {
pill.$element.remove = set_colored_removed_func(pill.item.display_value);
}
- const BACKSPACE = 8;
let key_handler = container.get_on_handler("keydown", ".input");
key_handler({
- keyCode: BACKSPACE,
+ key: "Backspace",
target: {
textContent: "",
},
@@ -463,7 +456,7 @@ run_test("insert_remove", (override) => {
key_handler = container.get_on_handler("keydown", ".pill");
key_handler({
- keyCode: BACKSPACE,
+ key: "Backspace",
preventDefault: noop,
});
diff --git a/static/js/input_pill.js b/static/js/input_pill.js
index 2a6b6f374c5..66865e88691 100644
--- a/static/js/input_pill.js
+++ b/static/js/input_pill.js
@@ -11,16 +11,6 @@ export function random_id() {
}
export function create(opts) {
- // a dictionary of the key codes that are associated with each key
- // to make if/else more human readable.
- const KEY = {
- ENTER: 13,
- BACKSPACE: 8,
- LEFT_ARROW: 37,
- RIGHT_ARROW: 39,
- COMMA: 188,
- };
-
if (!opts.container) {
blueslip.error("Pill needs container.");
return undefined;
@@ -247,9 +237,7 @@ export function create(opts) {
{
store.$parent.on("keydown", ".input", (e) => {
- const char = e.keyCode || e.charCode;
-
- if (char === KEY.ENTER) {
+ if (e.key === "Enter") {
// regardless of the value of the input, the ENTER keyword
// should be ignored in favor of keeping content to one line
// always.
@@ -278,7 +266,7 @@ export function create(opts) {
// if the user backspaces and there is input, just do normal char
// deletion, otherwise delete the last pill in the sequence.
if (
- char === KEY.BACKSPACE &&
+ e.key === "Backspace" &&
(funcs.value(e.target).length === 0 || window.getSelection().anchorOffset === 0)
) {
e.preventDefault();
@@ -291,13 +279,13 @@ export function create(opts) {
// should switch to focus the last pill in the list.
// the rest of the events then will be taken care of in the function
// below that handles events on the ".pill" class.
- if (char === KEY.LEFT_ARROW && window.getSelection().anchorOffset === 0) {
+ if (e.key === "ArrowLeft" && window.getSelection().anchorOffset === 0) {
store.$parent.find(".pill").last().trigger("focus");
}
// Typing of the comma is prevented if the last field doesn't validate,
// as well as when the new pill is created.
- if (char === KEY.COMMA) {
+ if (e.key === ",") {
// if the pill is successful, it will create the pill and clear
// the input.
if (funcs.appendPill(store.$input.text().trim()) !== false) {
@@ -312,18 +300,16 @@ export function create(opts) {
// handle events while hovering on ".pill" elements.
// the three primary events are next, previous, and delete.
store.$parent.on("keydown", ".pill", (e) => {
- const char = e.keyCode || e.charCode;
-
const $pill = store.$parent.find(".pill:focus");
- switch (char) {
- case KEY.LEFT_ARROW:
+ switch (e.key) {
+ case "ArrowLeft":
$pill.prev().trigger("focus");
break;
- case KEY.RIGHT_ARROW:
+ case "ArrowRight":
$pill.next().trigger("focus");
break;
- case KEY.BACKSPACE: {
+ case "Backspace": {
const $next = $pill.next();
const id = $pill.data("id");
funcs.removePill(id);