withTitle.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. const Wrapper = styled.div`
  5. flex: 1 1 50%;
  6. display: flex;
  7. flex-direction: column;
  8. align-items: stretch;
  9. @media only screen and (max-width: 768px) {
  10. flex-basis: 100%;
  11. }
  12. `;
  13. const Title = styled.h3`
  14. font-size: 24px;
  15. font-weight: 300;
  16. @media only screen and (max-width: 768px) {
  17. font-size: 18px;
  18. }
  19. `;
  20. const Count = styled.span`
  21. font-weight: bold;
  22. border-bottom: 1px dotted #999;
  23. `;
  24. const withTitle = ChartComponent => {
  25. function WithTitle(props) {
  26. return (
  27. <Wrapper>
  28. <Title>
  29. {props.periodText && <Count>{props.data.reduce((sum, view) => sum + view, 0)}</Count>}
  30. {props.periodText ? ` clicks in ${props.periodText}` : props.title}.
  31. </Title>
  32. <ChartComponent {...props} />
  33. </Wrapper>
  34. );
  35. }
  36. WithTitle.propTypes = {
  37. data: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.object])).isRequired,
  38. periodText: PropTypes.string,
  39. title: PropTypes.string,
  40. };
  41. WithTitle.defaultProps = {
  42. title: '',
  43. periodText: '',
  44. };
  45. return WithTitle;
  46. };
  47. export default withTitle;