"use client" import * as React from "react" import { cn } from "../../lib/utils" import { Button } from "../ui/button" import { Input } from "../ui/input" import { useDismissBeforeOverlay } from "../../hooks/use-dismiss-before-overlay" import { augmentColumnsWithCompanionFilters, resolveColumnFilter, type ResolvedColumnFilter, } from "../../lib/column-filter" import type { FilterFieldRuntimeContext } from "../../lib/column-filter-context" import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "../ui/popover" import { OPERATOR_LABELS, FILTER_TEXT_MASK_FORMAT_HINT } from "../../lib/table-properties-types" import type { ActiveFilter } from "../../lib/table-properties-types" import { formatYmdForDisplay } from "../../lib/date-filter" import { FilterDateCalendar } from "./filter-date-calendar" import { FilterDateRangeControl } from "./filter-date-range-control" import { FilterPersonOptionList } from "./filter-person-option-list" import { FilterRangeControl } from "./filter-range-control" import { FilterTextValueInput } from "./filter-text-value-input" import type { ColumnDef } from "./types" // ───────────────────────────────────────────────────────────────────────────── // FilterPill — active filter pill with inline editor popover // (driven by ColumnDef.filter config rather than FILTER_FIELDS) // ───────────────────────────────────────────────────────────────────────────── interface FilterPillProps { filter: ActiveFilter columns: ColumnDef[] fieldContext?: FilterFieldRuntimeContext /** * Whether this pill's editor is the open one. Controlled from the toolbar's * single `openFilterId` rather than per-pill state: two editors open at once * is never a state the user asked for, and it happened whenever an overlay * that Radix would not dismiss (any modal menu) sat between them. */ open?: boolean onOpenChange?: (open: boolean) => void onUpdate: (id: string, patch: Partial) => void onRemove: (id: string) => void /** Optional custom cell renderer for filter option values */ renderOptionValue?: (fieldKey: string, value: string) => React.ReactNode } type FilterOptionItem = { value: string; label: string; node?: React.ReactNode } const DEFAULT_OPERATORS_BY_TYPE: Record = { select: ["is", "is_not"], date: ["is", "is_not"], "date-range": ["is", "is_not"], range: ["is"], } const TEXT_OPERATORS: readonly FilterOperator[] = ["contains", "not_contains"] /** Operators a filter offers; a `contains` pair for free text, `is` for ranges. */ function filterOperators(filterDef: ResolvedColumnFilter): readonly FilterOperator[] { return filterDef.operators ?? DEFAULT_OPERATORS_BY_TYPE[filterDef.type] ?? TEXT_OPERATORS } /** Whether the filter has a value that narrows the table. */ function filterIsActive(filterDef: ResolvedColumnFilter, filter: ActiveFilter): boolean { if (filterDef.type === "date") return Boolean(filter.values[0]) if (filterDef.type === "date-range") return Boolean(filter.values[0] || filter.values[1]) if (filterDef.type === "range") return filter.values.length >= 2 return filter.values.length > 0 } /** The chip's value text: "…" until chosen, then the label, a count, a date or a range. */ function filterValueLabel( filterDef: ResolvedColumnFilter, filter: ActiveFilter, options: FilterOptionItem[], ): string { if (filterDef.type === "select") { if (filter.values.length === 0) return "…" if (filter.values.length === 1) { return options.find(o => o.value === filter.values[0])?.label ?? filter.values[0] } return `${filter.values.length} selected` } if (filterDef.type === "date") { const ymd = filter.values[0] return ymd ? formatYmdForDisplay(ymd) : "…" } if (filterDef.type === "date-range") { const start = filter.values[0] const end = filter.values[1] if (start && end) return `${formatYmdForDisplay(start)} to ${formatYmdForDisplay(end)}` if (start) return formatYmdForDisplay(start) return "…" } if (filterDef.type === "range") { if (filter.values.length >= 2) { return `${filter.values[0]} to ${filter.values[1]}${filterDef.rangeUnit ?? ""}` } return "…" } return filter.values[0] || "…" } interface FilterSelectOptionsProps { options: FilterOptionItem[] selectedValues: string[] /** Column label, for the listbox's accessible name. */ label: string fieldKey: string showSearch: boolean renderOptionValue?: (fieldKey: string, value: string) => React.ReactNode onToggleValue: (value: string) => void } /** Multi-select option list with optional search and a roving tabindex. */ function FilterSelectOptions({ options, selectedValues, label, fieldKey, showSearch, renderOptionValue, onToggleValue, }: FilterSelectOptionsProps) { const [optSearch, setOptSearch] = React.useState("") const deferredOptSearch = React.useDeferredValue(optSearch) /** Roving tabindex over the option list (same pattern as `ViewSegmentedControl`). */ const optionRefs = React.useRef<(HTMLButtonElement | null)[]>([]) const [activeOptionIndex, setActiveOptionIndex] = React.useState(0) React.useEffect(() => { setActiveOptionIndex(0) }, [optSearch]) const moveActiveOption = React.useCallback((index: number, len: number) => { if (len === 0) return const i = ((index % len) + len) % len setActiveOptionIndex(i) requestAnimationFrame(() => { optionRefs.current[i]?.focus() }) }, []) const onOptionKeyDown = React.useCallback( (e: React.KeyboardEvent, index: number, len: number) => { if (e.key === "ArrowDown") { e.preventDefault() moveActiveOption(index + 1, len) } else if (e.key === "ArrowUp") { e.preventDefault() moveActiveOption(index - 1, len) } else if (e.key === "Home") { e.preventDefault() moveActiveOption(0, len) } else if (e.key === "End") { e.preventDefault() moveActiveOption(len - 1, len) } }, [moveActiveOption], ) const needle = deferredOptSearch.toLowerCase() const filteredOpts = deferredOptSearch ? options.filter(o => o.label.toLowerCase().includes(needle)) : options const selectedValueSet = new Set(selectedValues) return (
{showSearch && (
setOptSearch(e.target.value)} className={cn("h-7 text-xs", optSearch ? "pe-8" : "pe-2")} autoFocus /> {optSearch ? (