diff --git a/CHANGELOG.md b/CHANGELOG.md index fd91065a8c..56e516ffe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -141,11 +141,12 @@ ([PR #960](https://github.com/alphagov/govuk-frontend/pull/960)) -- Pull Request Title goes here +- Allow attributes on select items - Description goes here (optional) + You can now provide attributes on select items + `attributes: { 'data-attribute': 'value' }` - ([PR #N](https://github.com/alphagov/govuk-frontend/pull/N)) + ([PR #977](https://github.com/alphagov/govuk-frontend/pull/977)) 🔧 Fixes: diff --git a/src/components/select/README.md b/src/components/select/README.md index d39bd93a5e..40e33734da 100644 --- a/src/components/select/README.md +++ b/src/components/select/README.md @@ -355,6 +355,18 @@ If you are using Nunjucks,then macros take the following arguments
<select>
element represents a control that provides
text: 'Sets the option item as disabled.'
}
],
+ [
+ {
+ text: 'item.attributes'
+ },
+ {
+ text: 'object'
+ },
+ {
+ text: 'No'
+ },
+ {
+ text: 'Any extra HTML attributes (for example data attributes) to the select option tag.'
+ }
+ ],
[
{
text: 'label'
diff --git a/src/components/select/template.njk b/src/components/select/template.njk
index 844176e484..43c7a03741 100644
--- a/src/components/select/template.njk
+++ b/src/components/select/template.njk
@@ -38,7 +38,10 @@
diff --git a/src/components/select/template.test.js b/src/components/select/template.test.js
index 6638417167..c676f15dc4 100644
--- a/src/components/select/template.test.js
+++ b/src/components/select/template.test.js
@@ -151,6 +151,40 @@ describe('Select', () => {
})
})
+ describe('when they include option attributes', () => {
+ it('renders the option attributes', () => {
+ const $ = render('select', {
+ value: '2',
+ items: [
+ {
+ text: 'Option 1',
+ attributes: {
+ 'data-attribute': 'ABC',
+ 'data-second-attribute': 'DEF'
+ }
+ },
+ {
+ text: 'Options 2',
+ attributes: {
+ 'data-attribute': 'GHI',
+ 'data-second-attribute': 'JKL'
+ }
+ }
+ ]
+ })
+
+ const $component = $('.govuk-select')
+
+ const $firstInput = $component.find('option:first-child')
+ expect($firstInput.attr('data-attribute')).toEqual('ABC')
+ expect($firstInput.attr('data-second-attribute')).toEqual('DEF')
+
+ const $secondInput = $component.find('option:last-child')
+ expect($secondInput.attr('data-attribute')).toEqual('GHI')
+ expect($secondInput.attr('data-second-attribute')).toEqual('JKL')
+ })
+ })
+
describe('when it includes a hint', () => {
it('renders the hint', () => {
const $ = render('select', {