diff --git a/CHANGELOG.md b/CHANGELOG.md index 65507fb0..7f13e896 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- New component called 'product-reference', where is possible to pull an identifier product information. + ## [0.25.0] - 2020-10-27 ### Added diff --git a/docs/README.md b/docs/README.md index 7f128c98..18e79966 100644 --- a/docs/README.md +++ b/docs/README.md @@ -255,6 +255,7 @@ Therefore, in order to customize the `product-list` configuration, you can simpl | `product-list-content-mobile` | Creates the product list layout for mobile devices. | | `message` | Renders a message text about the product availability. | | `product-name` | Renders the product names. | +| `product-reference` | Renders the product reference information. | | `price` | Renders the product prices. | | `unit-price` | Renders the price for each product unit added to the cart. | | `product-list-image` | Renders the product images. | @@ -298,6 +299,13 @@ Therefore, in order to customize the `product-list` configuration, you can simpl | ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | | `width` | `number` | Product image width (in pixels). | `96` | +### `product-reference` props + +| Prop name | Type | Description | Default value | +| :------------------: | :---------: | :---------------------------------: | :-------------: | +| `identifierLabel` | `string` | Text label to be displayed to the left of the product reference value. | `undefined` | +| `identifierOption` | `enum` | Desired product reference data i.e. product identifier to be displayed. Possible options are: `ProductId`, `ProductSkuItemId`, `ProductReferenceId`, and `ProductSkuReferenceId`. | `ProductReferenceId` | + ### `remove-button` props | Prop name | Type | Description | Default value | @@ -326,6 +334,9 @@ In order to apply CSS customizations in this and other blocks, follow the instru | `productPriceCurrency` | | `productPrice` | | `productQuantityLabel` | +| `productIdentifier` | +| `productIdentifierValue` | +| `productIdentifierLabelValue` | | `productVariationsContainer` | | `productVariationsItem` | | `quantityDropdownContainer` | @@ -340,7 +351,6 @@ In order to apply CSS customizations in this and other blocks, follow the instru | `unitPriceMeasurementUnit` | | `unitPricePerUnitCurrency` | - ## Contributors ✨ diff --git a/react/ProductReference.tsx b/react/ProductReference.tsx new file mode 100644 index 00000000..695747f8 --- /dev/null +++ b/react/ProductReference.tsx @@ -0,0 +1,57 @@ +import React, { FunctionComponent, useMemo } from 'react' +import { Loading } from 'vtex.render-runtime' +import { useCssHandles } from 'vtex.css-handles' + +import { useItemContext } from './ItemContext' +import { opaque } from './utils/opaque' +import { IdentifierType } from './constants/Identifiers' + +const CSS_HANDLES = [ + 'productIdentifier', + 'productIdentifierValue', + 'productIdentifierLabelValue', +] as const + +interface Props { + identifierLabel?: string + identifierOption?: string +} + +const ProductReference: FunctionComponent = props => { + const { identifierOption, identifierLabel } = props + const { item, loading } = useItemContext() + const handles = useCssHandles(CSS_HANDLES) + + const identifierValue = useMemo(() => { + if (identifierOption === IdentifierType.PRODUCT_REFERENCE_ID) + return item.productRefId + if (identifierOption === IdentifierType.PRODUCT_SKU_REFERENCE_ID) + return item.refId + if (identifierOption === IdentifierType.PRODUCT_SKU_ITEM_ID) return item.id + if (identifierOption === IdentifierType.PRODUCT_ID) return item.productId + + return item.productRefId + }, [item.productRefId, identifierOption, item.id, item.productId, item.refId]) + + if (loading) return + if (!identifierValue) return null + + return ( +
+ {identifierLabel && ( + + {identifierLabel} + + )} + + {identifierValue} + +
+ ) +} + +export default ProductReference diff --git a/react/constants/Identifiers.ts b/react/constants/Identifiers.ts new file mode 100644 index 00000000..6ac9d0df --- /dev/null +++ b/react/constants/Identifiers.ts @@ -0,0 +1,6 @@ +export const enum IdentifierType { + PRODUCT_ID = 'ProductId', + PRODUCT_SKU_ITEM_ID = 'ProductSkuItemId', + PRODUCT_REFERENCE_ID = 'ProductReferenceId', + PRODUCT_SKU_REFERENCE_ID = 'ProductSkuReferenceId', +} diff --git a/store/interfaces.json b/store/interfaces.json index 0257e566..e975c576 100644 --- a/store/interfaces.json +++ b/store/interfaces.json @@ -39,6 +39,9 @@ } } }, + "product-reference": { + "component": "ProductReference" + }, "product-brand": { "component": "ProductBrand", "preview": {