Merge pull request #2880 from alphagov/add-logic-to-autofocus

Prevent autofocus when page has scrolled
This commit is contained in:
Tom Byers
2019-04-03 15:37:25 +01:00
committed by GitHub
2 changed files with 7 additions and 1 deletions

View File

@@ -3,6 +3,11 @@
Modules.Autofocus = function() { Modules.Autofocus = function() {
this.start = function(component) { this.start = function(component) {
var 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; }
$('input, textarea, select', component).eq(0).trigger('focus'); $('input, textarea, select', component).eq(0).trigger('focus');

View File

@@ -16,7 +16,8 @@
{% call form_wrapper( {% call form_wrapper(
class='js-stick-at-top-when-scrolling send-one-off-form' if template.template_type != 'sms' else 'send-one-off-form', class='js-stick-at-top-when-scrolling send-one-off-form' if template.template_type != 'sms' else 'send-one-off-form',
module="autofocus" module="autofocus",
data_kwargs={'force-focus': True}
) %} ) %}
<div class="grid-row"> <div class="grid-row">
<div class="column-two-thirds {% if form.placeholder_value.label.text == 'phone number' %}extra-tracking{% endif %}"> <div class="column-two-thirds {% if form.placeholder_value.label.text == 'phone number' %}extra-tracking{% endif %}">