From 72b9c7109fad8399504d2bda33dbd6c598b9bed3 Mon Sep 17 00:00:00 2001 From: Oliver Byford Date: Fri, 9 Jun 2023 11:12:08 +0100 Subject: [PATCH 1/3] Omit value attribute from options with no value In HTML, if you omit the `value` attribute from an ` diff --git a/packages/govuk-frontend/src/govuk/components/select/template.test.js b/packages/govuk-frontend/src/govuk/components/select/template.test.js index 86dc9e9b0b..74e53c51ca 100644 --- a/packages/govuk-frontend/src/govuk/components/select/template.test.js +++ b/packages/govuk-frontend/src/govuk/components/select/template.test.js @@ -33,13 +33,43 @@ describe('Select', () => { expect($items.length).toEqual(3) }) - it('renders item with value', () => { + it('includes the value attribute', () => { const $ = render('select', examples.default) const $firstItem = $('.govuk-select option:first-child') expect($firstItem.attr('value')).toEqual('1') }) + it('includes the value attribute when the value option is an empty string', () => { + const $ = render('select', examples['with falsey values']) + + const $firstItem = $('.govuk-select option:nth(0)') + expect($firstItem.attr('value')).toEqual('') + }) + + it('includes the value attribute when the value option is false', () => { + const $ = render('select', examples['with falsey values']) + + const $secondItem = $('.govuk-select option:nth(1)') + expect($secondItem.attr('value')).toEqual('false') + }) + + it('includes the value attribute when the value option is 0', () => { + const $ = render('select', examples['with falsey values']) + + const $thirdItem = $('.govuk-select option:nth(2)') + expect($thirdItem.attr('value')).toEqual('0') + }) + + it('omits the value attribute if no value option is provided', () => { + const $ = render('select', examples['without values']) + + const $firstItem = $('.govuk-select option:first-child') + // Ideally we'd test for $firstItem.attr('value') == undefined but it's + // broken in Cheerio – https://github.com/cheeriojs/cheerio/issues/3237 + expect($firstItem.toString()).not.toContain('value') + }) + it('renders item with text', () => { const $ = render('select', examples.default) @@ -90,7 +120,7 @@ describe('Select', () => { }) it('renders without falsely items', () => { - const $ = render('select', examples['with falsey values']) + const $ = render('select', examples['with falsey items']) const $items = $('.govuk-select option') expect($items.length).toEqual(2) From e1bab47321616770fd4e68e1d21e9ee39d146d41 Mon Sep 17 00:00:00 2001 From: Oliver Byford Date: Fri, 9 Jun 2023 11:23:51 +0100 Subject: [PATCH 2/3] Allow selecting options based on implicit value Allow the top-level `value` option on a `govukSelect` to work with options that have no explicit value set. The value of an option element when there is no `value` attribute is the text content of the element, so use that instead. --- .../src/govuk/components/select/select.yaml | 13 +++++++++++++ .../src/govuk/components/select/template.njk | 4 +++- .../src/govuk/components/select/template.test.js | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/govuk-frontend/src/govuk/components/select/select.yaml b/packages/govuk-frontend/src/govuk/components/select/select.yaml index d82dcc42b0..da5ffcee1a 100644 --- a/packages/govuk-frontend/src/govuk/components/select/select.yaml +++ b/packages/govuk-frontend/src/govuk/components/select/select.yaml @@ -312,6 +312,19 @@ examples: - text: Green - text: Blue + - name: without values with selected value + hidden: true + data: + name: colors + id: colors + label: + text: Label text goes here + items: + - text: Red + - text: Green + - text: Blue + value: Green + - name: with falsey values hidden: true data: diff --git a/packages/govuk-frontend/src/govuk/components/select/template.njk b/packages/govuk-frontend/src/govuk/components/select/template.njk index 718bf8f306..77348750c3 100644 --- a/packages/govuk-frontend/src/govuk/components/select/template.njk +++ b/packages/govuk-frontend/src/govuk/components/select/template.njk @@ -44,8 +44,10 @@ {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}> {% for item in params.items %} {% if item %} + {# Allow selecting by text content (the value for an option when no value attribute is specified) #} + {% set effectiveValue = item.value | default(item.text) %} {% endif %} diff --git a/packages/govuk-frontend/src/govuk/components/select/template.test.js b/packages/govuk-frontend/src/govuk/components/select/template.test.js index 74e53c51ca..6bf15e4bc1 100644 --- a/packages/govuk-frontend/src/govuk/components/select/template.test.js +++ b/packages/govuk-frontend/src/govuk/components/select/template.test.js @@ -91,6 +91,13 @@ describe('Select', () => { expect($selectedItem.attr('selected')).toBeTruthy() }) + it('selects options with implicit value using selected value', () => { + const $ = render('select', examples['without values with selected value']) + + const $selectedItem = $("option:contains('Green')") + expect($selectedItem.attr('selected')).toBeTruthy() + }) + it('allows item.selected to override value', () => { const $ = render('select', examples['item selected overrides value']) From d8222a662c128627a28cc6d6faa6c6e403276034 Mon Sep 17 00:00:00 2001 From: Oliver Byford Date: Fri, 9 Jun 2023 11:33:35 +0100 Subject: [PATCH 3/3] Document in CHANGELOG --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8476105665..08cf39de3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -208,6 +208,16 @@ If you’re not using Nunjucks macros, update your warning text HTML to replace This change was introduced in [pull request #3569: Remove unnecesary class from Warning Text component](https://github.com/alphagov/govuk-frontend/pull/3569). +#### Check that selects work as expected + +The `govukSelect` macro will no longer include an empty value attribute for options that do not have a value set. + +This means that the value of the select if that option is selected will now be the text content of the option, rather than an empty string. + +If you need to maintain the existing behaviour, you can set the value to an empty string. + +This change was introduced in [pull request #3773: Omit the value attribute from select options with no value](https://github.com/alphagov/govuk-frontend/pull/3773). + ## 4.6.0 (Feature release) ### New features