Skip to content

Commit d2a90c8

Browse files
evanyan13Evan Yan
andauthored
[CI-4303] Update Code Examples - ProductCard (#154)
* Update code examples for ProductCard * Update examples * Add instruction --------- Co-authored-by: Evan Yan <evan.yan@constructor.io>
1 parent 40eb056 commit d2a90c8

File tree

1 file changed

+75
-34
lines changed

1 file changed

+75
-34
lines changed
Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,118 @@
11
import { Meta, Canvas, Story, Controls, Markdown } from '@storybook/blocks';
22
import meta, { Primary } from './ProductCard.stories';
3-
import RenderProps from './RenderProps.md?raw'
3+
import RenderProps from './RenderProps.md?raw';
44
/* This "?raw" import raw text form the md file */
55

66
<Meta of={meta} />
77

88
# `ProductCard`
9-
A Component to manage Product Card Interactions.
9+
10+
A Component to manage Product Card Interactions.
1011

1112
The Product Card component comes bundled with Constructor tracking and default rendering built-in.
1213

1314
<style>
14-
{`
15+
{`
1516
.docs-story div.innerZoomElementWrapper {
1617
max-height: 100vh;
1718
}
1819
`}
1920
</style>
20-
<Canvas sourceState="none">
21+
22+
<Canvas sourceState='none'>
2123
<Story of={Primary} />
2224
</Canvas>
2325

24-
2526
## Basic example
27+
28+
To view the examples and see this component in action
29+
30+
- **Search** - append your URL with `?q=shirt`
31+
- **Browse** - append your URL with `/group_id/1035`
32+
2633
```jsx
27-
import { useCioClient, ProductCard, CioPlp } from '@constructor-io/constructorio-ui-plp'
34+
import { CioPlp, CioPlpGrid, ProductCard, utils } from '@constructor-io/constructorio-ui-plp';
35+
import '@constructor-io/constructorio-ui-plp/styles.css';
36+
37+
const DEMO_API_KEY = 'key_M57QS8SMPdLdLx4x';
2838

29-
function myApp() {
30-
...
31-
39+
export default function MyComponent() {
3240
return (
33-
<CioPlp apiKey={YOUR_API_KEY}>
34-
<ProductCard item={item} />
41+
<CioPlp apiKey={DEMO_API_KEY}>
42+
<CioPlpGrid>
43+
{(props) => {
44+
if (
45+
utils.isPlpBrowseDataResults(props.data) ||
46+
utils.isPlpSearchDataResults(props.data)
47+
) {
48+
const firstItem = props.data.response.results[0];
49+
return (
50+
<div className='cio-product-tiles-container'>
51+
<ProductCard item={firstItem} />
52+
</div>
53+
);
54+
}
55+
}}
56+
</CioPlpGrid>
3557
</CioPlp>
36-
)
58+
);
3759
}
3860
```
3961

4062
## Render Props pattern
63+
4164
If you prefer handle the rendering of the ProductCard component, you may pass a Render Props function to the component.
65+
4266
```jsx
43-
import { ProductCardComponent, useOnAddToCart, useOnProductCardClick } from '@constructor-io/constructorio-ui-plp'
67+
import { CioPlp, CioPlpGrid, ProductCard, utils } from '@constructor-io/constructorio-ui-plp';
68+
import '@constructor-io/constructorio-ui-plp/styles.css';
69+
70+
const DEMO_API_KEY = 'key_M57QS8SMPdLdLx4x';
4471

4572
function MyCustomProductCard(props) {
46-
const { onClick, formatPrice, productInfo, onAddToCart, item } = props
47-
const price = productInfo.itemPrice
73+
const {
74+
item,
75+
productInfo,
76+
formatPrice,
77+
onAddToCart,
78+
onClick,
79+
productCardCnstrcDataAttributes: cnstrcData,
80+
} = props;
4881

49-
return (
50-
<div class="cio-product-card" onClick={(e) => onClick(e, item)}>
51-
<div class="title">{item.itemName}</div>
52-
<div class="price">{formatPrice(price)}</div>
53-
<button type="button" onClick={(e) => onAddToCart(e, item)}>Add To Cart</button>
54-
</div>
55-
)
82+
const { productSwatch, itemName, itemPrice, itemImageUrl, itemUrl } = productInfo;
83+
84+
return <>{/* CUSTOM_MARKUP_HERE */}</>;
5685
}
5786

58-
function myApp() {
59-
const myCustomAddToCart = () => console.log('Added to cart')
60-
const myCustomProductCardClick = () => console.log('Product Card Clicked')
61-
const myCustomCallbacks = { myCustomAddToCart, myCustomProductCardClick }
62-
...
63-
87+
export default function MyComponent() {
6488
return (
65-
<CioPlp apiKey={YOUR_API_KEY} callbacks={myCustomCallbacks}>
66-
<ProductCard>
67-
<MyCustomProductCard />
68-
</ProductCard>
89+
<CioPlp apiKey={DEMO_API_KEY}>
90+
<CioPlpGrid>
91+
{(props) => {
92+
if (
93+
utils.isPlpBrowseDataResults(props.data) ||
94+
utils.isPlpSearchDataResults(props.data)
95+
) {
96+
const firstItem = props.data.response.results[0];
97+
return (
98+
<div className='cio-product-tiles-container'>
99+
<ProductCard item={firstItem}>
100+
{(productCardProps) => {
101+
return <MyCustomProductCard {...productCardProps} />;
102+
}}
103+
</ProductCard>
104+
</div>
105+
);
106+
}
107+
}}
108+
</CioPlpGrid>
69109
</CioPlp>
70-
)
110+
);
71111
}
72112
```
73113

74114
### Arguments passed to children via Render Props
75-
>Accessible to children of the ProductCard component via render props.
115+
116+
> Accessible to children of the ProductCard component via render props.
76117
77118
<Markdown>{RenderProps}</Markdown>

0 commit comments

Comments
 (0)