-
Notifications
You must be signed in to change notification settings - Fork 328
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
Make radios and checkboxes components easier to link to from error summary #1426
Make radios and checkboxes components easier to link to from error summary #1426
Conversation
@@ -8,7 +8,7 @@ module.exports = (app) => { | |||
body('passport-number') | |||
.exists() | |||
.not().isEmpty().withMessage('Enter your passport number'), | |||
body('expiry-day') | |||
body('expiry') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced making these changes on the date input component actually makes it any easier.
You can see that I was not able to simplify the logic in the validation section:
govuk-frontend/app/views/full-page-examples/passport-details/index.js
Lines 28 to 52 in d41467d
// If any of the date inputs error apply a general error. | |
const expiryNamePrefix = 'expiry' | |
const expiryErrors = Object.values(errors).filter(error => error.id.includes(expiryNamePrefix + '-')) | |
if (expiryErrors) { | |
const firstExpiryErrorId = expiryErrors[0].id | |
// Get the first error message and merge it into a single error message. | |
errors[expiryNamePrefix] = { | |
id: expiryNamePrefix, | |
href: '#' + firstExpiryErrorId | |
} | |
// Construct a single error message based on all three error messages. | |
errors[expiryNamePrefix].text = 'Enter your expiry ' | |
if (expiryErrors.length === 3) { | |
errors[expiryNamePrefix].text += 'date' | |
} else { | |
errors[expiryNamePrefix].text += expiryErrors.map(error => error.text.replace('Enter your expiry ', '')).join(' and ') | |
} | |
} | |
let errorSummary = Object.values(errors) | |
if (expiryErrors) { | |
// Remove all other errors from the summary so we only have one message that links to the expiry input. | |
errorSummary = errorSummary.filter(error => !error.id.includes(expiryNamePrefix + '-')) | |
} |
Is it worth changing the date input considering this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed that the full page example that features the date input doesnt work anymore, so maybe I will have to change this logic, will see if it makes it simpler or not, I suspect it'll be more complex than it already is...
7c9706e
to
36be88e
Compare
36be88e
to
78691cd
Compare
78691cd
to
8407a10
Compare
8407a10
to
84f05a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thorough cleanup @NickColley, thanks!
CHANGELOG.md
Outdated
@@ -4,6 +4,35 @@ | |||
|
|||
💥 Breaking changes: | |||
|
|||
- Make radios and checkboxes components easier to link to from the error summary component | |||
|
|||
The first input in the radios and checkboxes will have an `id` and `name` attribute that match, allowing for easier generation of error summary links. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only true if you haven't specified your own idPrefix
, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should be more explicit about what's actually changed here?
Perhaps something like…
The ID of the first input in a set of radios or checkboxes will no longer be suffixed with
-1
. This means that it will be the same as youridPrefix
, or ifidPrefix
is not set it'll be the same as thename
We've made this change to make it easier to link from the error summary to radios or checkboxes.
CHANGELOG.md
Outdated
|
||
The first input in the radios and checkboxes will have an `id` and `name` attribute that match, allowing for easier generation of error summary links. | ||
|
||
You should check that clicking an error link for a radios or checkboxes results in the first input being focused. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it clear enough what we mean by 'error link'? Would it be better to use something like:
You should check that clicking an error link for a radios or checkboxes results in the first input being focused. | |
You should check that clicking a link from the error summary to radios or checkboxes results in the first input being focused. |
|
||
You should check that clicking an error link for a radios or checkboxes results in the first input being focused. | ||
|
||
To migrate you can update your error summary so it links to the first element, for example if you are using an `idPrefix` option of `question`, your error summary can now link to `#question` instead of `#question-1`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users might also be overriding the id
of the first radio/checkbox, which I think is what we suggested in #929. In that case, they could remove the id
as it won't be doing anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured since this wasnt required that I'd leave the up to people themselves rather than potentially confusing things... maybe should just have this in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this and I'm worried that there's just a lot of steps being described that could confuse people so I wanted to focus on the things they definitely need to do...
There's no real harm leaving those item.id
s around, it's just not necessary anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nevermind I've included it after all..
If you cannot change your error summary, you could change your component to set the `item.id` option for the first input, for example: | ||
|
||
```javascript | ||
{{ govukRadios({ |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
{%- set id = item.id -%} | ||
{%- else -%} | ||
{%- set id = idPrefix -%} | ||
{#- The first id should not have a prefix so it's easy to link to from the error summary component -#} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number is a suffix, not a prefix 🙂
{#- The first id should not have a prefix so it's easy to link to from the error summary component -#} | |
{#- The first id should not have a number suffix so it's easy to link to from the error summary component -#} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i always get these mixed up lol :)
{%- set id = idPrefix -%} | ||
{#- The first id should not have a prefix so it's easy to link to from the error summary component -#} | ||
{%- if not loop.first -%} | ||
{%- set id = id + "-" + loop.index -%} |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give this a go thanks for the comment
84f05a0
to
bb86303
Compare
bb86303
to
0b4ad6b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
If the user has not specified an id for the first item, it will by default render without a suffix number which will make it easier to link to from the error summary as it'll match the `name` attribute.
If the user has not specified an id for the first item, it will by default render without a suffix number which will make it easier to link to from the error summary as it'll match the `name` attribute.
0b4ad6b
to
a33f173
Compare
Updates the radios, checkboxes and date input components so it's easier to link to their first items from the error summary component.
I have tried to simplify the date input component in a similar way but the complexity was the same after I changed it, if not worse. So if we want to make the date input component easier to use in the future we'll need to think about something else.
Closes #929