Skip to content

Commit

Permalink
Merge pull request #552 from alphagov/allow-autofocus-on-inputs
Browse files Browse the repository at this point in the history
Allow autofocus on inputs
  • Loading branch information
alex-ju authored Oct 1, 2018
2 parents 9309354 + bfc1986 commit e2a1e8f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* You can now use `GovukPublishingComponents::AppHelpers::Environment.current_acceptance_environment` to get the current environment, for use in the admin layout components (PR #548)
* Update GOV.UK Frontend from 1.2.0 to 2.1.0 and manage breaking changes (PR #545)
* Add navigation and meta links to footer component (PR #550)

* Allow autofocus on input component (PR #552)

## 11.0.0

Expand Down
10 changes: 6 additions & 4 deletions app/views/govuk_publishing_components/components/_input.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
label ||= nil
hint ||= nil
error_message ||= nil
autofocus ||= nil
tabindex ||= nil
hint_id = "hint-#{SecureRandom.hex(4)}" if hint
error_message_id = "error-message-#{SecureRandom.hex(4)}" if error_message

Expand Down Expand Up @@ -48,16 +50,16 @@
} %>
<% end %>

<%= text_field_tag name,
value,
{
<%= tag.input name: name,
value: value,
class: css_classes,
id: id,
type: type,
data: data,
tabindex: tabindex,
autofocus: autofocus,
aria: {
describedby: aria_described_by
}
}
%>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ accessibility_criteria: |
* be of the appropriate type for their use, e.g. password inputs should be of type 'password'
Labels use the [label component](/component-guide/label).
Avoid using autofocus and tabindex unless you have user research to support this behaviour.
govuk_frontend_components:
- input
examples:
Expand Down Expand Up @@ -56,3 +58,10 @@ examples:
text: "Search for"
name: "name"
value: "moose"
autofocused:
data:
label:
text: "Username"
name: "username"
autofocus: true
tabindex: 0
11 changes: 11 additions & 0 deletions spec/components/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def component_name
assert_select ".govuk-input[data-module='contextual-guidance']"
end

it "renders autofocused input" do
render_component(
label: { text: "Username" },
name: "username",
autofocus: true,
tabindex: 0
)

assert_select ".govuk-input[autofocus][tabindex='0']"
end

context "when a hint is provided" do
before do
render_component(
Expand Down

0 comments on commit e2a1e8f

Please sign in to comment.