From db60945ab5cdabaf79dfd63efa0a2e41bfe9d7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbc-yevhenii-buliuk=E2=80=9D?= <“yevhenii.buliuk@bigcommerce.com”> Date: Tue, 12 Jul 2022 12:18:03 +0300 Subject: [PATCH] fix(storefront): BCTHEME-1128 Form Input Error Message and Accessibility. --- CHANGELOG.md | 1 + assets/js/theme/auth.js | 14 ++++++++++++++ assets/js/theme/common/utils/form-utils.js | 7 +++++-- templates/components/common/forms/checkbox.html | 3 ++- templates/components/common/forms/date.html | 9 ++++++--- templates/components/common/forms/multiline.html | 3 ++- templates/components/common/forms/number.html | 3 ++- templates/components/common/forms/password.html | 3 ++- templates/components/common/forms/radio.html | 3 ++- templates/components/common/forms/select.html | 3 ++- .../components/common/forms/selectortext.html | 3 ++- templates/components/common/forms/text.html | 3 ++- 12 files changed, 42 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5e568f8e..be72876c57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Draft +- Form Input Error Message and Accessibility. [#2235](https://github.com/bigcommerce/cornerstone/issues/2235). ## 6.5.0 (06-24-2022) - Category icons do not appear in Search Form [#2221]https://github.com/bigcommerce/cornerstone/pull/2221 diff --git a/assets/js/theme/auth.js b/assets/js/theme/auth.js index dba4e76729..85a103f497 100644 --- a/assets/js/theme/auth.js +++ b/assets/js/theme/auth.js @@ -112,6 +112,7 @@ export default class Auth extends PageManager { const createAccountValidator = nod({ submit: `${this.formCreateSelector} input[type='submit']`, tap: announceInputErrorMessage, + delay: 0, }); const $stateElement = $('[data-field-type="State"]'); const emailSelector = `${this.formCreateSelector} [data-field-type='EmailAddress']`; @@ -177,6 +178,19 @@ export default class Auth extends PageManager { return; } + const errorFormFields = $createAccountForm.find('.form-field--error .focus-field'); + const formFieldErrorMessages = $createAccountForm.find('span.form-inlineMessage'); + + $(formFieldErrorMessages).each((index, message) => { + if (index > 0) { + $(message).removeAttr('id'); + $(message).removeAttr('role'); + $(message).removeAttr('aria-live'); + } + }); + + errorFormFields[0].focus(); + event.preventDefault(); }); } diff --git a/assets/js/theme/common/utils/form-utils.js b/assets/js/theme/common/utils/form-utils.js index bebb29a536..90341d6da2 100644 --- a/assets/js/theme/common/utils/form-utils.js +++ b/assets/js/theme/common/utils/form-utils.js @@ -141,9 +141,12 @@ function announceInputErrorMessage({ element, result }) { if (errorMessage.length) { const $errMessage = $(errorMessage[0]); + const ariaDescribedby = $(element).attr('aria-describedby'); - if (!$errMessage.attr('role')) { - $errMessage.attr('role', 'alert'); + if (!$errMessage.attr('role') && !$errMessage.attr('aria-live') && !$errMessage.attr('id')) { + $errMessage.attr('role', 'status'); + $errMessage.attr('aria-live', 'polite'); + $errMessage.attr('id', `${ariaDescribedby}`); } } } diff --git a/templates/components/common/forms/checkbox.html b/templates/components/common/forms/checkbox.html index dfafd60f4d..419fdc35d6 100644 --- a/templates/components/common/forms/checkbox.html +++ b/templates/components/common/forms/checkbox.html @@ -12,7 +12,8 @@ id="{{name}}" data-input data-label="{{label}}" - class="form-checkbox" + class="form-checkbox focus-field" + aria-describedby="{{id}}_describedby" {{#if checked}}checked{{/if}} {{#if private_id}}data-field-type="{{private_id}}"{{/if}} > diff --git a/templates/components/common/forms/date.html b/templates/components/common/forms/date.html index d7d6e00097..42e1fa253a 100644 --- a/templates/components/common/forms/date.html +++ b/templates/components/common/forms/date.html @@ -7,36 +7,39 @@