Skip to content
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

fix(storefront): bctheme-448 fix multiple swatch options #2040

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
- Added settings for payment banners. [#2021](https://github.com/bigcommerce/cornerstone/pull/2021)
- Use https:// for schema markup. [#2039](https://github.com/bigcommerce/cornerstone/pull/2039)
- Update focus tooltip styles contrast to achieve accessibility AA Complaince. [#2047](https://github.com/bigcommerce/cornerstone/pull/2047)
Expand Down
35 changes: 28 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,9 @@ 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'));
this.storeInitMessagesForSwatches();

const $form = $('form[data-cart-item-add]', $scope);
const $productOptionsElement = $('[data-product-option-change]', $form);
Expand All @@ -43,10 +45,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 @@ -76,6 +85,16 @@ export default class ProductDetails extends ProductDetailsBase {
this.previewModal = modalFactory('#previewModal')[0];
}

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

setProductVariant() {
const unsatisfiedRequiredFields = [];
const options = [];
Expand Down Expand Up @@ -218,12 +237,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