Skip to content

Commit

Permalink
Add new 'include_blank' option for autocomplete module
Browse files Browse the repository at this point in the history
It's a common use case to show a blank option on the select
dropdown, and it feels wrong that in each case where that's
required, we have to prefix options with `[""]`. It should just
be a natively supported feature (allowing us to change the
underlying implementation much more easily).

This commit carries over the `include_blank` option from the
'select_with_search' component so that we have feature parity
between the two components.

It also uses the new option on existing 'autocomplete'
instances.
  • Loading branch information
ChrisBAshton committed Feb 18, 2025
1 parent 534daa7 commit 2140ccb
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/views/admin/detailed_guides/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
id: "edition_related_detailed_guide_ids",
name: "edition[related_detailed_guide_ids][]",
error_items: errors_for(edition.errors, :related_detailed_guide_ids),
include_blank: true,
label: {
text: "Related guides",
heading_size: "m",
},
select: {
options: [""] + taggable_detailed_guides_container,
options: taggable_detailed_guides_container,
multiple: true,
selected: edition.related_detailed_guide_ids,
},
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/editions/_appointment_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<%= render "components/autocomplete", {
id: "edition_role_appointment_ids",
name: "edition[role_appointment_ids][]",
include_blank: true,
label: {
text: "Ministers",
heading_size: "m",
},
select: {
options: [""] + taggable_ministerial_role_appointments_container,
options: taggable_ministerial_role_appointments_container,
multiple: true,
selected: edition.role_appointment_ids,
},
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/editions/_organisation_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
<%= render "components/autocomplete", {
id: "edition_supporting_organisation_ids",
name: "edition[supporting_organisation_ids][]",
include_blank: true,
label: {
text: "Supporting organisations",
heading_size: "m",
},
select: {
multiple: true,
selected: edition.edition_organisations.reject(&:lead?).map(&:organisation_id),
options: [""] + taggable_organisations_container,
options: taggable_organisations_container,
},
} %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<%= render "components/autocomplete", {
id: "edition_statistical_data_set_document_ids",
name: "edition[statistical_data_set_document_ids][]",
include_blank: true,
label: {
text: "Statistical data sets",
heading_size: "m",
},
select: {
options: [""] + taggable_statistical_data_sets_container,
options: taggable_statistical_data_sets_container,
multiple: true,
selected: edition.statistical_data_set_document_ids,
},
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/editions/_topical_event_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<%= render "components/autocomplete", {
id: "edition_topical_event_ids",
name: "edition[topical_event_ids][]",
include_blank: true,
label: {
text: "Topical events",
heading_size: "m",
},
select: {
options: [""] + TopicalEvent.order(:name).map { |topical_event| [topical_event.name, topical_event.id]},
options: TopicalEvent.order(:name).map { |topical_event| [topical_event.name, topical_event.id]},
multiple: true,
selected: edition.topical_event_ids,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
id: "edition_worldwide_organisation_document_ids",
name: "edition[worldwide_organisation_document_ids][]",
error_items: errors_for(edition.errors, :worldwide_organisations),
include_blank: true,
label: {
text: "Worldwide organisations" + "#{' (required)' if required}",
heading_size: "m",
},
select: {
options: [""] + taggable_worldwide_organisations_container,
options: taggable_worldwide_organisations_container,
multiple: true,
selected: edition.worldwide_organisation_document_ids,
},
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/needs/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<%= render "components/autocomplete", {
id: "need_ids",
name: "need_ids[]",
include_blank: true,
label: {
text: "Select associated user needs",
bold: true,
required: true,
},
heading_size: "l",
select: {
options: [""] + taggable_needs_container,
options: taggable_needs_container,
multiple: true,
selected: @document.need_ids,
},
Expand Down
14 changes: 7 additions & 7 deletions app/views/admin/organisations/_closed_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@
<%= render "components/autocomplete", {
id: "organisation_superseding_organisation_ids",
name: "organisation[superseding_organisation_ids][]",
include_blank: true,
label: {
text: "Superseding organisations",
heading_size: "m",
},
select: {
options: [""] +
(Organisation.with_translations(:en) - [organisation]).map do |org|
[
org.name,
org.id,
]
end,
options: (Organisation.with_translations(:en) - [organisation]).map do |org|
[
org.name,
org.id,
]
end,
multiple: true,
selected: organisation.superseding_organisation_ids,
},
Expand Down
13 changes: 6 additions & 7 deletions app/views/admin/organisations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,12 @@
heading_size: "m",
},
select: {
options: [""] +
(Organisation.with_translations(:en) - [organisation]).map do |org|
[
org.name,
org.id,
]
end,
options: (Organisation.with_translations(:en) - [organisation]).map do |org|
[
org.name,
org.id,
]
end,
multiple: true,
selected: organisation.parent_organisation_ids,
},
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<%= render "components/autocomplete", {
id: "user_world_location_ids",
name: "user[world_location_ids][]",
include_blank: true,
label: {
text: "Locations",
heading_size: "l",
},
select: {
multiple: true,
selected: form.object.world_location_ids,
options: [""] + WorldLocation.all.map { |l| [l.name, l.id] },
options: WorldLocation.all.map { |l| [l.name, l.id] },
},
} %>
<div class="govuk-button-group">
Expand Down
6 changes: 5 additions & 1 deletion app/views/components/_autocomplete.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
error_items ||= []
aria = error_id if error_items.any?
select ||= {}
include_blank ||= false
autocomplete_configuration_options ||= {
showAllValues: true,
}
Expand Down Expand Up @@ -38,7 +39,10 @@
<% end %>

<%= select_tag name,
options_for_select(select[:options], select[:selected]),
options_for_select(
include_blank ? ([""] + select[:options]) : select[:options],
select[:selected],
),
id: id,
class: "govuk-select",
size: select[:size],
Expand Down
21 changes: 18 additions & 3 deletions app/views/components/docs/autocomplete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ examples:
select:
options:
-
-
- France
- fr
-
- Germany
- de
-
- United Kingdom
- uk

with_blank_option:
data:
id: autocomplete
name: autocomplete
include_blank: true
label:
text: Select your country
select:
options:
-
- France
- fr
Expand Down Expand Up @@ -101,8 +118,6 @@ examples:
text: Select your country
select:
options:
-
-
-
- France
- fr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
id: "#{parent_class}_lead_organisation",
name: "content_block/edition[organisation_id]",
error_items: errors_for(@form.content_block_edition.errors, "lead_organisation".to_sym),
include_blank: true,
label: {
text: "Lead organisation",
},
select: {
multiple: false,
options: [""] + taggable_organisations_container,
options: taggable_organisations_container,
selected: @form.content_block_edition.edition_organisation&.organisation_id,
},
} %>
Expand Down
8 changes: 7 additions & 1 deletion test/components/autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def component_data
},
select: {
options: [
[""],
["France", "fr"],
["Germany", "de"],
["United Kingdom", "uk"],
Expand Down Expand Up @@ -43,6 +42,13 @@ def component_data
assert_select ".app-c-autocomplete .govuk-select option[value='de'][selected='selected']"
end

test "renders with a blank option" do
data = component_data
data[:include_blank] = true
render_component(data)
assert_select ".app-c-autocomplete .govuk-select option[value='']"
end

test "renders with an error" do
data = component_data
data[:error_items] = [
Expand Down

0 comments on commit 2140ccb

Please sign in to comment.