This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
index.tsx
328 lines (316 loc) · 7.4 KB
/
index.tsx
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
import classNames from 'classnames';
import { formatPrice } from '@woocommerce/price-format';
import { createInterpolateElement } from '@wordpress/element';
import type { Currency } from '@woocommerce/types';
/**
* Internal dependencies
*/
import './style.scss';
interface PriceRangeProps {
/**
* Currency configuration object
*/
currency: Currency | Record< string, never > | undefined;
/**
* The maximum price for the range
*/
maxPrice: string | number;
/**
* The minimum price for the range
*/
minPrice: string | number;
/**
* CSS class applied to each of the elements containing the prices
*
* **Note:** this excludes the dash in between the elements
*/
priceClassName?: string | undefined;
/**
* Any custom style to be applied to each of the elements containing the prices
*
* **Note:** this excludes the dash in between the elements
*/
priceStyle?: React.CSSProperties | undefined;
}
const PriceRange = ( {
currency,
maxPrice,
minPrice,
priceClassName,
priceStyle = {},
}: PriceRangeProps ) => {
return (
<>
<span className="screen-reader-text">
{ sprintf(
/* translators: %1$s min price, %2$s max price */
__(
'Price between %1$s and %2$s',
'woo-gutenberg-products-block'
),
formatPrice( minPrice ),
formatPrice( maxPrice )
) }
</span>
<span aria-hidden={ true }>
<FormattedMonetaryAmount
className={ classNames(
'wc-block-components-product-price__value',
priceClassName
) }
currency={ currency }
value={ minPrice }
style={ priceStyle }
/>
—
<FormattedMonetaryAmount
className={ classNames(
'wc-block-components-product-price__value',
priceClassName
) }
currency={ currency }
value={ maxPrice }
style={ priceStyle }
/>
</span>
</>
);
};
interface SalePriceProps {
/**
* Currency configuration object
*/
currency: Currency | Record< string, never > | undefined;
/**
* CSS class to be applied to the regular price container
*
* i.e. `<del>` element
*/
regularPriceClassName?: string | undefined;
/**
* Custom style to be applied to the regular price container
*
* i.e. `<del>` element
*/
regularPriceStyle?: React.CSSProperties | undefined;
/**
* The regular price before the sale
*/
regularPrice: number | string;
/**
* CSS class to be applied to the sale price container
*
* i.e. `<ins>` element
*/
priceClassName?: string | undefined;
/**
* Custom style to be applied to the regular price container
*
* i.e. `<ins>` element
*/
priceStyle?: React.CSSProperties | undefined;
/**
* The new price during the sale
*/
price: number | string | undefined;
}
const SalePrice = ( {
currency,
regularPriceClassName,
regularPriceStyle,
regularPrice,
priceClassName,
priceStyle,
price,
}: SalePriceProps ) => {
return (
<>
<span className="screen-reader-text">
{ __( 'Previous price:', 'woo-gutenberg-products-block' ) }
</span>
<FormattedMonetaryAmount
currency={ currency }
renderText={ ( value ) => (
<del
className={ classNames(
'wc-block-components-product-price__regular',
regularPriceClassName
) }
style={ regularPriceStyle }
>
{ value }
</del>
) }
value={ regularPrice }
/>
<span className="screen-reader-text">
{ __( 'Discounted price:', 'woo-gutenberg-products-block' ) }
</span>
<FormattedMonetaryAmount
currency={ currency }
renderText={ ( value ) => (
<ins
className={ classNames(
'wc-block-components-product-price__value',
'is-discounted',
priceClassName
) }
style={ priceStyle }
>
{ value }
</ins>
) }
value={ price }
/>
</>
);
};
export interface ProductPriceProps {
/**
* Where to align the wrapper
*
* Applies the `wc-block-components-product-price--align-${ align }` utility
* class to the wrapper.
*/
align?: 'left' | 'center' | 'right' | undefined;
/**
* CSS class for the wrapper
*/
className?: string | undefined;
/**
* Currency configuration object
*/
currency?: Currency | Record< string, never >;
/**
* The string version of the element to use for the price interpolation
*
* **Note:** It should contain `<price/>` (which is also the default value)
*/
format?: string;
/**
* The current price
*/
price?: number | string;
/**
* CSS class for the current price wrapper
*/
priceClassName?: string;
/**
* Custom style for the current price
*/
priceStyle?: React.CSSProperties | undefined;
/**
* The maximum price in a range
*
* If both `maxPrice` and `minPrice` are set, the component will be rendered
* as a `PriceRange` component, otherwise, this value will be ignored.
*/
maxPrice?: number | string | undefined;
/**
* The minimum price in a range
*
* If both `maxPrice` and `minPrice` are set, the component will be rendered
* as a `PriceRange` component, otherwise, this value will be ignored.
*/
minPrice?: number | string | undefined;
/**
* The regular price if the item is currently on sale
*
* If this property exists and is different from the current price, then the
* component will be rendered as a `SalePrice` component.
*/
regularPrice?: number | string | undefined;
/**
* CSS class to apply to the regular price wrapper
*/
regularPriceClassName?: string | undefined;
/**
* Custom style to apply to the regular price wrapper.
*/
regularPriceStyle?: React.CSSProperties | undefined;
}
const ProductPrice = ( {
align,
className,
currency,
format = '<price/>',
maxPrice,
minPrice,
price,
priceClassName,
priceStyle,
regularPrice,
regularPriceClassName,
regularPriceStyle,
}: ProductPriceProps ): JSX.Element => {
const wrapperClassName = classNames(
className,
'price',
'wc-block-components-product-price',
{
[ `wc-block-components-product-price--align-${ align }` ]: align,
}
);
if ( ! format.includes( '<price/>' ) ) {
format = '<price/>';
// eslint-disable-next-line no-console
console.error( 'Price formats need to include the `<price/>` tag.' );
}
const isDiscounted = regularPrice && price !== regularPrice;
let priceComponent = (
<span
className={ classNames(
'wc-block-components-product-price__value',
priceClassName
) }
/>
);
if ( isDiscounted ) {
priceComponent = (
<SalePrice
currency={ currency }
price={ price }
priceClassName={ priceClassName }
priceStyle={ priceStyle }
regularPrice={ regularPrice }
regularPriceClassName={ regularPriceClassName }
regularPriceStyle={ regularPriceStyle }
/>
);
} else if ( minPrice !== undefined && maxPrice !== undefined ) {
priceComponent = (
<PriceRange
currency={ currency }
maxPrice={ maxPrice }
minPrice={ minPrice }
priceClassName={ priceClassName }
priceStyle={ priceStyle }
/>
);
} else if ( price ) {
priceComponent = (
<FormattedMonetaryAmount
className={ classNames(
'wc-block-components-product-price__value',
priceClassName
) }
currency={ currency }
value={ price }
style={ priceStyle }
/>
);
}
return (
<span className={ wrapperClassName }>
{ createInterpolateElement( format, {
price: priceComponent,
} ) }
</span>
);
};
export default ProductPrice;