Skip to content

Commit 509fa46

Browse files
committed
Allow items to override checked state from values
If a checkbox, radio or select option has the checked or selected option set, always use its value to set the checked or selected state, even when it’s falsey. As suggested by @edwardhorsford [1]: > This is helpful when most of the items just need to check if the stored value equals the value, but one or two need more complex logic to determine if they should be selected. [1]: #2616 (review)
1 parent 89f8d3c commit 509fa46

File tree

9 files changed

+69
-6
lines changed

9 files changed

+69
-6
lines changed

src/govuk/components/checkboxes/checkboxes.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ params:
7777
- name: checked
7878
type: boolean
7979
required: false
80-
description: If `true`, the checkbox will be checked when the page loads.
80+
description: Whether the checkbox should be checked when the page loads. Takes precedence over the top-level `values` option.
8181
- name: conditional
8282
type: boolean
8383
required: false
@@ -899,3 +899,17 @@ examples:
899899
text: British
900900
- value: irish
901901
text: Irish
902+
903+
- name: item checked overrides values
904+
hidden: true
905+
data:
906+
name: colors
907+
items:
908+
- value: red
909+
text: Red
910+
- value: green
911+
text: Green
912+
checked: false
913+
- value: blue
914+
text: Blue
915+
values: [red, green]

src/govuk/components/checkboxes/template.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
{%- if item.divider %}
6464
<div class="govuk-checkboxes__divider">{{ item.divider }}</div>
6565
{%- else %}
66-
{% set isChecked = item.checked or (params.values and item.value in params.values) %}
66+
{% set isChecked = item.checked | default(params.values and item.value in params.values) %}
6767
{% set hasHint = true if item.hint.text or item.hint.html %}
6868
{% set itemHintId = id + "-item-hint" if hasHint else "" %}
6969
{% set itemDescribedBy = describedBy if not hasFieldset else "" %}

src/govuk/components/checkboxes/template.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ describe('Checkboxes', () => {
194194
expect($other.attr('checked')).toEqual('checked')
195195
})
196196

197+
it('allows item.checked to override values', () => {
198+
const $ = render('checkboxes', examples['item checked overrides values'])
199+
200+
const $green = $('.govuk-checkboxes').find('input[value="green"]')
201+
expect($green.attr('checked')).toBeUndefined()
202+
})
203+
197204
describe('when they include attributes', () => {
198205
it('it renders the attributes', () => {
199206
const $ = render('checkboxes', examples['items with attributes'])

src/govuk/components/radios/radios.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ params:
6969
- name: checked
7070
type: boolean
7171
required: false
72-
description: If `true`, the radio will be checked when the page loads.
72+
description: Whether the radio should be checked when the page loads. Takes precedence over the top-level `value` option.
7373
- name: conditional
7474
type: string
7575
required: false
@@ -889,3 +889,17 @@ examples:
889889
- value: no
890890
text: No
891891
checked: true
892+
893+
- name: item checked overrides value
894+
hidden: true
895+
data:
896+
name: colors
897+
items:
898+
- value: red
899+
text: Red
900+
- value: green
901+
text: Green
902+
checked: false
903+
- value: blue
904+
text: Blue
905+
value: green

src/govuk/components/radios/template.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
{%- if item.divider %}
5757
<div class="govuk-radios__divider">{{ item.divider }}</div>
5858
{%- else %}
59-
{% set isChecked = item.checked or (params.value and item.value == params.value) %}
59+
{% set isChecked = item.checked | default(params.value and item.value == params.value) %}
6060
{% set hasHint = true if item.hint.text or item.hint.html %}
6161
{% set itemHintId = id + '-item-hint' %}
6262
<div class="govuk-radios__item">

src/govuk/components/radios/template.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ describe('Radios', () => {
137137
expect($lastInput.attr('checked')).toEqual('checked')
138138
})
139139

140+
it('allows item.checked to override value', () => {
141+
const $ = render('radios', examples['item checked overrides value'])
142+
143+
const $green = $('.govuk-radios').find('input[value="green"]')
144+
expect($green.attr('checked')).toBeUndefined()
145+
})
146+
140147
describe('when they include attributes', () => {
141148
it('it renders the attributes', () => {
142149
const $ = render('radios', examples['items with attributes'])

src/govuk/components/select/select.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ params:
2323
- name: selected
2424
type: boolean
2525
required: false
26-
description: Sets the option as selected when the page loads.
26+
description: Whether the option should be selected when the page loads. Takes precedence over the top-level `value` option.
2727
- name: disabled
2828
type: boolean
2929
required: false
@@ -304,3 +304,17 @@ examples:
304304
-
305305
value: 2
306306
text: GOV.UK frontend option 2
307+
308+
- name: item selected overrides value
309+
hidden: true
310+
data:
311+
name: colors
312+
items:
313+
- value: red
314+
text: Red
315+
- value: green
316+
text: Green
317+
selected: false
318+
- value: blue
319+
text: Blue
320+
value: green

src/govuk/components/select/template.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{% for item in params.items %}
4343
{% if item %}
4444
<option value="{{ item.value }}"
45-
{{-" selected" if item.selected or (params.value and item.value == params.value) }}
45+
{{-" selected" if item.selected | default(params.value and item.value == params.value) }}
4646
{{-" disabled" if item.disabled }}
4747
{%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>{{ item.text }}</option>
4848
{% endif %}

src/govuk/components/select/template.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ describe('Select', () => {
6969
expect($selectedItem.attr('selected')).toBeTruthy()
7070
})
7171

72+
it('allows item.selected to override value', () => {
73+
const $ = render('select', examples['item selected overrides value'])
74+
75+
const $selectedItem = $('option[value="green"]')
76+
expect($selectedItem.attr('selected')).toBeUndefined()
77+
})
78+
7279
it('renders item with disabled', () => {
7380
const $ = render('select', examples.default)
7481

0 commit comments

Comments
 (0)