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 = ({ display, value, isDisabled, disabledHint, isChecked, onChange, }) => ( {display} ); export const CheckboxFilter: FunctionComponent = ({ "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 ( {alwaysShow.map((item) => ( ))} {isExpandible && ( {showWhenExpanded.map((item) => ( ))} )} {isExpandible && ( )} ); };