|
1 | 1 | import { Meta, Canvas, Story, Controls, Markdown } from '@storybook/blocks'; |
2 | 2 | import meta, { Primary } from './ProductCard.stories'; |
3 | | -import RenderProps from './RenderProps.md?raw' |
| 3 | +import RenderProps from './RenderProps.md?raw'; |
4 | 4 | /* This "?raw" import raw text form the md file */ |
5 | 5 |
|
6 | 6 | <Meta of={meta} /> |
7 | 7 |
|
8 | 8 | # `ProductCard` |
9 | | -A Component to manage Product Card Interactions. |
| 9 | + |
| 10 | +A Component to manage Product Card Interactions. |
10 | 11 |
|
11 | 12 | The Product Card component comes bundled with Constructor tracking and default rendering built-in. |
12 | 13 |
|
13 | 14 | <style> |
14 | | -{` |
| 15 | + {` |
15 | 16 | .docs-story div.innerZoomElementWrapper { |
16 | 17 | max-height: 100vh; |
17 | 18 | } |
18 | 19 | `} |
19 | 20 | </style> |
20 | | -<Canvas sourceState="none"> |
| 21 | + |
| 22 | +<Canvas sourceState='none'> |
21 | 23 | <Story of={Primary} /> |
22 | 24 | </Canvas> |
23 | 25 |
|
24 | | - |
25 | 26 | ## 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 | + |
26 | 33 | ```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'; |
28 | 38 |
|
29 | | -function myApp() { |
30 | | - ... |
31 | | - |
| 39 | +export default function MyComponent() { |
32 | 40 | 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> |
35 | 57 | </CioPlp> |
36 | | - ) |
| 58 | + ); |
37 | 59 | } |
38 | 60 | ``` |
39 | 61 |
|
40 | 62 | ## Render Props pattern |
| 63 | + |
41 | 64 | If you prefer handle the rendering of the ProductCard component, you may pass a Render Props function to the component. |
| 65 | + |
42 | 66 | ```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'; |
44 | 71 |
|
45 | 72 | 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; |
48 | 81 |
|
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 */}</>; |
56 | 85 | } |
57 | 86 |
|
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() { |
64 | 88 | 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> |
69 | 109 | </CioPlp> |
70 | | - ) |
| 110 | + ); |
71 | 111 | } |
72 | 112 | ``` |
73 | 113 |
|
74 | 114 | ### 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. |
76 | 117 |
|
77 | 118 | <Markdown>{RenderProps}</Markdown> |
0 commit comments