Skip to content

Commit

Permalink
Allow items to override checked state from values
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
36degrees committed May 23, 2022
1 parent 5435716 commit 095cec3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/govuk/components/checkboxes/checkboxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,17 @@ examples:
text: British
- value: irish
text: Irish

- name: item checked overrides values
hidden: true
data:
name: colors
items:
- value: red
text: Red
- value: green
text: Green
checked: false
- value: blue
text: Blue
values: [red, green]
2 changes: 1 addition & 1 deletion src/govuk/components/checkboxes/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{%- if item.divider %}
<div class="govuk-checkboxes__divider">{{ item.divider }}</div>
{%- else %}
{% set isChecked = item.checked or (params.values and item.value in params.values) %}
{% set isChecked = item.checked | default(params.values and item.value in params.values) %}
{% set hasHint = true if item.hint.text or item.hint.html %}
{% set itemHintId = id + "-item-hint" if hasHint else "" %}
{% set itemDescribedBy = describedBy if not hasFieldset else "" %}
Expand Down
7 changes: 7 additions & 0 deletions src/govuk/components/checkboxes/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ describe('Checkboxes', () => {
expect($other.attr('checked')).toEqual('checked')
})

it('allows item.checked to override values', () => {
const $ = render('checkboxes', examples['item checked overrides value'])

const $green = $('.govuk-checkboxes').find('input[value="green"]')
expect($green.attr('checked')).toBeUndefined()
})

describe('when they include attributes', () => {
it('it renders the attributes', () => {
const $ = render('checkboxes', examples['items with attributes'])
Expand Down
14 changes: 14 additions & 0 deletions src/govuk/components/radios/radios.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,17 @@ examples:
- value: no
text: No
checked: true

- name: item checked overrides value
hidden: true
data:
name: colors
items:
- value: red
text: Red
- value: green
text: Green
checked: false
- value: blue
text: Blue
value: green
2 changes: 1 addition & 1 deletion src/govuk/components/radios/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{%- if item.divider %}
<div class="govuk-radios__divider">{{ item.divider }}</div>
{%- else %}
{% set isChecked = item.checked or (params.value and item.value == params.value) %}
{% set isChecked = item.checked | default(params.value and item.value == params.value) %}
{% set hasHint = true if item.hint.text or item.hint.html %}
{% set itemHintId = id + '-item-hint' %}
<div class="govuk-radios__item">
Expand Down
7 changes: 7 additions & 0 deletions src/govuk/components/radios/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ describe('Radios', () => {
expect($lastInput.attr('checked')).toEqual('checked')
})

it('allows item.checked to override value', () => {
const $ = render('radios', examples['item checked overrides value'])

const $green = $('.govuk-radios').find('input[value="green"]')
expect($green.attr('checked')).toBeUndefined()
})

describe('when they include attributes', () => {
it('it renders the attributes', () => {
const $ = render('radios', examples['items with attributes'])
Expand Down
14 changes: 14 additions & 0 deletions src/govuk/components/select/select.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,17 @@ examples:
-
value: 2
text: GOV.UK frontend option 2

- name: item selected overrides value
hidden: true
data:
name: colors
items:
- value: red
text: Red
- value: green
text: Green
selected: false
- value: blue
text: Blue
value: green
2 changes: 1 addition & 1 deletion src/govuk/components/select/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{% for item in params.items %}
{% if item %}
<option value="{{ item.value }}"
{{-" selected" if item.selected or (params.value and item.value == params.value) }}
{{-" selected" if item.selected | default(params.value and item.value == params.value) }}
{{-" disabled" if item.disabled }}
{%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>{{ item.text }}</option>
{% endif %}
Expand Down
7 changes: 7 additions & 0 deletions src/govuk/components/select/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ describe('Select', () => {
expect($selectedItem.attr('selected')).toBeTruthy()
})

it('allows item.selected to override value', () => {
const $ = render('select', examples['item selected overrides value'])

const $selectedItem = $('option[value="green"]')
expect($selectedItem.attr('selected')).toBeUndefined()
})

it('renders item with disabled', () => {
const $ = render('select', examples.default)

Expand Down

0 comments on commit 095cec3

Please sign in to comment.