Merge pull request #2522 from GSA/2515-remove-autofocus-js

2515 - Remove autofocus.js
This commit is contained in:
Jonathan Bobel
2025-05-01 10:02:26 -04:00
committed by GitHub
9 changed files with 20 additions and 134 deletions

View File

@@ -1,27 +0,0 @@
(function(Modules) {
"use strict";
Modules.Autofocus = function() {
this.start = function(component) {
var $component = $(component),
forceFocus = $component.data('forceFocus');
// if the page loads with a scroll position, we can't assume the item to focus onload
// is still where users intend to start
if (($(window).scrollTop() > 0) && !forceFocus) { return; }
// See if the component itself is something we want to send focus to
var target = $component.filter('input, textarea, select');
// Otherwise look inside the component to see if there are any elements
// we want to send focus to
if (target.length === 0) {
target = $('input, textarea, select', $component);
}
target.eq(0).trigger('focus');
};
};
})(window.GOVUK.Modules);

View File

@@ -345,8 +345,13 @@ h2.recipient-list {
}
}
// Button ellipses loading
.usa-search__input, [type=search] {
border-right: 1px solid;
height: 40px;
margin-top: units(1);
}
// Button ellipses loading
.dot-anim::after {
content: '.';
animation: dotPulse 1.5s steps(3, end) infinite;

View File

@@ -3,7 +3,7 @@
show=False,
form=None,
label=None,
autofocus=False
autofocus=True
) %}
{%- set search_label = label or form.search.label.text %}
@@ -13,7 +13,7 @@
} %}
{% if autofocus %}
{% set x=param_extensions.__setitem__("attributes", {"data-module": "autofocus"}) %}
{% set _ = param_extensions.__setitem__("attributes", {"autofocus": "autofocus"}) %}
{% endif %}
{% if show %}

View File

@@ -30,13 +30,13 @@
</div>
{% if show_search_box %}
<div data-module="autofocus">
<div>
{{ live_search(target_selector='.user-list-item', show=True, form=form) }}
</div>
{% endif %}
<div class="user-list">
{% for user in users %}
<div class="user-list">
{% if user.status != 'cancelled' %}
<div class="user-list-item">
<h2 class="user-list-item-heading font-body-lg margin-top-0" title="{{ user.email_address }}">
@@ -101,7 +101,7 @@
{% endif %}
</div>
{% endif %}
{% endfor %}
</div>
{% endfor %}
{% endblock %}

View File

@@ -24,13 +24,13 @@
</h1>
{% if show_search_box %}
<div data-module="autofocus">
<div>
{{ live_search(target_selector='.user-list-item', show=True, form=form) }}
</div>
{% endif %}
<div class="user-list">
{% for user in users %}
<div class="user-list">
<div class="user-list-item">
<div class="grid-row">
<div class="grid-col-9">
@@ -60,8 +60,8 @@
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
<div class="js-stick-at-bottom-when-scrolling">
{{ usaButton({

View File

@@ -59,7 +59,6 @@ const javascripts = () => {
paths.toolkit + 'javascripts/govuk/modules.js',
paths.toolkit + 'javascripts/govuk/show-hide-content.js',
paths.src + 'javascripts/copyToClipboard.js',
paths.src + 'javascripts/autofocus.js',
paths.src + 'javascripts/enhancedTextbox.js',
paths.src + 'javascripts/fileUpload.js',
paths.src + 'javascripts/radioSelect.js',

View File

@@ -815,8 +815,9 @@ def test_manage_org_users_should_show_live_search_if_more_than_7_users(
)
assert len(page.select(".user-list-item")) == number_of_users
textbox = page.select_one("[data-module=autofocus] .usa-input")
assert "value" not in textbox
textbox = page.select_one(".usa-input")
assert textbox is not None
assert textbox.get("value") is None
assert textbox["name"] == "search"
# data-module=autofocus is set on a containing element so it
# shouldnt also be set on the textbox itself

View File

@@ -186,7 +186,7 @@ def test_should_show_live_search_if_more_than_7_users(
)
assert len(page.select(".user-list-item")) == number_of_users
textbox = page.select_one("[data-module=autofocus] .usa-input")
textbox = page.select_one(".usa-input")
assert "value" not in textbox
assert textbox["name"] == "search"
# data-module=autofocus is set on a containing element so it

View File

@@ -1,92 +0,0 @@
const helpers = require('./support/helpers.js');
beforeAll(() => {
require('../../app/assets/javascripts/autofocus.js');
});
afterAll(() => {
require('./support/teardown.js');
});
describe('Autofocus', () => {
const labelText = 'Search by name';
let focusHandler;
let search;
beforeEach(() => {
document.title = 'Find services by name - GOV.UK Notify';
// set up DOM
document.body.innerHTML =
`<div id="wrapper">
<label class="form-label" for="search">
${labelText}
</label>
<input autocomplete="off" class="form-control form-control-1-1" id="search" name="search" type="search" value="" data-module="autofocus">
</div>`;
focusHandler = jest.fn();
search = document.getElementById('search');
search.addEventListener('focus', focusHandler, false);
});
afterEach(() => {
document.body.innerHTML = '';
search.removeEventListener('focus', focusHandler);
focusHandler = null;
});
test('is focused when modules start', () => {
// start module
window.GOVUK.modules.start();
expect(focusHandler).toHaveBeenCalled();
});
test('is focused when attribute is set on outer element', () => {
document.getElementById('search').removeAttribute('data-module');
document.getElementById('wrapper').setAttribute('data-module', 'autofocus');
// start module
window.GOVUK.modules.start();
expect(focusHandler).toHaveBeenCalled();
});
test('is not focused if the window has scrolled', () => {
// mock the window being scrolled 25px
$.prototype.scrollTop = jest.fn(() => 25);
// start module
window.GOVUK.modules.start();
expect(focusHandler).not.toHaveBeenCalled();
});
test('is focused if the window has scrolled but the force-focus flag is set', () => {
// mock the window being scrolled 25px
$.prototype.scrollTop = jest.fn(() => 25);
// set the force-focus flag
document.querySelector('#search').setAttribute('data-force-focus', true);
// start module
window.GOVUK.modules.start();
expect(focusHandler).toHaveBeenCalled();
});
});