Skip to content
Merged
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
179 changes: 135 additions & 44 deletions src/stories/components/ProductSwatch/Code Examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,73 @@ A Component to manage Product Swatch interactions and UI.

## Basic Example

```jsx
import React from 'react';
import { CioPlp, ProductSwatch, useProductSwatch } from '@constructor-io/constructorio-ui-plp';

const apiKey = 'MY_API_KEY';
This component must be used within a **ProductCard** as it relies on the ProductCard's context to access product data.

function MyComponent() {
const { swatchList, selectedVariation, selectVariation } = useProductSwatch({ item });
- You will also need to pass the **itemFieldGetter** prop to CioPlp since the ProductSwatch component depends on these getters to extract the correct data from ProductCard.

```jsx
import {
CioPlp,
CioPlpGrid,
ProductCard,
ProductSwatch,
SwatchItem,
utils,
} from "@constructor-io/constructorio-ui-plp";
import "@constructor-io/constructorio-ui-plp/styles.css";

const DEMO_API_KEY = "key_M57QS8SMPdLdLx4x";

export default function MyComponent() {
return (
<>
<CioPlp apiKey={MY_API_KEY}>
<ProductSwatch swatchObject={{ swatchList, selectedVariation, selectVariation }} />
</CioPlp>
</>
<CioPlp
apiKey={DEMO_API_KEY}
itemFieldGetters={{
getPrice: (item) => item.data.price,
getSwatches: (item) => {
const swatchList: SwatchItem[] = [];

item?.variations?.forEach((variation) => {
swatchList.push({
itemName: item?.itemName,
url: item?.data.url,
imageUrl: variation?.imageUrl,
variationId: variation?.data.variationId,
swatchPreview: variation?.imageUrl || "",
});
});

return swatchList;
},
getSwatchPreview: (item) => item.imageUrl || "",
}}
>
<CioPlpGrid>
{(props) => {
if (
utils.isPlpBrowseDataResults(props.data) ||
utils.isPlpSearchDataResults(props.data)
) {
const firstItem = props.data.response.results.find(
(item) => item.variations && item.variations?.length > 1
);
return (
<ProductCard item={firstItem}>
{(productProps) => {
if (productProps.productInfo.productSwatch) {
return (
<ProductSwatch
swatchObject={productProps.productInfo.productSwatch}
/>
);
}
}}
</ProductCard>
);
}
}}
</CioPlpGrid>
</CioPlp>
);
}
```
Expand All @@ -45,47 +97,86 @@ If you prefer handle the rendering of the ProductSwatch component, you may pass
> Example handler:
>
> ```js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsx

> onClickHandler={(event) => {
> onClickHandler = (event) => {
> event.preventDefault();
> }}
> };
>
> <div onClick={onClickHandler}></div>
> <div onClick={onClickHandler}></div>;
> ```

```jsx
import { CioPlp, ProductSwatch } from '@constructor-io/constructorio-ui-plp'

function myApp() {
...
import {
CioPlp,
CioPlpGrid,
ProductCard,
ProductSwatch,
SwatchItem,
utils,
} from "@constructor-io/constructorio-ui-plp";
import "@constructor-io/constructorio-ui-plp/styles.css";

const DEMO_API_KEY = "key_M57QS8SMPdLdLx4x";

function MyCustomSwatch(props) {
const { swatchList, selectedVariation, selectVariation } = props;
return <pre>{JSON.stringify(swatchList, null, 2)}</pre>;
}

export default function MyComponent() {
return (
<CioPlp apiKey={YOUR_API_KEY}>
<ProductSwatch>
{
({
swatchList,
selectedVariation,
selectVariation,
}) => (
<ul>
{swatchList?.map((swatch) => (
<li
key={swatch.variationId}
className={`cio-swatch-item ${
selectedVariation?.variationId === swatch.variationId ? 'cio-swatch-selected' : ''
}`}
onClick={() => selectVariation(swatch)}
style={{
background: swatch?.swatchPreview
<CioPlp
apiKey={DEMO_API_KEY}
itemFieldGetters={{
getPrice: (item) => item.data.price,
getSwatches: (item) => {
const swatchList: SwatchItem[] = [];

item?.variations?.forEach((variation) => {
swatchList.push({
itemName: item?.itemName,
url: item?.data.url,
imageUrl: variation?.imageUrl,
variationId: variation?.data.variationId,
swatchPreview: variation?.imageUrl || "",
});
});

return swatchList;
},
getSwatchPreview: (item) => item.imageUrl || "",
}}
>
<CioPlpGrid>
{(props) => {
if (
utils.isPlpBrowseDataResults(props.data) ||
utils.isPlpSearchDataResults(props.data)
) {
const firstItem = props.data.response.results.find(
(item) => item.variations && item.variations?.length > 1
);
return (
<ProductCard item={firstItem}>
{(productProps) => {
if (productProps.productInfo.productSwatch) {
return (
<ProductSwatch
swatchObject={productProps.productInfo.productSwatch}
>
{(swatchProps) => {
return <MyCustomSwatch {...swatchProps} />;
}}
</ProductSwatch>
);
}
}}
/>
))}
</ul>
)
}
</ProductSwatch>
</ProductCard>
);
}
}}
</CioPlpGrid>
</CioPlp>
)
);
}
```

Expand Down
Loading