1 | import React, { Component } from 'react'
|
2 |
|
3 | export default function withSortedSizes(WrappedComponent) {
|
4 | return class extends Component {
|
5 | sortVariantsBySize = () => {
|
6 | const { variants: inVariants } = this.props
|
7 | const variantFilter = (variant, index, self) => {
|
8 | return index === self.findIndex((v) => (
|
9 | v.id === variant.id
|
10 | ))
|
11 | }
|
12 | const sizeMap = {
|
13 | 'XXS': 20,
|
14 | 'XS': 21,
|
15 | 'S': 22,
|
16 | 'M': 23,
|
17 | 'L': 24,
|
18 | 'XL': 25,
|
19 | 'XXL': 26,
|
20 | 'XS/S': 30,
|
21 | 'M/L': 31
|
22 | }
|
23 | const variantSort = (currentVariant, nextVariant) => {
|
24 | const currentVariantWeight = sizeMap[currentVariant.size.toUpperCase()] || parseInt(currentVariant.size, 10)
|
25 | const nextVariantWeight = sizeMap[nextVariant.size.toUpperCase()] || parseInt(nextVariant.size, 10)
|
26 | return (currentVariantWeight > nextVariantWeight) ? 1 : -1
|
27 | }
|
28 | return inVariants
|
29 | .filter(variantFilter)
|
30 | .sort(variantSort)
|
31 | }
|
32 | render () {
|
33 | const sortedVariants = this.sortVariantsBySize()
|
34 | return <WrappedComponent {...this.props} variants={sortedVariants} />
|
35 | }
|
36 | }
|
37 | }
|
38 |
|
\ | No newline at end of file |