Guest User

Untitled

a guest
Nov 17th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classNames from 'classnames';
  4.  
  5.  
  6. function extractPricingContent(text, priceTag) {
  7. if (!text) {
  8. return [];
  9. }
  10.  
  11. // how do i do this?
  12. const priceToken = /priceTag/i;
  13. const priceTokenMatched = text.match(priceToken);
  14. const [first, second] = text.split(priceToken);
  15.  
  16. return [first, !!priceTokenMatched, second];
  17. }
  18.  
  19. function DiscountedPriceDisplay(props) {
  20. const { content, regularPrice, discountPrice, priceToken, priceDisplayClassName } = props;
  21. // probably don't want second
  22. const [first, showPriceContent, second] = extractPricingContent(content, priceToken);
  23.  
  24. return (
  25. <h3 className={priceDisplayClassName}>
  26. <span>
  27. {first}
  28. {showPriceContent && (
  29. <span>
  30. <span
  31. className={classNames({
  32. 'u-strikethrough': !!discountPrice,
  33. })}
  34. >
  35. {regularPrice}
  36. </span>
  37. {' '}
  38. {!!discountPrice && <span className="u-highlight">{discountPrice}</span>}
  39. {' '}
  40. </span>
  41. )}
  42. </span>
  43. </h3>
  44. );
  45. }
  46.  
  47. DiscountedPriceDisplay.propTypes = {
  48. content: PropTypes.string,
  49. regularPrice: PropTypes.string,
  50. discountPrice: PropTypes.string,
  51. priceToken: PropTypes.string.isRequired,
  52. priceDisplayClassName: PropTypes.string.isRequired,
  53. };
  54.  
  55. DiscountedPriceDisplay.defaultProps = {
  56. content: '',
  57. regularPrice: '',
  58. discountPrice: '',
  59. };
  60.  
  61. export default DiscountedPriceDisplay;
Add Comment
Please, Sign In to add comment