The personal finance app for everyone (by everyone)
Ruby
fix(categories): keep focus in the category menu after a failed create (#4074)
scragnog571bb3b95ee2
diff --git a/app/components/DS/category_select.html.erb b/app/components/DS/category_select.html.erbindex b6be70933d..65cb0e996d 100644--- a/app/components/DS/category_select.html.erb+++ b/app/components/DS/category_select.html.erb@@ -136,7 +136,7 @@text: helpers.t("transactions.form.back_to_categories"),icon: "arrow-left",tone: :secondary,- data: { category_select_target: "parentBack", action: "click->category-select#hideParentPicker" }+ data: { action: "click->category-select#hideParentPicker" }) %><p id="<%= parent_label_id %>"class="px-3 py-1 text-xs text-secondary"diff --git a/app/javascript/controllers/category_select_controller.js b/app/javascript/controllers/category_select_controller.jsindex b3b82ea263..144a1a9569 100644--- a/app/javascript/controllers/category_select_controller.js+++ b/app/javascript/controllers/category_select_controller.js@@ -19,7 +19,6 @@ export default class extends Controller {"parentOption","parentOptionTemplate","parentRows",- "parentBack",];static values = {@@ -109,7 +108,6 @@ export default class extends Controller {if (event.key === "Enter" &&!this.createFormTarget.classList.contains("hidden") &&- !this.#parentPickerOpen() &&!this.creating) {event.preventDefault();@@ -233,6 +231,10 @@ export default class extends Controller {if (!category) {this.hideParentPicker();this.showCreateError(error);+ // The row that was clicked is disabled during the request, so focus+ // has fallen to the page, where Escape would close the surrounding+ // dialog. Put it back where the name can be fixed or retried.+ this.searchTarget.focus();return;}diff --git a/test/system/transaction_category_select_test.rb b/test/system/transaction_category_select_test.rbindex 6427361230..4ec6527391 100644--- a/test/system/transaction_category_select_test.rb+++ b/test/system/transaction_category_select_test.rb@@ -199,6 +199,23 @@ class TransactionCategorySelectTest < ApplicationSystemTestCaseassert_selector "[role='alert']", text: "taken", wait: 10assert_no_selector "[data-category-select-target='parentPicker']", visible: trueend+ assert_keyboard_back_in_search+ end++ test "a failed top-level create keeps focus in the menu" do+ visit new_transaction_url+ find("[data-controller='category-select'] button").click++ within "[data-controller='category-select']" do+ fill_in "Search categories", with: "Late Top Level"+ end+ Category.create!(family: users(:family_admin).family, name: "Late Top Level", color: "#e99537")++ within "[data-controller='category-select']" do+ find("[data-category-select-target='createForm']").click+ assert_selector "[role='alert']", text: "taken", wait: 10+ end+ assert_keyboard_back_in_searchendtest "a top-level category created inline is offered as a parent without reloading" do@@ -232,4 +249,13 @@ class TransactionCategorySelectTest < ApplicationSystemTestCasedef send_escapepage.driver.browser.action.send_keys(:escape).performend++ # Focus is back in the search, so Escape closes the menu, not the+ # New transaction dialog around it.+ def assert_keyboard_back_in_search+ assert_equal "search", active_element_attribute("data-category-select-target")+ send_escape+ assert_no_selector "[data-category-select-target='menu']", visible: true+ assert_selector "dialog[open]"+ endend