Skip to content

Commit d97466f

Browse files
evanyan13Evan Yan
andauthored
[CI-4381] Fix clicking on ProductSwatch initiating a redirect (#156)
* Add event handling in productswatch * Add note in storybook docs * Update comment * Update position of event modifiers * Move event modifer to swatch container * Change position of handler * Update implementation * Update storybook --------- Co-authored-by: Evan Yan <evan.yan@constructor.io>
1 parent eb2d91a commit d97466f

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

src/components/ProductSwatch/ProductSwatch.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
.cio-swatch-container {
2828
display: flex;
2929
flex-wrap: wrap;
30-
padding: 0;
30+
padding: 16px;
3131
gap: 10px;
32+
margin: auto;
33+
cursor: default;
3234
}

src/components/ProductSwatch/ProductSwatch.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export default function ProductSwatch(props: ProductSwatchProps) {
3131
}
3232
};
3333

34+
const swatchContainerClickHandler = (e: React.MouseEvent) => {
35+
e.preventDefault();
36+
};
37+
3438
return (
3539
<>
3640
{typeof children === 'function' ? (
@@ -40,7 +44,8 @@ export default function ProductSwatch(props: ProductSwatchProps) {
4044
selectedVariation,
4145
})
4246
) : (
43-
<div>
47+
/* eslint-disable jsx-a11y/no-static-element-interactions */
48+
<div onClick={swatchContainerClickHandler}>
4449
<ul className='cio-swatch-container'>
4550
{swatchList?.map((swatch) => {
4651
const isSelected = selectedVariation?.variationId === swatch.variationId;

src/stories/components/ProductSwatch/Code Examples.mdx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
11
import { Meta, Canvas, Markdown } from '@storybook/blocks';
22
import * as ProductSwatchStories from './ProductSwatch.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={ProductSwatchStories} />
77

88
# `ProductSwatch`
9+
910
A Component to manage Product Swatch interactions and UI.
1011

1112
<Canvas of={ProductSwatchStories.Primary} />
1213

1314
{/* Todo: Update examples */}
15+
1416
## Basic Example
15-
```jsx
16-
import React from 'react';
17-
import { CioPlp, ProductSwatch, useProductSwatch } from '@constructor-io/constructorio-ui-plp';
1817

19-
const apiKey = 'MY_API_KEY'
18+
```jsx
19+
import React from 'react';
20+
import { CioPlp, ProductSwatch, useProductSwatch } from '@constructor-io/constructorio-ui-plp';
2021

21-
function MyComponent() {
22-
const {swatchList, selectedVariation, selectVariation} = useProductSwatch({ item });
22+
const apiKey = 'MY_API_KEY';
2323

24-
return (
25-
<>
26-
<CioPlp apiKey={MY_API_KEY}>
27-
<ProductSwatch swatchObject={{ swatchList, selectedVariation, selectVariation }} />
28-
</CioPlp>
29-
</>
30-
);
31-
}
32-
```
24+
function MyComponent() {
25+
const { swatchList, selectedVariation, selectVariation } = useProductSwatch({ item });
3326

27+
return (
28+
<>
29+
<CioPlp apiKey={MY_API_KEY}>
30+
<ProductSwatch swatchObject={{ swatchList, selectedVariation, selectVariation }} />
31+
</CioPlp>
32+
</>
33+
);
34+
}
35+
```
3436

3537
## Render Props pattern
38+
3639
If you prefer handle the rendering of the ProductSwatch component, you may pass a Render Props function to the component.
40+
41+
> If your `ProductSwatch` is nested within a clickable parent component (eg. `ProductCard`), we recommend adding the following event modifiers to your container:
42+
>
43+
> - `event.preventDefault()` - Prevents default browser behaviors like link navigation
44+
>
45+
> Example handler:
46+
>
47+
> ```js
48+
> onClickHandler={(event) => {
49+
> event.preventDefault();
50+
> }}
51+
>
52+
> <div onClick={onClickHandler}></div>
53+
> ```
54+
3755
```jsx
3856
import { CioPlp, ProductSwatch } from '@constructor-io/constructorio-ui-plp'
3957
4058
function myApp() {
4159
...
42-
60+
4361
return (
4462
<CioPlp apiKey={YOUR_API_KEY}>
4563
<ProductSwatch>
@@ -72,7 +90,9 @@ function myApp() {
7290
```
7391
7492
### Arguments passed to children via Render Props
75-
>Accessible to children of the ProductSwatch component via render props.
93+
94+
> Accessible to children of the ProductSwatch component via render props.
7695
7796
`ProductSwatch`
97+
7898
<Markdown>{RenderProps}</Markdown>

0 commit comments

Comments
 (0)