Skip to content

Commit 9b5e080

Browse files
Added Prop hideUnavailableVariations that enables hiding unavailable variations from DOM
1 parent 605572f commit 9b5e080

File tree

6 files changed

+25
-1
lines changed

6 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Prop `hideUnavailableVariations` that enables hiding unavailable variations from DOM
13+
1014
## [3.176.2] - 2025-02-03
1115

1216
### Changed

docs/SKUSelector.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The `sku-selector` block is mainly used in Product Details Pages (PDPs) to displ
6464
| `variationsSpacing` | `number` | Defines the `margin-bottom` size to be applied in the rendered product variations. Possible values range from `0` to `11` (the prop value is not in `px`; every value represents a tachyon class). | `7` |
6565
| `visibility` | `enum` | Defines the scenarios in which the SKU selector should be displayed. Possible values are: `always` (it will always be displayed, even if the product has only one SKU option) or `more-than-one` (the SKU selector is only displayed when the product has more than one SKU option). | `always` |
6666
| `visibleVariations` | `array` | Specifies which product variations should be displayed on the product details page. Please note that no variations will be displayed if you declare a name that does not represent a real product variation or an empty array. There is more information regarding this prop structure below this table. | `undefined` |
67+
| `hideUnavailableVariations`|`boolean`| When this prop is set to `true` all unavailable variations will not be rendered.|`false`|
6768

6869
- **`visibleVariations` props**
6970

react/components/SKUSelector/Wrapper.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ interface Props {
170170
sliderArrowSize?: number
171171
sliderItemsPerPage?: ResponsiveValuesTypes.ResponsiveValue<number>
172172
sortVariationsByLabel?: boolean
173+
hideUnavailableVariations?: boolean
173174
/** Used to override default CSS handles */
174175
classes?: CssHandlesTypes.CustomClasses<typeof SKU_SELECTOR_CSS_HANDLES>
175176
}
@@ -262,6 +263,7 @@ function SKUSelectorWrapper(props: Props) {
262263
sliderArrowSize={props.sliderArrowSize}
263264
sliderDisplayThreshold={props.sliderDisplayThreshold}
264265
sortVariationsByLabel={props.sortVariationsByLabel}
266+
hideUnavailableVariations={props.hideUnavailableVariations}
265267
/>
266268
</SKUSelectorCssHandlesProvider>
267269
)

react/components/SKUSelector/components/SKUSelector.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ interface Props {
6868
sliderArrowSize: number
6969
sliderItemsPerPage: ResponsiveValuesTypes.ResponsiveValue<number>
7070
sortVariationsByLabel?: boolean
71+
hideUnavailableVariations?: boolean
7172
}
7273

7374
function isSkuAvailable(item?: SelectorProductItem) {
@@ -371,6 +372,7 @@ function SKUSelector({
371372
sliderArrowSize,
372373
sliderItemsPerPage,
373374
sortVariationsByLabel = false,
375+
hideUnavailableVariations,
374376
}: Props) {
375377
const { handles } = useSKUSelectorCssHandles()
376378
const variationsSpacing = getValidMarginBottom(marginBottomProp)
@@ -465,6 +467,7 @@ function SKUSelector({
465467
sliderDisplayThreshold={sliderDisplayThreshold}
466468
sliderArrowSize={sliderArrowSize}
467469
sliderItemsPerPage={sliderItemsPerPage}
470+
hideUnavailableVariations={hideUnavailableVariations}
468471
/>
469472
)
470473
})}

react/components/SKUSelector/components/Variation.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface Props {
3333
sliderDisplayThreshold: number
3434
sliderArrowSize: number
3535
sliderItemsPerPage: ResponsiveValuesTypes.ResponsiveValue<number>
36+
hideUnavailableVariations?: boolean
3637
}
3738

3839
const ITEMS_VISIBLE_THRESHOLD = 2
@@ -60,8 +61,13 @@ const Variation: FC<Props> = ({
6061
sliderArrowSize,
6162
sliderDisplayThreshold,
6263
sliderItemsPerPage,
64+
hideUnavailableVariations,
6365
}) => {
64-
const { originalName, name, options } = variation
66+
const { originalName, name, options: initialOptions } = variation
67+
68+
const options = hideUnavailableVariations
69+
? initialOptions.filter(option => option.available)
70+
: [...initialOptions]
6571

6672
const visibleItemsWhenCollapsed = maxItems - ITEMS_VISIBLE_THRESHOLD
6773

@@ -88,6 +94,11 @@ const Variation: FC<Props> = ({
8894
)
8995

9096
const showAllAction = useCallback(() => setShowAll(true), [setShowAll])
97+
98+
if (options.length === 0) {
99+
return null
100+
}
101+
91102
const containerClasses = classnames(
92103
'flex flex-column',
93104
containerClassesProp,

react/components/SKUSelector/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ interface Props {
187187
sliderArrowSize?: number
188188
sliderItemsPerPage?: ResponsiveValuesTypes.ResponsiveValue<number>
189189
sortVariationsByLabel?: boolean
190+
hideUnavailableVariations?: boolean
190191
}
191192

192193
const getNewSelectedVariations = (
@@ -254,6 +255,7 @@ const SKUSelectorContainer: FC<Props> = ({
254255
tablet: 2,
255256
phone: 1,
256257
},
258+
hideUnavailableVariations = false,
257259
}) => {
258260
const productContext = useProduct()
259261
const selectedItem = productContext?.selectedItem
@@ -425,6 +427,7 @@ const SKUSelectorContainer: FC<Props> = ({
425427
sliderArrowSize={sliderArrowSize}
426428
sliderItemsPerPage={sliderItemsPerPage}
427429
sortVariationsByLabel={sortVariationsByLabel}
430+
hideUnavailableVariations={hideUnavailableVariations}
428431
/>
429432
)
430433
}

0 commit comments

Comments
 (0)