Skip to content

Commit

Permalink
fix(storefront): bctheme-448 fix multiple swatch options
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Apr 29, 2021
1 parent 2e7effa commit 059b827
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 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
- Fixed displaying swatch name for multiple swatch options on page. [#2040](https://github.com/bigcommerce/cornerstone/pull/2040)
- Incorrect focus order for product carousels. [#2034](https://github.com/bigcommerce/cornerstone/pull/2034)
- Removed duplicates of main tag.[#2032](https://github.com/bigcommerce/cornerstone/pull/2032)
- Hamburger Menu Icon missing on Google AMP Pages. [#2022](https://github.com/bigcommerce/cornerstone/pull/2022)
Expand Down
33 changes: 26 additions & 7 deletions assets/js/theme/common/product-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ export default class ProductDetails extends ProductDetailsBase {
this.imageGallery.init();
this.listenQuantityChange();
this.$swatchOptionMessage = $('.swatch-option-message');
this.swatchOptionMessageInitText = this.$swatchOptionMessage.text();
this.swatchInitMessageStorage = {};
this.swatchGroupIdList = $('[id^="swatchGroup"]').map((_, group) => $(group).attr('id'));

// store swatch options initial message text
if (this.swatchGroupIdList.length && isEmpty(this.swatchInitMessageStorage)) {
this.swatchGroupIdList.each((_, swatchGroupId) => {
if (!this.swatchInitMessageStorage[swatchGroupId]) {
this.swatchInitMessageStorage[swatchGroupId] = $(`#${swatchGroupId} ~ .swatch-option-message`).text().trim();
}
});
}

const $form = $('form[data-cart-item-add]', $scope);
const $productOptionsElement = $('[data-product-option-change]', $form);
Expand All @@ -43,10 +53,17 @@ export default class ProductDetails extends ProductDetailsBase {

if (context.showSwatchNames) {
this.$swatchOptionMessage.removeClass('u-hidden');
$productSwatchGroup.on('change', ({ target }) => this.showSwatchNameOnOption($(target)));

$productSwatchGroup.on('change', ({ target }) => {
const swatchGroupElement = target.parentNode.parentNode;

this.showSwatchNameOnOption($(target), $(swatchGroupElement));
});

$.each($productSwatchGroup, (_, element) => {
if ($(element).is(':checked')) this.showSwatchNameOnOption($(element));
const swatchGroupElement = element.parentNode.parentNode;

if ($(element).is(':checked')) this.showSwatchNameOnOption($(element), $(swatchGroupElement));
});
}

Expand Down Expand Up @@ -218,12 +235,14 @@ export default class ProductDetails extends ProductDetailsBase {
* if this setting is enabled in Page Builder
* show name for swatch option
*/
showSwatchNameOnOption($swatch) {
showSwatchNameOnOption($swatch, $swatchGroup) {
const swatchName = $swatch.attr('aria-label');
const activeSwatchGroupId = $swatchGroup.attr('aria-labelledby');
const $swatchOptionMessage = $(`#${activeSwatchGroupId} ~ .swatch-option-message`);

$('[data-product-attribute="swatch"] [data-option-value]').text(swatchName);
this.$swatchOptionMessage.text(`${this.swatchOptionMessageInitText} ${swatchName}`);
this.setLiveRegionAttributes(this.$swatchOptionMessage, 'status', 'assertive');
$('[data-option-value]', $swatchGroup).text(swatchName);
$swatchOptionMessage.text(`${this.swatchInitMessageStorage[activeSwatchGroupId]} ${swatchName}`);
this.setLiveRegionAttributes($swatchOptionMessage, 'status', 'assertive');
}

setLiveRegionAttributes($element, roleType, ariaLiveStatus) {
Expand Down
4 changes: 2 additions & 2 deletions templates/components/products/options/swatch.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form-field" data-product-attribute="swatch" role="radiogroup" aria-labelledby="swatchGroup">
<label class="form-label form-label--alternate form-label--inlineSmall" id="swatchGroup">
<div class="form-field" data-product-attribute="swatch" role="radiogroup" aria-labelledby="swatchGroup_{{id}}">
<label class="form-label form-label--alternate form-label--inlineSmall" id="swatchGroup_{{id}}">
{{this.display_name}}:
<span data-option-value></span>

Expand Down

0 comments on commit 059b827

Please sign in to comment.