All files / src/views/Search CheckboxFilter.tsx

11.11% Statements 2/18
0% Branches 0/12
0% Functions 0/6
11.76% Lines 2/17

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 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                                                                                                        1x                                                               1x                                                                                                                                                                
import { ChevronDownIcon, ChevronUpIcon } from "@chakra-ui/icons";
import {
  Flex,
  Button,
  Collapse,
  Checkbox,
  Stack,
  Text,
  Tooltip,
  useDisclosure,
} from "@chakra-ui/react";
import type { FunctionComponent } from "react";
import { SEARCH_ANALYTICS } from "./constants";
import { FilterHeading, FilterHeadingProps } from "./FilterHeading";
import testIds from "./testIds";
import { clickEvent, eventName, useAnalytics } from "../../contexts/Analytics";
 
interface CheckboxOption {
  display: string;
  value: string;
  isDisabled?: boolean;
  disabledHint?: string;
}
 
interface CheckboxItemProps extends CheckboxOption {
  onChange: () => void;
  isChecked: boolean;
}
 
export interface CheckboxFilterProps extends FilterHeadingProps {
  /**
   * Test ID to select checkbox in tests
   */
  "data-testid"?: string;
  /**
   * Number of items that can be initially shown
   */
  initialItemCount?: number;
  /**
   * Defines checkbox items
   */
  options: CheckboxOption[];
  /**
   * Selected values
   */
  values: string[];
  /**
   * Callback triggered when an item is clicked
   */
  onValueChange: (value: string) => void;
}
 
const CheckboxItem: FunctionComponent<CheckboxItemProps> = ({
  display,
  value,
  isDisabled,
  disabledHint,
  isChecked,
  onChange,
}) => (
  <Checkbox
    isChecked={isChecked}
    isDisabled={isDisabled}
    key={value}
    onChange={onChange}
  >
    <Tooltip
      hasArrow
      isDisabled={!isDisabled && !disabledHint}
      label={disabledHint}
      placement="right"
    >
      <Text
        color="textTertiary"
        data-testid={testIds.filterItem}
        data-value={value}
        isTruncated
      >
        {display}
      </Text>
    </Tooltip>
  </Checkbox>
);
 
export const CheckboxFilter: FunctionComponent<CheckboxFilterProps> = ({
  "data-testid": dataTestid,
  initialItemCount,
  hint,
  name,
  options,
  values: checkedValues,
  onValueChange,
}) => {
  const collapse = useDisclosure();
  const { trackCustomEvent } = useAnalytics();
 
  const getOnChange = (item: CheckboxOption) => () => {
    trackCustomEvent(
      clickEvent({
        name: eventName(SEARCH_ANALYTICS.FILTERS, name, "Filter", item.display),
      })
    );
    onValueChange(item.value);
  };
 
  let alwaysShow: typeof options = options;
  let showWhenExpanded: typeof options = [];
 
  if (initialItemCount) {
    alwaysShow = options.slice(0, initialItemCount);
    showWhenExpanded = options.slice(initialItemCount, options.length);
  }
 
  const isExpandible = showWhenExpanded.length > 0;
 
  return (
    <Flex data-testid={dataTestid} direction="column">
      <FilterHeading hint={hint} name={name} />
      <Stack mt={1} spacing={1}>
        {alwaysShow.map((item) => (
          <CheckboxItem
            {...item}
            isChecked={checkedValues.includes(item.value)}
            key={item.value}
            onChange={getOnChange(item)}
          />
        ))}
        {isExpandible && (
          <Collapse animateOpacity in={collapse.isOpen} unmountOnExit>
            <Stack spacing={1}>
              {showWhenExpanded.map((item) => (
                <CheckboxItem
                  {...item}
                  isChecked={checkedValues.includes(item.value)}
                  key={item.value}
                  onChange={getOnChange(item)}
                />
              ))}
            </Stack>
          </Collapse>
        )}
      </Stack>
      {isExpandible && (
        <Flex align="start" mt={1}>
          <Button
            color="textTertiary"
            data-event={eventName(SEARCH_ANALYTICS.FILTERS, name, "Show More")}
            fontWeight="normal"
            leftIcon={collapse.isOpen ? <ChevronUpIcon /> : <ChevronDownIcon />}
            onClick={collapse.onToggle}
            size="sm"
            textAlign="left"
            variant="link"
            w="auto"
          >
            {collapse.isOpen
              ? `Show fewer options (${alwaysShow.length})`
              : `Show more options (${showWhenExpanded.length})`}
          </Button>
        </Flex>
      )}
    </Flex>
  );
};