Skip to content

Commit

Permalink
fix: compatible with swatch pattern option
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 authored and libruce committed Apr 16, 2024
1 parent 23261ad commit fbfe550
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
44 changes: 30 additions & 14 deletions apps/storefront/src/components/form/B3ControlSwatchRadio.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box } from '@mui/material'
import { Avatar, Box } from '@mui/material'

import B3ControlRectangle from './B3ControlRectangle'
import { ColorContainer } from './styled'
Expand All @@ -9,19 +9,35 @@ export default function B3ControlSwatchRadio(props: Form.B3UIProps) {

const newOptions = options.map((option: Form.SwatchRadioGroupListProps) => ({
...option,
label: (
<ColorContainer>
{(option.colors || []).map((color: string) => (
<Box
className="swatch-color-item"
sx={{
background: `${color}`,
}}
key={color}
/>
))}
</ColorContainer>
),
label:
option?.image && option?.image.data ? (
<Avatar
variant="square"
sx={{
width: '22px',
height: '22px',

'& img': {
width: '22px',
height: '22px',
},
}}
>
<img src={option?.image.data} alt={option?.image.alt} />
</Avatar>
) : (
<ColorContainer>
{(option.colors || []).map((color: string) => (
<Box
className="swatch-color-item"
sx={{
background: `${color}`,
}}
key={color}
/>
))}
</ColorContainer>
),
}))

const labelStyle = {
Expand Down
4 changes: 4 additions & 0 deletions apps/storefront/src/components/form/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace Form {
value: string
label: string
colors?: string[]
image?: {
alt?: string
data?: string
}
}
}

Expand Down
1 change: 1 addition & 0 deletions apps/storefront/src/types/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface ALlOptionValue {
colors?: string[]
product_id?: number
checked_value?: boolean
image_url?: string
} | null
is_default: boolean
adjusters?: {
Expand Down
7 changes: 5 additions & 2 deletions apps/storefront/src/utils/b3Product/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ const getFieldOptions = (
value: item.id,
label: item.label,
image: {
data: productImages[item.value_data?.product_id || ''] || '',
alt: '',
data:
fieldType === 'swatch'
? item.value_data?.image_url
: productImages[item.value_data?.product_id || ''] || '',
alt: fieldType === 'swatch' ? item.label : '',
},
colors: item.value_data?.colors || [],
})
Expand Down

0 comments on commit fbfe550

Please sign in to comment.