Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add sponsored badge option #1098

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Option to display a "Sponsored" tag above product name

## [3.169.5] - 2023-10-05


Expand Down
3 changes: 3 additions & 0 deletions docs/ProductName.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The `product-name` block is responsible for displaying a product name along with
| `showBrandName` | `boolean` | Displays the brand name. | `false`|
| `showProductReference` | `boolean` | Displays the product reference code. | `false`|
| `showSku` | `boolean` | Displays the SKU value. | `false` |
| `showSponsoredBadge` | `boolean` | Displays a sponsored badge above the product's name. | `false` |
| `sponsoredBadgeLabel` | `string` | Text of the sponsored badge if it's rendered. | `""` |
| `tag` | `string` | Defines the HTML tag of the product container. Possible values are: `div`, `h1`, `h2`, `h3`. | `div` |

## Customization
Expand All @@ -66,3 +68,4 @@ To apply CSS customizations in this and other blocks, follow the [Using CSS hand
| `productNameSkuLoader` |
| `productReference` |
| `productSku` |
| `sponsoredBadge` |
13 changes: 13 additions & 0 deletions react/ProductName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import ContentLoader from 'react-content-loader'
import { useCssHandles } from 'vtex.css-handles'
import type { CssHandlesTypes } from 'vtex.css-handles'
import { Link } from 'vtex.render-runtime'
import { IOMessage } from 'vtex.native-types'

const CSS_HANDLES = [
'productNameContainer',
'sponsoredBadge',
'productBrand',
'productSku',
'productReference',
Expand Down Expand Up @@ -47,6 +49,10 @@ type Props = {
showProductReference?: boolean
/** Show brand name */
showBrandName?: boolean
/** Show sponsored badge */
showSponsoredBadge?: boolean
/** Label to display in the sponsored badge */
sponsoredBadgeLabel?: string
/** Classes to be applied to root element */
className?: string
/** Classes to be applied to brandName element */
Expand Down Expand Up @@ -116,6 +122,8 @@ function ProductName({
showSku = false,
showBrandName = false,
showProductReference = false,
showSponsoredBadge = false,
sponsoredBadgeLabel,
tag: Wrapper = 'div',
classes,
name,
Expand Down Expand Up @@ -168,6 +176,11 @@ function ProductName({
className={`${handles.productNameLink} pointer c-link hover-c-link active-c-link no-underline underline-hover`}
linkProps={linkProps}
>
{showSponsoredBadge && (
<span className={`${handles.sponsoredBadge} db c-muted-1 t-mini-s`}>
<IOMessage id={sponsoredBadgeLabel} />
</span>
)}
<span className={`${handles.productBrand} ${brandNameClass ?? ''}`}>
{name} {showBrandName && brandName && `- ${brandName}`}
</span>
Expand Down
9 changes: 9 additions & 0 deletions react/__tests__/components/ProductName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ describe('<ProductName />', () => {
expect(asFragment()).toMatchSnapshot()
})

it('should match the snapshot with Name and Sponsored Badge', () => {
const { asFragment } = renderComponent({
sponsoredBadgeLabel: 'sponsoredBadgeTest',
showSponsoredBadge: true,
})

expect(asFragment()).toMatchSnapshot()
})

it('should match the snapshot with all options', () => {
const { asFragment } = renderComponent({
skuName: 'ProductSkuName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ exports[`<ProductName /> should match the snapshot with Name and SkuName 1`] = `
</DocumentFragment>
`;

exports[`<ProductName /> should match the snapshot with Name and Sponsored Badge 1`] = `
<DocumentFragment>
<h1
class="productNameContainer mv0 t-heading-4"
>
<span
class="sponsoredBadge db c-muted-1 t-mini-s"
>
sponsoredBadgeTest
</span>
<span
class="productBrand "
>
ProductTest
</span>
</h1>
</DocumentFragment>
`;

exports[`<ProductName /> should match the snapshot with all options 1`] = `
<DocumentFragment>
<h1
Expand Down
Loading