From a218143db7ff52698e417ddec9c2113331acde9a Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Mon, 31 May 2021 16:38:57 +0000 Subject: [PATCH] search: Use e.key instead of deprecated e.which. Tested by making sure Enter works and expected in the navbar search with and without the typeahead being present. --- frontend_tests/node_tests/search.js | 12 ++++++------ frontend_tests/node_tests/search_legacy.js | 12 ++++++------ static/js/search.js | 7 +++---- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/frontend_tests/node_tests/search.js b/frontend_tests/node_tests/search.js index ef317854c3..ba143cb602 100644 --- a/frontend_tests/node_tests/search.js +++ b/frontend_tests/node_tests/search.js @@ -228,7 +228,7 @@ test("initialize", () => { let default_prevented = false; let ev = { type: "keydown", - which: 15, + key: "a", preventDefault() { default_prevented = true; }, @@ -237,11 +237,11 @@ test("initialize", () => { assert.equal(keydown(ev), undefined); assert(!default_prevented); - ev.which = 13; + ev.key = "Enter"; assert.equal(keydown(ev), undefined); assert(!default_prevented); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; assert.equal(keydown(ev), undefined); assert(default_prevented); @@ -288,14 +288,14 @@ test("initialize", () => { assert(!is_blurred); assert(!search_button.prop("disabled")); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => false; searchbox_form.trigger(ev); assert(!is_blurred); assert(!search_button.prop("disabled")); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; searchbox_form.trigger(ev); assert(is_blurred); @@ -308,7 +308,7 @@ test("initialize", () => { assert(!search_button.prop("disabled")); _setup("ver"); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; searchbox_form.trigger(ev); assert(is_blurred); diff --git a/frontend_tests/node_tests/search_legacy.js b/frontend_tests/node_tests/search_legacy.js index 6f8a145779..c64b07af32 100644 --- a/frontend_tests/node_tests/search_legacy.js +++ b/frontend_tests/node_tests/search_legacy.js @@ -192,11 +192,11 @@ run_test("initialize", () => { assert.equal(keydown(ev), undefined); assert(!default_prevented); - ev.which = 13; + ev.key = "Enter"; assert.equal(keydown(ev), undefined); assert(!default_prevented); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; assert.equal(keydown(ev), undefined); assert(default_prevented); @@ -235,21 +235,21 @@ run_test("initialize", () => { ]; _setup(""); - ev.which = 15; + ev.key = "a"; search_query_box.is = () => false; searchbox_form.trigger(ev); assert(!is_blurred); assert(!search_button.prop("disabled")); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => false; searchbox_form.trigger(ev); assert(!is_blurred); assert(!search_button.prop("disabled")); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; searchbox_form.trigger(ev); assert(is_blurred); @@ -262,7 +262,7 @@ run_test("initialize", () => { assert(!search_button.prop("disabled")); _setup("ver"); - ev.which = 13; + ev.key = "Enter"; search_query_box.is = () => true; searchbox_form.trigger(ev); assert(is_blurred); diff --git a/static/js/search.js b/static/js/search.js index 6121ff9dbc..11457bc377 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -134,8 +134,7 @@ export function initialize() { searchbox_form .on("keydown", (e) => { update_button_visibility(); - const code = e.which; - if (code === 13 && search_query_box.is(":focus")) { + if (e.key === "Enter" && search_query_box.is(":focus")) { // Don't submit the form so that the typeahead can instead // handle our Enter keypress. Any searching that needs // to be done will be handled in the keyup. @@ -147,8 +146,8 @@ export function initialize() { is_using_input_method = false; return; } - const code = e.which; - if (code === 13 && search_query_box.is(":focus")) { + + if (e.key === "Enter" && search_query_box.is(":focus")) { // We just pressed Enter and the box had focus, which // means we didn't use the typeahead at all. In that // case, we should act as though we're searching by