All files / date-picker/components/DatePicker Presets.jsx

93.75% Statements 15/16
75% Branches 9/12
83.33% Functions 5/6
100% Lines 15/15
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                            1x         1x                                 1x                                                         6x       6x         6x               1x 1x 6x 6x 6x   6x                                     1x                       1x         1x              
import React from 'react';
import PropTypes from 'prop-types';
import styled, { css } from 'styled-components';
 
import {
  Button,
  Text,
} from '@bufferapp/components';
 
import {
  curiousBlue,
  geyser,
} from '@bufferapp/components/style/color';
 
const Header = styled.header`
  margin: 0 0 0.5rem;
  height: auto;
`;
 
const List = styled.ul`
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
 
  &:first-of-type {
    margin-top: 0;
  }
 
  &:last-of-type {
    margin-bottom: 0;
  }
`;
 
const Item = styled.li`
  width: 33%;
  list-style: none;
  cursor: pointer;
  display: inline-block;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  text-decoration: none;
  text-shadow: none;
  border: 1px solid ${geyser};
  border-radius: 3px;
  text-align: center;
  user-select: none;
  margin: 0.1rem;
  white-space: nowrap;
 
  &:first-of-type {
    margin-left: 0;
  }
 
  &:last-of-type {
    margin-right: 0;
  }
 
  > button {
    padding: 0.5rem 0.25rem !important;
  }
 
  ${props => props.inactive && css`
    opacity: 1;
  `}
 
  ${props => props.active && css`
    background: ${curiousBlue};
    border-color: ${curiousBlue};
  `}
 
  ${props => props.disabled && css`
    color: #AAAAAA !important;
    opacity: .3 !important;
    background: transparent !important;
    cursor: not-allowed;
  `}
`;
 
const Presets = ({ presets, selectPreset, startDate, endDate }) => {
  const presetButtons = presets.map((preset) => {
    const dataTip = preset.disabled ? 'We don\'t have complete data for this range.' : null;
    const handleClick = preset.disabled ? null : (() => selectPreset(preset));
    const buttonTextColor = (preset.selected ? 'white' : null);
 
    return (
      <Item
        active={preset.selected}
        inactive={!preset.selected}
        disabled={preset.disabled}
        key={preset.name.toLowerCase().replace(' ', '-')}
        data-tip={dataTip}
      >
        <Button
          noStyle
          fillContainer
          onClick={handleClick}
        >
          <Text size="small" color={buttonTextColor}>{preset.name}</Text>
        </Button>
      </Item>
    );
  });
 
  return (
    <Header>
      <List>
        {presetButtons.slice(0, 3)}
      </List>
      <List>
        {presetButtons.splice(3)}
      </List>
    </Header>
  );
};
 
Presets.defaultProps = {
  startDate: null,
  endDate: null,
};
 
Presets.propTypes = {
  selectPreset: PropTypes.func.isRequired,
  startDate: PropTypes.number,
  endDate: PropTypes.number,
};
 
export default Presets;