Skip to content

Commit

Permalink
self employed keydown removal (#4839)
Browse files Browse the repository at this point in the history
* use data attr for handler

* refactor enter keydown listener to leverage event propagation to better handle AJAX elements

---------

Co-authored-by: Ann Caballero <[email protected]>
  • Loading branch information
charlienparker and anncaballeroo authored Dec 17, 2024
1 parent b3e4c1d commit 6e62d9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions app/assets/javascripts/keyboard_navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ function handleContactInfoKeyDown(event, radioId, modifyDiv) {
}

document.addEventListener('turbolinks:load', () => {
/**
* Selects all elements in the document that have the attribute 'data-keydown-id'.
*/
const elements = document.querySelectorAll('[data-keydown-id]');

elements.forEach((element) => {
const keydownId = element.dataset.keydownId;

element.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
clickElementById(keydownId);
}
});
// Use event propogation to handle `Enter` keydown events for HTML elements with the attribute `data-keydown-id`.
document.addEventListener('keydown', (event) => {
/*
* Trigger the click on the proxy element identified by the attribute.
*/
const keydownId = event.target?.dataset?.keydownId;
if (event.key === 'Enter' && keydownId) {
clickElementById(keydownId);
}
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="row mb-2">
<div class="pull-right mt-1 mb-2 pl-3">
<a tabindex="0" onkeydown="handleButtonKeyDown(event, 'self-employed-income-cancel-<%= income_id %>')" id="self-employed-income-cancel-<%= income_id %>" class="button btn-default outline income-cancel"><%= l10n("cancel") %></a>
<a tabindex="0" data-keydown-id='self-employed-income-cancel-<%= income_id %>' id="self-employed-income-cancel-<%= income_id %>" class="button btn-default outline income-cancel"><%= l10n("cancel") %></a>
<%= f.submit l10n("faa.other_incomes.save"), class: "button btn-save ml-1", :data => { :cuke => "self-employed-income-save-button" }, onclick: "checkDate('#{income_id}')" %>
</div>
</div>
Expand Down

0 comments on commit 6e62d9a

Please sign in to comment.