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

Remove numeric class from columns #739

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
21 changes: 5 additions & 16 deletions src/middleware/dataview.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,14 @@ export const constructTableParams = (req, res, next) => {
const fields = uniqueDatasetFields
const rows = entities.map(entity => ({
columns: Object.fromEntries(fields.map(field => {
let value
let classes = ''
let value = entity[field]
const classes = ''
let html

// if the value is a number or a date string
if (/^\d{4}-\d{2}-\d{2}$/.test(entity[field]) || /^\d+(\.\d+)?$/.test(entity[field])) {
classes = 'govuk-table__cell--numeric'
value = entity[field]
}

const urlRegex = /^https?:\/\/[^\s]+$/

if (typeof entity[field] === 'string') {
const text = entity[field]
if (urlRegex.test(text)) {
html = `<a href='${text}' target='_blank' rel='noopener noreferrer' aria-label='${text} (opens in new tab)'>${text}</a>`
} else {
value = text
}
if (urlRegex.test(entity[field])) {
html = `<a href='${entity[field]}' target='_blank' rel='noopener noreferrer' aria-label='${entity[field]} (opens in new tab)'>${entity[field]}</a>`
value = undefined
}

const valueObj = {
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<table class="govuk-table dl-table">
<colgroup>
{% for field in params.fields %}
<col class="{% if (r/.*-date/g).test(field) %}govuk-table__cell--numeric{% endif %}" />
<col />
{% endfor %}
</colgroup>
<thead class="govuk-table__head dl-table__head">
Expand Down
8 changes: 4 additions & 4 deletions test/unit/middleware/dataview.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ describe('dataview.middleware.test.js', () => {
rows: [
{
columns: {
num_field: { value: 0.06, classes: 'govuk-table__cell--numeric', html: undefined },
date_field: { value: '2022-01-01', classes: 'govuk-table__cell--numeric', html: undefined }
num_field: { value: 0.06, classes: '', html: undefined },
date_field: { value: '2022-01-01', classes: '', html: undefined }
}
},
{
columns: {
num_field: { value: '10', classes: 'govuk-table__cell--numeric', html: undefined },
date_field: { value: '2022-01-02', classes: 'govuk-table__cell--numeric', html: undefined }
num_field: { value: '10', classes: '', html: undefined },
date_field: { value: '2022-01-02', classes: '', html: undefined }
}
}
]
Expand Down
Loading