@@ -141,69 +141,69 @@ const parseOptionNameToDisplayOption =
141141 hideImpossibleCombinations : boolean
142142 disableUnavailableSelectOptions : boolean
143143 } ) =>
144- ( variationValue : {
145- name : string
146- originalName : string
147- } ) : DisplayOption | null => {
148- const isSelected = selectedVariations [ variationName ] === variationValue . name
149- const image = imagesMap ?. [ variationName ] ?. [ variationValue . name ]
150-
151- const newSelectedVariation = clone ( selectedVariations )
152-
153- newSelectedVariation [ variationName ] = isSelected
154- ? null
155- : variationValue . name
156-
157- const possibleItems = findListItemsWithSelectedVariations (
158- skuItems ,
159- newSelectedVariation
160- )
144+ ( variationValue : {
145+ name : string
146+ originalName : string
147+ } ) : DisplayOption | null => {
148+ const isSelected = selectedVariations [ variationName ] === variationValue . name
149+ const image = imagesMap ?. [ variationName ] ?. [ variationValue . name ]
161150
162- if ( possibleItems . length > 0 ) {
163- // This is a valid combination option
164- const [ item ] = possibleItems
165- const callbackFn = onSelectItemMemo ( {
166- name : variationName ,
167- value : variationValue . name ,
168- skuId : item . itemId ,
169- isMainAndImpossible : false ,
170- possibleItems,
171- } )
151+ const newSelectedVariation = clone ( selectedVariations )
172152
173- return {
174- label : variationValue . name ,
175- originalName : variationValue . originalName ,
176- onSelectItem : callbackFn ,
177- image,
178- available : showItemAsAvailable ( {
179- possibleItems,
180- selectedVariations,
181- variationCount,
182- isSelected,
183- } ) ,
184- impossible : false ,
185- disabled : disableUnavailableSelectOptions ,
186- }
187- }
153+ newSelectedVariation [ variationName ] = isSelected
154+ ? null
155+ : variationValue . name
188156
189- if ( ! hideImpossibleCombinations ) {
190- // This is a impossible combination and will only appear if the prop allows.
191- return {
192- label : variationValue . name ,
193- originalName : variationValue . originalName ,
194- // eslint-disable-next-line @typescript-eslint/no-empty-function
195- onSelectItem : ( ) => { } ,
196- image,
197- available : true ,
198- impossible : true ,
199- disabled : disableUnavailableSelectOptions ,
200- }
157+ const possibleItems = findListItemsWithSelectedVariations (
158+ skuItems ,
159+ newSelectedVariation
160+ )
161+
162+ if ( possibleItems . length > 0 ) {
163+ // This is a valid combination option
164+ const [ item ] = possibleItems
165+ const callbackFn = onSelectItemMemo ( {
166+ name : variationName ,
167+ value : variationValue . name ,
168+ skuId : item . itemId ,
169+ isMainAndImpossible : false ,
170+ possibleItems,
171+ } )
172+
173+ return {
174+ label : variationValue . name ,
175+ originalName : variationValue . originalName ,
176+ onSelectItem : callbackFn ,
177+ image,
178+ available : showItemAsAvailable ( {
179+ possibleItems,
180+ selectedVariations,
181+ variationCount,
182+ isSelected,
183+ } ) ,
184+ impossible : false ,
185+ disabled : disableUnavailableSelectOptions ,
201186 }
187+ }
202188
203- // This is a impossible combination and will be hidden.
204- return null
189+ if ( ! hideImpossibleCombinations ) {
190+ // This is a impossible combination and will only appear if the prop allows.
191+ return {
192+ label : variationValue . name ,
193+ originalName : variationValue . originalName ,
194+ // eslint-disable-next-line @typescript-eslint/no-empty-function
195+ onSelectItem : ( ) => { } ,
196+ image,
197+ available : true ,
198+ impossible : true ,
199+ disabled : disableUnavailableSelectOptions ,
200+ }
205201 }
206202
203+ // This is a impossible combination and will be hidden.
204+ return null
205+ }
206+
207207const variationNameToDisplayVariation =
208208 ( {
209209 variations,
@@ -226,32 +226,33 @@ const variationNameToDisplayVariation =
226226 disableUnavailableSelectOptions : boolean
227227 sortVariationsByLabel : boolean
228228 } ) =>
229- ( variationName : string ) : DisplayVariation => {
230- const name = variationName
231- const { values, originalName } = variations [ variationName ]
232- const options = values
233- . map (
234- parseOptionNameToDisplayOption ( {
235- selectedVariations,
236- variationName,
237- skuItems,
238- onSelectItemMemo,
239- imagesMap,
240- variationCount,
241- hideImpossibleCombinations,
242- disableUnavailableSelectOptions,
243- } )
244- )
245- . filter ( Boolean ) as DisplayOption [ ]
246-
247- if ( sortVariationsByLabel ) {
248- options . sort ( ( a , b ) => {
249- return a . label < b . label ? - 1 : a . label > b . label ? 1 : 0 ;
229+ ( variationName : string ) : DisplayVariation => {
230+ const name = variationName
231+ const { values, originalName } = variations [ variationName ]
232+ const options = values
233+ . map (
234+ parseOptionNameToDisplayOption ( {
235+ selectedVariations,
236+ variationName,
237+ skuItems,
238+ onSelectItemMemo,
239+ imagesMap,
240+ variationCount,
241+ hideImpossibleCombinations,
242+ disableUnavailableSelectOptions,
250243 } )
251- }
252- return { name, originalName, options }
244+ )
245+ . filter ( Boolean ) as DisplayOption [ ]
246+
247+ if ( sortVariationsByLabel ) {
248+ options . sort ( ( a , b ) => {
249+ return a . label < b . label ? - 1 : a . label > b . label ? 1 : 0
250+ } )
253251 }
254252
253+ return { name, originalName, options }
254+ }
255+
255256// Parameters are explained on PropTypes
256257const getAvailableVariations = ( {
257258 variations,
@@ -261,7 +262,7 @@ const getAvailableVariations = ({
261262 skuItems,
262263 hideImpossibleCombinations,
263264 disableUnavailableSelectOptions,
264- sortVariationsByLabel
265+ sortVariationsByLabel,
265266} : AvailableVariationParams ) : DisplayVariation [ ] => {
266267 const variationCount = Object . keys ( variations ) . length
267268
@@ -275,7 +276,7 @@ const getAvailableVariations = ({
275276 variationCount,
276277 hideImpossibleCombinations,
277278 disableUnavailableSelectOptions,
278- sortVariationsByLabel
279+ sortVariationsByLabel,
279280 } )
280281 )
281282}
@@ -314,18 +315,18 @@ function SKUSelector({
314315 sliderDisplayThreshold,
315316 sliderArrowSize,
316317 sliderItemsPerPage,
317- sortVariationsByLabel = false
318+ sortVariationsByLabel = false ,
318319} : Props ) {
319320 const { handles } = useSKUSelectorCssHandles ( )
320321 const variationsSpacing = getValidMarginBottom ( marginBottomProp )
321322 const onSelectItemMemo = useCallback (
322323 ( {
323- name,
324- value,
325- skuId,
326- isMainAndImpossible,
327- possibleItems,
328- } : CallbackItem ) =>
324+ name,
325+ value,
326+ skuId,
327+ isMainAndImpossible,
328+ possibleItems,
329+ } : CallbackItem ) =>
329330 ( ) =>
330331 onSelectItem ( {
331332 name,
@@ -346,7 +347,7 @@ function SKUSelector({
346347 skuItems,
347348 hideImpossibleCombinations,
348349 disableUnavailableSelectOptions,
349- sortVariationsByLabel
350+ sortVariationsByLabel,
350351 } ) ,
351352 [
352353 variations ,
@@ -356,7 +357,7 @@ function SKUSelector({
356357 skuItems ,
357358 hideImpossibleCombinations ,
358359 disableUnavailableSelectOptions ,
359- sortVariationsByLabel
360+ sortVariationsByLabel ,
360361 ]
361362 )
362363
0 commit comments