-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathlist-price.jsx
More file actions
75 lines (71 loc) · 2.53 KB
/
list-price.jsx
File metadata and controls
75 lines (71 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import PropTypes from 'prop-types'
import {Text, VisuallyHidden} from '@salesforce/retail-react-app/app/components/shared/ui'
import {useIntl} from 'react-intl'
import msg from '@salesforce/retail-react-app/app/components/display-price/messages'
/**
* Component that displays list price of a product with a11y
* @param currency - currency
* @param price - price of the product
* @param as - an HTML tag or component to be rendered as
* @param isRange - show price as range or not
* @param labelForA11y - label to be used for a11y
* @param extraProps - extra props to be passed into Text Component
* @returns {JSX.Element}
*/
const ListPrice = ({labelForA11y, price, isRange = false, as = 's', currency, ...extraProps}) => {
const intl = useIntl()
const listPriceText = intl.formatNumber(price, {
style: 'currency',
currency
})
return (
<>
{isRange ? (
<Text
as={as}
{...extraProps}
aria-label={intl.formatMessage(msg.ariaLabelListPriceWithRange, {
listPrice: listPriceText || ''
})}
color="gray.700"
>
{listPriceText}
</Text>
) : (
<Text
as={as}
{...extraProps}
aria-label={intl.formatMessage(msg.ariaLabelListPrice, {
listPrice: listPriceText || ''
})}
color="gray.700"
>
{listPriceText}
</Text>
)}
{/*For screen reader, we want to make sure the product name is announced before the price to avoid confusion*/}
<VisuallyHidden aria-live="polite" aria-atomic={true}>
{labelForA11y}
{intl.formatMessage(msg.ariaLabelListPrice, {
listPrice: listPriceText || ''
})}
</VisuallyHidden>
</>
)
}
ListPrice.propTypes = {
price: PropTypes.number.isRequired,
currency: PropTypes.string.isRequired,
labelForA11y: PropTypes.string,
as: PropTypes.string,
isRange: PropTypes.bool,
extraProps: PropTypes.object
}
export default ListPrice