All files / table filter.js

95% Statements 19/20
93.33% Branches 14/15
77.78% Functions 7/9
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                  5x   5x         5x         5x         5x           5x 1x     4x       4x       5x 1x     4x     5x       4x 1x   3x                     1x                   1x                         5x                              
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
 
import * as React from 'react';
 
import {Button, KIND, SIZE} from '../button/index.js';
import {getOverrides} from '../helpers/overrides.js';
import FilterIcon from '../icon/filter.js';
import {StatefulPopover, PLACEMENT} from '../popover/index.js';
 
import {
  StyledFilterButton,
  StyledFilterContent,
  StyledFilterHeading,
  StyledFilterFooter,
} from './styled-components.js';
import TrapFocus from './trap-focus.js';
import type {FilterProps} from './types.js';
 
export default function Filter(props: FilterProps) {
  const {onSelectAll = () => {}, onReset = () => {}, overrides = {}} = props;
 
  const [MenuButton, menuButtonProps] = getOverrides(
    overrides.MenuButton,
    StyledFilterButton,
  );
 
  const [Content, contentProps] = getOverrides(
    overrides.Content,
    StyledFilterContent,
  );
 
  const [Heading, headingProps] = getOverrides(
    overrides.Heading,
    StyledFilterHeading,
  );
 
  const [Footer, footerProps] = getOverrides(
    overrides.Footer,
    StyledFilterFooter,
  );
 
  function getIconColor(theme) {
    if (props.disabled) {
      return theme.colors.mono500;
    }
 
    Iif (props.active) {
      return theme.colors.colorPrimary;
    }
 
    return theme.colors.tableFilter;
  }
 
  function getIconHoverColor(theme) {
    if (props.disabled || props.active) {
      return null;
    }
 
    return theme.colors.colorPrimary;
  }
 
  return (
    <StatefulPopover
      placement={PLACEMENT.bottom}
      stateReducer={(_, nextState) => {
        if (props.disabled) {
          return {...nextState, isOpen: false};
        }
        return nextState;
      }}
      content={
        <TrapFocus>
          <Heading {...headingProps}>Filter Column</Heading>
          <Content {...contentProps}>{props.children}</Content>
          <Footer {...footerProps}>
            <Button
              kind={KIND.minimal}
              size={SIZE.compact}
              onClick={() => {
                onSelectAll();
              }}
            >
              Select All
            </Button>
 
            <Button
              kind={KIND.minimal}
              size={SIZE.compact}
              onClick={() => {
                onReset();
              }}
            >
              Reset
            </Button>
          </Footer>
        </TrapFocus>
      }
    >
      <MenuButton {...menuButtonProps}>
        <FilterIcon
          overrides={{
            Svg: {
              style: ({$theme}) => ({
                color: getIconColor($theme),
                ':hover': {
                  color: getIconHoverColor($theme),
                  cursor: props.disabled ? null : 'pointer',
                },
              }),
            },
          }}
          size={18}
        />
      </MenuButton>
    </StatefulPopover>
  );
}