Skip to content

Commit

Permalink
Merge pull request #2690 from ONSdigital/enhancement/2195/optional-wi…
Browse files Browse the repository at this point in the history
…dth-table-column-param

Added optional column width percentage
  • Loading branch information
willcAND authored Jun 26, 2023
2 parents e7a0765 + 56503de commit 9887e2a
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 54 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions src/components/table/_macro-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

## th

| Name | Type | Required | Description |
| --------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| thClasses | string | false | Classes to add to the `th` element |
| ariaSort | string | false | Set to “ascending” or “descending” to set the default order of a table column when the page loads when setting `variants` to “sortable”. Defaults to “none”. |
| value | string | true | The content for the `th` cell |
| numeric | boolean | false | Set to “true” if all the cells in the column contain numbers. This aligns the content to the right so numbers can easily be compared. |
| Name | Type | Required | Description |
| --------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| thClasses | string | false | Classes to add to the `th` element |
| ariaSort | string | false | Set to “ascending” or “descending” to set the default order of a table column when the page loads when setting `variants` to “sortable”. Defaults to “none”. |
| value | string | true | The content for the `th` cell |
| numeric | boolean | false | Set to “true” if all the cells in the column contain numbers. This aligns the content to the right so numbers can easily be compared. |
| widthPercentage | integer | false | Sets the `width` attribute for the `th` element |

## tr

Expand Down
2 changes: 1 addition & 1 deletion src/components/table/_macro.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<thead class="ons-table__head">
<tr class="ons-table__row">
{% for th in params.ths %}
<th scope="col" class="ons-table__header{{ ' ' + th.thClasses if th.thClasses }}{{ " ons-table__header--numeric" if th.numeric }}"{% if 'sortable' in variants %} aria-sort="{{- th.ariaSort | default('none') -}}"{% endif %}>
<th scope="col" class="ons-table__header{{ ' ' + th.thClasses if th.thClasses }}{{ " ons-table__header--numeric" if th.numeric }}"{% if 'sortable' in variants %} aria-sort="{{- th.ariaSort | default('none') -}}"{% endif %}{% if th.widthPercentage %} width="{{ th.widthPercentage }}%"{% endif %}>
<span class="ons-table__header-text">{{- th.value -}}</span>
{% if 'sortable' in variants %}
{{
Expand Down
16 changes: 16 additions & 0 deletions src/components/table/_macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ describe('macro: table', () => {
expect($('.ons-table__header').hasClass('another-extra-column-class')).toBe(true);
});

it('adds additional width attribute to column header', () => {
const $ = cheerio.load(
renderComponent('table', {
...EXAMPLE_TABLE,
ths: [
{
value: 'Column 1',
widthPercentage: 50,
},
],
}),
);

expect($('.ons-table__header').attr('width')).toBe('50%');
});

it('does not add "numeric" modifier class to column header when `td.numeric` is not provided', () => {
const $ = cheerio.load(renderComponent('table', EXAMPLE_TABLE));

Expand Down
9 changes: 6 additions & 3 deletions src/components/table/example-table-numeric.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
"caption": "A basic table with numeric values",
"ths": [
{
"value": "Country"
"value": "Country",
"widthPercentage": 50
},
{
"value": "Population mid-2020",
"numeric": true
"numeric": true,
"widthPercentage": 25
},
{
"value": "% change 2019 to 2020",
"numeric": true
"numeric": true,
"widthPercentage": 25
}
],
"trs": [
Expand Down

0 comments on commit 9887e2a

Please sign in to comment.