Skip to content

Commit

Permalink
fix(storefront): BCTHEME-372 Error message not announced automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
BC-tymurbiedukhin committed Feb 10, 2021
1 parent 3ded830 commit 30e1244
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Draft
- Error message not announced automatically. [#1983](https://github.com/bigcommerce/cornerstone/pull/1983)
- Make every product option group id unique. [#1979](https://github.com/bigcommerce/cornerstone/pull/1979)
- Fixed required checkbox message displaying. [1963](https://github.com/bigcommerce/cornerstone/pull/1963)
- Provided sufficient & informative text along with the color swatches [#1976](https://github.com/bigcommerce/cornerstone/pull/1976)
Expand Down
16 changes: 14 additions & 2 deletions assets/js/theme/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import nod from './common/nod';
import Wishlist from './wishlist';
import validation from './common/form-validation';
import stateCountry from './common/state-country';
import { classifyForm, Validators, insertStateHiddenField, createPasswordValidationErrorTextObject } from './common/utils/form-utils';
import {
classifyForm,
Validators,
announceInputErrorMessage,
insertStateHiddenField,
createPasswordValidationErrorTextObject,
} from './common/utils/form-utils';
import { createTranslationDictionary } from './common/utils/translations-utils';
import { creditCardType, storeInstrument, Validators as CCValidators, Formatters as CCFormatters } from './common/payment-method';
import swal from './global/sweet-alert';
Expand Down Expand Up @@ -139,6 +145,7 @@ export default class Account extends PageManager {
const $stateElement = $(stateSelector);
const addressValidator = nod({
submit: 'form[data-address-form] input[type="submit"]',
tap: announceInputErrorMessage,
});

addressValidator.add(validationModel);
Expand Down Expand Up @@ -226,7 +233,10 @@ export default class Account extends PageManager {

const validationModel = validation($paymentMethodForm, this.context);
const paymentMethodSelector = 'form[data-payment-method-form]';
const paymentMethodValidator = nod({ submit: `${paymentMethodSelector} input[type="submit"]` });
const paymentMethodValidator = nod({
submit: `${paymentMethodSelector} input[type="submit"]`,
tap: announceInputErrorMessage,
});
const $stateElement = $(`${paymentMethodSelector} [data-field-type="State"]`);

let $last;
Expand Down Expand Up @@ -317,6 +327,7 @@ export default class Account extends PageManager {
const formEditSelector = 'form[data-edit-account-form]';
const editValidator = nod({
submit: '${formEditSelector} input[type="submit"]',
tap: announceInputErrorMessage,
});
const emailSelector = `${formEditSelector} [data-field-type="EmailAddress"]`;
const $emailElement = $(emailSelector);
Expand Down Expand Up @@ -400,6 +411,7 @@ export default class Account extends PageManager {
registerInboxValidation($inboxForm) {
const inboxValidator = nod({
submit: 'form[data-inbox-form] input[type="submit"]',
tap: announceInputErrorMessage,
});

inboxValidator.add([
Expand Down
11 changes: 10 additions & 1 deletion assets/js/theme/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import stateCountry from './common/state-country';
import nod from './common/nod';
import validation from './common/form-validation';
import forms from './common/models/forms';
import { classifyForm, Validators, createPasswordValidationErrorTextObject } from './common/utils/form-utils';
import {
classifyForm,
Validators,
createPasswordValidationErrorTextObject,
announceInputErrorMessage,
} from './common/utils/form-utils';
import { createTranslationDictionary } from './common/utils/translations-utils';

export default class Auth extends PageManager {
Expand All @@ -19,6 +24,7 @@ export default class Auth extends PageManager {

this.loginValidator = nod({
submit: '.login-form input[type="submit"]',
tap: announceInputErrorMessage,
});

this.loginValidator.add([
Expand Down Expand Up @@ -56,6 +62,7 @@ export default class Auth extends PageManager {
registerForgotPasswordValidation($forgotPasswordForm) {
this.forgotPasswordValidator = nod({
submit: '.forgot-password-form input[type="submit"]',
tap: announceInputErrorMessage,
});

this.forgotPasswordValidator.add([
Expand Down Expand Up @@ -86,6 +93,7 @@ export default class Auth extends PageManager {
const newPasswordForm = '.new-password-form';
const newPasswordValidator = nod({
submit: $(`${newPasswordForm} input[type="submit"]`),
tap: announceInputErrorMessage,
});
const passwordSelector = $(`${newPasswordForm} input[name="password"]`);
const password2Selector = $(`${newPasswordForm} input[name="password_confirm"]`);
Expand All @@ -103,6 +111,7 @@ export default class Auth extends PageManager {
const validationModel = validation($createAccountForm, this.context);
const createAccountValidator = nod({
submit: `${this.formCreateSelector} input[type='submit']`,
tap: announceInputErrorMessage,
});
const $stateElement = $('[data-field-type="State"]');
const emailSelector = `${this.formCreateSelector} [data-field-type='EmailAddress']`;
Expand Down
3 changes: 2 additions & 1 deletion assets/js/theme/cart/shipping-estimator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stateCountry from '../common/state-country';
import nod from '../common/nod';
import utils from '@bigcommerce/stencil-utils';
import { Validators } from '../common/utils/form-utils';
import { Validators, announceInputErrorMessage } from '../common/utils/form-utils';
import collapsibleFactory from '../common/collapsible';
import swal from '../global/sweet-alert';

Expand All @@ -22,6 +22,7 @@ export default class ShippingEstimator {
this.shippingEstimator = 'form[data-shipping-estimator]';
this.shippingValidator = nod({
submit: `${this.shippingEstimator} .shipping-estimate-submit`,
tap: announceInputErrorMessage,
});

$('.shipping-estimate-submit', this.$element).on('click', event => {
Expand Down
25 changes: 24 additions & 1 deletion assets/js/theme/common/utils/form-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ function insertStateHiddenField($stateField) {
$stateField.after($('<input />', stateFieldAttrs));
}

/**
* Announce form input error message by screen reader
* @param {params.element} dom input element where checking is happened
* @param {params.result} result of validation check
*/
function announceInputErrorMessage({ element, result }) {
if (result) {
return;
}
const activeInputContainer = $(element).parent();
// the reason for using span tag is nod-validate lib
// which does not add error message class while initialising form
const errorMessage = $(activeInputContainer).find('span');

if (errorMessage.length) {
const $errMessage = $(errorMessage[0]);

if (!$errMessage.attr('role')) {
$errMessage.attr('role', 'alert');
}
}
}

const Validators = {
/**
* Sets up a new validation when the form is dirty
Expand Down Expand Up @@ -310,4 +333,4 @@ const Validators = {
},
};

export { Validators, insertStateHiddenField };
export { Validators, insertStateHiddenField, announceInputErrorMessage };
2 changes: 2 additions & 0 deletions assets/js/theme/contact-us.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PageManager from './page-manager';
import nod from './common/nod';
import forms from './common/models/forms';
import { announceInputErrorMessage } from './common/utils/form-utils';

export default class ContactUs extends PageManager {
onReady() {
Expand All @@ -11,6 +12,7 @@ export default class ContactUs extends PageManager {
const formSelector = 'form[data-contact-form]';
const contactUsValidator = nod({
submit: `${formSelector} input[type="submit"]`,
tap: announceInputErrorMessage,
});
const $contactForm = $(formSelector);

Expand Down
3 changes: 3 additions & 0 deletions assets/js/theme/gift-certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import nod from './common/nod';
import giftCertChecker from './common/gift-certificate-validator';
import formModel from './common/models/forms';
import { createTranslationDictionary } from './common/utils/translations-utils';
import { announceInputErrorMessage } from './common/utils/form-utils';
import { api } from '@bigcommerce/stencil-utils';
import { defaultModal } from './global/modal';

Expand Down Expand Up @@ -48,6 +49,7 @@ export default class GiftCertificate extends PageManager {
const purchaseValidator = nod({
submit: '#gift-certificate-form input[type="submit"]',
delay: 300,
tap: announceInputErrorMessage,
});

if ($customAmounts.length) {
Expand Down Expand Up @@ -196,6 +198,7 @@ export default class GiftCertificate extends PageManager {
checkCertBalanceValidator($balanceForm) {
const balanceValidator = nod({
submit: $balanceForm.find('input[type="submit"]'),
tap: announceInputErrorMessage,
});

balanceValidator.add({
Expand Down
2 changes: 2 additions & 0 deletions assets/js/theme/product/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import nod from '../common/nod';
import { CollapsibleEvents } from '../common/collapsible';
import forms from '../common/models/forms';
import { safeString } from '../common/utils/safe-string';
import { announceInputErrorMessage } from '../common/utils/form-utils';

export default class {
constructor($reviewForm) {
this.validator = nod({
submit: $reviewForm.find('input[type="submit"]'),
tap: announceInputErrorMessage,
});

this.$reviewsContent = $('#product-reviews');
Expand Down
2 changes: 2 additions & 0 deletions assets/js/theme/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hooks } from '@bigcommerce/stencil-utils';
import CatalogPage from './catalog';
import FacetedSearch from './common/faceted-search';
import { announceInputErrorMessage } from './common/utils/form-utils';
import compareProducts from './global/compare-products';
import urlUtils from './common/utils/url-utils';
import Url from 'url';
Expand Down Expand Up @@ -318,6 +319,7 @@ export default class Search extends CatalogPage {
this.$form = $form;
this.validator = nod({
submit: $form,
tap: announceInputErrorMessage,
});

return this;
Expand Down
2 changes: 2 additions & 0 deletions assets/js/theme/wishlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'foundation-sites/js/foundation/foundation.reveal';
import nod from './common/nod';
import PageManager from './page-manager';
import { wishlistPaginatorHelper } from './common/utils/pagination-utils';
import { announceInputErrorMessage } from './common/utils/form-utils';

export default class WishList extends PageManager {
constructor(context) {
Expand Down Expand Up @@ -33,6 +34,7 @@ export default class WishList extends PageManager {
registerAddWishListValidation($addWishlistForm) {
this.addWishlistValidator = nod({
submit: '.wishlist-form input[type="submit"]',
tap: announceInputErrorMessage,
});

this.addWishlistValidator.add([
Expand Down

0 comments on commit 30e1244

Please sign in to comment.