Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attributes on checkboxes/radios #942

Merged
merged 1 commit into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

([PR #N](https://github.com/alphagov/govuk-frontend/pull/N))

- Allow attributes on checkboxes/radios

You can now provide attributes on checkbox and radio items
`attributes: { 'data-attribute': 'value' }`

([PR #942](https://github.com/alphagov/govuk-frontend/pull/942))

🔧 Fixes:

- Pull Request Title goes here
Expand Down
12 changes: 12 additions & 0 deletions src/components/checkboxes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,18 @@ If you are using Nunjucks,then macros take the following arguments

<tr class="govuk-table__row">

<th class="govuk-table__header" scope="row">items.{}.attributes</th>

<td class="govuk-table__cell ">object</td>

<td class="govuk-table__cell ">No</td>

<td class="govuk-table__cell ">Any extra HTML attributes (for example data attributes) to add to the checkbox input tag.</td>

</tr>

<tr class="govuk-table__row">

<th class="govuk-table__header" scope="row">classes</th>

<td class="govuk-table__cell ">string</td>
Expand Down
14 changes: 14 additions & 0 deletions src/components/checkboxes/README.njk
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@
text: 'If true, checkbox will be disabled.'
}
],
[
{
text: 'items.{}.attributes'
},
{
text: 'object'
},
{
text: 'No'
},
{
text: 'Any extra HTML attributes (for example data attributes) to add to the checkbox input tag.'
}
],
[
{
text: 'classes'
Expand Down
3 changes: 2 additions & 1 deletion src/components/checkboxes/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
{{-" checked" if item.checked }}
{{-" disabled" if item.disabled }}
{%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%}
{%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%}>
{%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%}
{%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>
{{ govukLabel({
html: item.html,
text: item.text,
Expand Down
36 changes: 36 additions & 0 deletions src/components/checkboxes/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,42 @@ describe('Checkboxes', () => {
expect($secondInput.attr('checked')).toEqual('checked')
expect($lastInput.attr('checked')).toEqual('checked')
})

describe('when they include attributes', () => {
it('it renders the attributes', () => {
const $ = render('checkboxes', {
name: 'example-name',
items: [
{
value: '1',
text: 'Option 1',
attributes: {
'data-attribute': 'ABC',
'data-second-attribute': 'DEF'
}
},
{
value: '2',
text: 'Option 2',
attributes: {
'data-attribute': 'GHI',
'data-second-attribute': 'JKL'
}
}
]
})

const $component = $('.govuk-checkboxes')

const $firstInput = $component.find('.govuk-checkboxes__item:first-child input')
expect($firstInput.attr('data-attribute')).toEqual('ABC')
expect($firstInput.attr('data-second-attribute')).toEqual('DEF')

const $lastInput = $component.find('.govuk-checkboxes__item:last-child input')
expect($lastInput.attr('data-attribute')).toEqual('GHI')
expect($lastInput.attr('data-second-attribute')).toEqual('JKL')
})
})
})

describe('when they include a hint', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/components/radios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,18 @@ If you are using Nunjucks,then macros take the following arguments

<tr class="govuk-table__row">

<th class="govuk-table__header" scope="row">items.{}.attributes</th>

<td class="govuk-table__cell ">object</td>

<td class="govuk-table__cell ">No</td>

<td class="govuk-table__cell ">Any extra HTML attributes (for example data attributes) to add to the radio input tag.</td>

</tr>

<tr class="govuk-table__row">

<th class="govuk-table__header" scope="row">classes</th>

<td class="govuk-table__cell ">string</td>
Expand Down
14 changes: 14 additions & 0 deletions src/components/radios/README.njk
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ Let users select a single option from a list.
text: 'If true, radio will be disabled.'
}
],
[
{
text: 'items.{}.attributes'
},
{
text: 'object'
},
{
text: 'No'
},
{
text: 'Any extra HTML attributes (for example data attributes) to add to the radio input tag.'
}
],
[
{
text: 'classes'
Expand Down
3 changes: 2 additions & 1 deletion src/components/radios/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
{{-" checked" if item.checked }}
{{-" disabled" if item.disabled }}
{%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%}
{%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%}>
{%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%}
{%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>
{{ govukLabel({
html: item.html,
text: item.text,
Expand Down
37 changes: 37 additions & 0 deletions src/components/radios/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,43 @@ describe('Radios', () => {
expect($lastInput.attr('checked')).toEqual('checked')
})

describe('when they include attributes', () => {
it('it renders the attributes', () => {
const $ = render('radios', {
name: 'example-name',
items: [
{
value: 'yes',
text: 'Yes',
attributes: {
'data-attribute': 'ABC',
'data-second-attribute': 'DEF'
}
},
{
value: 'no',
text: 'No',
checked: true,
attributes: {
'data-attribute': 'GHI',
'data-second-attribute': 'JKL'
}
}
]
})

const $component = $('.govuk-radios')

const $firstInput = $component.find('.govuk-radios__item:first-child input')
expect($firstInput.attr('data-attribute')).toEqual('ABC')
expect($firstInput.attr('data-second-attribute')).toEqual('DEF')

const $lastInput = $component.find('.govuk-radios__item:last-child input')
expect($lastInput.attr('data-attribute')).toEqual('GHI')
expect($lastInput.attr('data-second-attribute')).toEqual('JKL')
})
})

describe('when they include a hint', () => {
it('it renders the hint text', () => {
const $ = render('radios', {
Expand Down