-
Notifications
You must be signed in to change notification settings - Fork 335
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
Prevent inputs ending up off screen or obscured by keyboards when linking from the error summary to inputs within a large fieldset #1570
Changes from all commits
5b822cb
fac0a74
200b058
db33439
7b27912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,9 @@ ErrorSummary.prototype.getFragmentFromUrl = function (url) { | |
* | ||
* Returns the first element that exists from this list: | ||
* | ||
* - The `<legend>` associated with the closest `<fieldset>` ancestor | ||
* - The `<legend>` associated with the closest `<fieldset>` ancestor, as long | ||
* as the top of it is no more than half a viewport height away from the | ||
* bottom of the input | ||
* - The first `<label>` that is associated with the input using for="inputId" | ||
* - The closest parent `<label>` | ||
* | ||
|
@@ -109,7 +111,32 @@ ErrorSummary.prototype.getAssociatedLegendOrLabel = function ($input) { | |
var legends = $fieldset.getElementsByTagName('legend') | ||
|
||
if (legends.length) { | ||
return legends[0] | ||
var $candidateLegend = legends[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory we could write tests for this logic, using puppeteer on the demo pages you've been testing, this could be useful in the future if we (hopefully not) encounter other edge cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added to the existing tests to try and cover the two new examples that have been added to the review app. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Epic! Thank you |
||
|
||
// If the input type is radio or checkbox, always use the legend if there | ||
// is one. | ||
if ($input.type === 'checkbox' || $input.type === 'radio') { | ||
return $candidateLegend | ||
} | ||
|
||
// For other input types, only scroll to the fieldset’s legend (instead of | ||
// the label associated with the input) if the input would end up in the | ||
// top half of the screen. | ||
// | ||
// This should avoid situations where the input either ends up off the | ||
// screen, or obscured by a software keyboard. | ||
var legendTop = $candidateLegend.getBoundingClientRect().top | ||
var inputRect = $input.getBoundingClientRect() | ||
|
||
// If the browser doesn't support Element.getBoundingClientRect().height | ||
// or window.innerHeight (like IE8), bail and just link to the label. | ||
if (inputRect.height && window.innerHeight) { | ||
var inputBottom = inputRect.top + inputRect.height | ||
|
||
if (inputBottom - legendTop < window.innerHeight / 2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When testing this on my machine I find that I'm always linked to the errored input for the last example despite the fieldset being able to fit all on the screen at once. Is the issue that mobile keyboards open after the navigation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More or less – the logic checks to see whether the input would end up entirely in the top half of the viewport, to allow for keyboards. |
||
return $candidateLegend | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
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.