|
| 1 | +import classNames from "classnames"; |
| 2 | +import { map } from "lodash"; |
| 3 | +import * as React from "react"; |
| 4 | +import styles from "./FilterList.module.css"; |
| 5 | +import { Filter } from "../../entity/FileFilter"; |
| 6 | + |
| 7 | +interface Props { |
| 8 | + name: string; |
| 9 | + filters: Filter[]; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * UI for displaying the annotation values applied as file filters. Each `FileFilter` within `props.filters` |
| 14 | + * must relate to the same annotation (e.g. Each `FileFilter::name` should be equal). |
| 15 | + * Logic is based on the FilterMedallion component |
| 16 | + */ |
| 17 | +export default function FilterList(props: Props) { |
| 18 | + const { filters, name } = props; |
| 19 | + |
| 20 | + const [expanded, setExpanded] = React.useState(false); |
| 21 | + |
| 22 | + // Determine if filter has reached its max-width and text is overflowing |
| 23 | + // If a filter is no longer overflowing but its "expanded" state is truthy, reset. |
| 24 | + const [overflowing, setOverflowing] = React.useState(false); |
| 25 | + const textRef = React.useRef<HTMLElement | null>(null); |
| 26 | + React.useEffect(() => { |
| 27 | + if (textRef.current) { |
| 28 | + const width = textRef.current.clientWidth; |
| 29 | + const scrollWidth = textRef.current.scrollWidth; |
| 30 | + const isOverflowing = scrollWidth > width; |
| 31 | + setOverflowing(isOverflowing); |
| 32 | + if (!isOverflowing) { |
| 33 | + setExpanded(false); |
| 34 | + } |
| 35 | + } else { |
| 36 | + setOverflowing(false); |
| 37 | + } |
| 38 | + }, [textRef, filters]); |
| 39 | + |
| 40 | + const operator = filters.length > 1 ? "for values of" : "equal to"; |
| 41 | + const valueDisplay = map(filters, (filter) => filter.displayValue).join(", "); |
| 42 | + const display = ` ${operator} ${valueDisplay}`; |
| 43 | + |
| 44 | + return ( |
| 45 | + <span className={styles.filter}> |
| 46 | + <span |
| 47 | + className={classNames(styles.filterText, { |
| 48 | + [styles.expandable]: overflowing, |
| 49 | + [styles.expanded]: expanded, |
| 50 | + })} |
| 51 | + onClick={() => overflowing && setExpanded((prev) => !prev)} |
| 52 | + ref={textRef} |
| 53 | + title={name + display} |
| 54 | + > |
| 55 | + <b>{name}</b> {display} |
| 56 | + </span> |
| 57 | + </span> |
| 58 | + ); |
| 59 | +} |
0 commit comments