feat: add text filter on org member list (#17632)
* feat: improve text filters (WIP) * move function to bottom * apply some styles * fix selection of TextFilterOptions * rename value to operand * remove unused file * merge filters/filters into filters/utils * fix regression of not putting url params correctly * move makeWhereClause to filters/utils * fix negative, empty, and not empty operators * fix initial filtering from search state (url) * fix type errors * do not send an empty array to query * update yarn.lock * i18n for text filter operators * extract logic as useColumnFilters() * add missing import * fix type error * revert yarn.lock * use i18n * insensitive text match * move data-table to @calcom/features * fix type errors * fix type errors * fix type errors --------- Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
ff18b9a733
commit
1404581f78
28 files changed
+520
-309
No files matched your search
@@ -1,176 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { Row } from "@tanstack/react-table";
|
||||
import { flexRender } from "@tanstack/react-table";
|
||||
import type { Table as ReactTableType } from "@tanstack/react-table";
|
||||
import { useMemo } from "react";
|
||||
import { useVirtual } from "react-virtual";
|
||||
|
||||
import classNames from "@calcom/lib/classNames";
|
||||
|
||||
import { Icon } from "../icon";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../table/TableNew";
|
||||
|
||||
export interface DataTableProps<TData, TValue> {
|
||||
table: ReactTableType<TData>;
|
||||
tableContainerRef: React.RefObject<HTMLDivElement>;
|
||||
isPending?: boolean;
|
||||
onRowMouseclick?: (row: Row<TData>) => void;
|
||||
onScroll?: (e: React.UIEvent<HTMLDivElement, UIEvent>) => void;
|
||||
tableOverlay?: React.ReactNode;
|
||||
variant?: "default" | "compact";
|
||||
"data-testid"?: string;
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
export function DataTable<TData, TValue>({
|
||||
table,
|
||||
tableContainerRef,
|
||||
isPending,
|
||||
variant,
|
||||
onRowMouseclick,
|
||||
onScroll,
|
||||
children,
|
||||
...rest
|
||||
}: DataTableProps<TData, TValue> & React.ComponentPropsWithoutRef<"div">) {
|
||||
const { rows } = table.getRowModel();
|
||||
|
||||
const rowVirtualizer = useVirtual({
|
||||
parentRef: tableContainerRef,
|
||||
size: rows.length,
|
||||
overscan: 10,
|
||||
});
|
||||
const { virtualItems: virtualRows, totalSize } = rowVirtualizer;
|
||||
const paddingTop = virtualRows.length > 0 ? virtualRows?.[0]?.start || 0 : 0;
|
||||
const paddingBottom =
|
||||
virtualRows.length > 0 ? totalSize - (virtualRows?.[virtualRows.length - 1]?.end || 0) : 0;
|
||||
|
||||
const columnSizeVars = useMemo(() => {
|
||||
const headers = table.getFlatHeaders();
|
||||
const colSizes: { [key: string]: number } = {};
|
||||
for (let i = 0; i < headers.length; i++) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const header = headers[i]!;
|
||||
colSizes[`--header-${header.id}-size`] = header.getSize();
|
||||
colSizes[`--col-${header.column.id}-size`] = header.column.getSize();
|
||||
}
|
||||
return colSizes;
|
||||
}, [table.getState().columnSizingInfo, table.getState().columnSizing]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames(
|
||||
"grid h-[85dvh]", // Set a fixed height for the container
|
||||
rest.className
|
||||
)}
|
||||
style={{
|
||||
gridTemplateRows: "auto 1fr auto",
|
||||
gridTemplateAreas: "'header' 'body' 'footer'",
|
||||
...rest.style,
|
||||
...columnSizeVars,
|
||||
}}
|
||||
data-testid={rest["data-testid"] ?? "data-table"}>
|
||||
<div
|
||||
ref={tableContainerRef}
|
||||
onScroll={onScroll}
|
||||
className="scrollbar-thin border-subtle relative h-full overflow-auto rounded-md border"
|
||||
style={{ gridArea: "body" }}>
|
||||
<Table className="border-0">
|
||||
<TableHeader className="bg-subtle sticky top-0 z-10">
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id}>
|
||||
{headerGroup.headers.map((header) => {
|
||||
const meta = header.column.columnDef.meta;
|
||||
return (
|
||||
<TableHead
|
||||
key={header.id}
|
||||
style={{
|
||||
...(meta?.sticky?.position === "left" && { left: `${meta.sticky.gap || 0}px` }),
|
||||
...(meta?.sticky?.position === "right" && { right: `${meta.sticky.gap || 0}px` }),
|
||||
width: `calc(var(--header-${header?.id}-size) * 1px)`,
|
||||
}}
|
||||
className={classNames(
|
||||
header.column.getCanSort() ? "cursor-pointer select-none" : "",
|
||||
meta?.sticky && "bg-subtle sticky top-0 z-20"
|
||||
)}>
|
||||
<div className="flex items-center" onClick={header.column.getToggleSortingHandler()}>
|
||||
{header.isPlaceholder
|
||||
? null
|
||||
: flexRender(header.column.columnDef.header, header.getContext())}
|
||||
{header.column.getIsSorted() && (
|
||||
<Icon
|
||||
name="arrow-up"
|
||||
className="ml-2 h-4 w-4"
|
||||
style={{
|
||||
transform:
|
||||
header.column.getIsSorted() === "asc" ? "rotate(0deg)" : "rotate(180deg)",
|
||||
transition: "transform 0.2s ease-in-out",
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</TableHead>
|
||||
);
|
||||
})}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{paddingTop > 0 && (
|
||||
<tr>
|
||||
<td style={{ height: `${paddingTop}px` }} />
|
||||
</tr>
|
||||
)}
|
||||
{virtualRows && !isPending ? (
|
||||
virtualRows.map((virtualRow) => {
|
||||
const row = rows[virtualRow.index] as Row<TData>;
|
||||
return (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
onClick={() => onRowMouseclick && onRowMouseclick(row)}
|
||||
className={classNames(
|
||||
onRowMouseclick && "hover:cursor-pointer",
|
||||
variant === "compact" && "!border-0",
|
||||
"group"
|
||||
)}>
|
||||
{row.getVisibleCells().map((cell) => {
|
||||
const column = table.getColumn(cell.column.id);
|
||||
const meta = column?.columnDef.meta;
|
||||
return (
|
||||
<TableCell
|
||||
key={cell.id}
|
||||
style={{
|
||||
...(meta?.sticky?.position === "left" && { left: `${meta.sticky.gap || 0}px` }),
|
||||
...(meta?.sticky?.position === "right" && { right: `${meta.sticky.gap || 0}px` }),
|
||||
width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
|
||||
}}
|
||||
className={classNames(
|
||||
variant === "compact" && "p-1.5",
|
||||
meta?.sticky && "group-hover:bg-muted bg-default sticky"
|
||||
)}>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</TableCell>
|
||||
);
|
||||
})}
|
||||
</TableRow>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={table.getAllColumns().length} className="h-24 text-center">
|
||||
No results.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{paddingBottom > 0 && (
|
||||
<tr>
|
||||
<td style={{ height: `${paddingBottom}px` }} />
|
||||
</tr>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { Column } from "@tanstack/react-table";
|
||||
import { useState } from "react";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
|
||||
import { Badge } from "../badge";
|
||||
import { Button } from "../button";
|
||||
import {
|
||||
Command,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
CommandSeparator,
|
||||
} from "../command";
|
||||
import type { IconName } from "../icon";
|
||||
import { Icon } from "../icon";
|
||||
import { Popover, PopoverContent, PopoverTrigger } from "../popover";
|
||||
|
||||
interface FilterOption {
|
||||
label: string;
|
||||
value: string;
|
||||
icon?: IconName;
|
||||
options?: FilterOption[];
|
||||
}
|
||||
|
||||
interface DataTableFilter<TData, TValue> {
|
||||
column?: Column<TData, TValue>;
|
||||
title?: string;
|
||||
options: FilterOption[];
|
||||
}
|
||||
|
||||
export function DataTableFilter<TData, TValue>({ column, title, options }: DataTableFilter<TData, TValue>) {
|
||||
const facets = column?.getFacetedUniqueValues();
|
||||
const selectedValues = new Set(column?.getFilterValue() as string[]);
|
||||
const [currentOptions, setCurrentOptions] = useState<FilterOption[]>(options);
|
||||
const [navigationPath, setNavigationPath] = useState<string[]>([]);
|
||||
|
||||
const flattenOptions = (opts: FilterOption[]): FilterOption[] => {
|
||||
return opts.reduce((acc, option) => {
|
||||
acc.push(option);
|
||||
if (option.options) {
|
||||
acc.push(...flattenOptions(option.options));
|
||||
}
|
||||
return acc;
|
||||
}, [] as FilterOption[]);
|
||||
};
|
||||
|
||||
const allOptions = flattenOptions(options);
|
||||
|
||||
const handleSelect = (option: FilterOption) => {
|
||||
if (option.options) {
|
||||
setCurrentOptions(option.options);
|
||||
setNavigationPath([...navigationPath, option.label]);
|
||||
} else {
|
||||
if (selectedValues.has(option.value)) {
|
||||
selectedValues.delete(option.value);
|
||||
} else {
|
||||
selectedValues.add(option.value);
|
||||
}
|
||||
const filterValues = Array.from(selectedValues);
|
||||
column?.setFilterValue(filterValues.length ? filterValues : undefined);
|
||||
}
|
||||
};
|
||||
|
||||
const handleBack = () => {
|
||||
if (navigationPath.length > 0) {
|
||||
const newPath = [...navigationPath];
|
||||
newPath.pop();
|
||||
setNavigationPath(newPath);
|
||||
|
||||
let newOptions = options;
|
||||
for (const label of newPath) {
|
||||
const found = newOptions.find((o) => o.label === label);
|
||||
if (found && found.options) {
|
||||
newOptions = found.options;
|
||||
}
|
||||
}
|
||||
setCurrentOptions(newOptions);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button color="secondary" size="sm" className="border-subtle h-8 rounded-md">
|
||||
<Icon name="filter" className="mr-2 h-4 w-4" />
|
||||
{title}
|
||||
{selectedValues?.size > 0 && (
|
||||
<>
|
||||
<div className="ml-2 hidden space-x-1 md:flex">
|
||||
{selectedValues.size > 2 ? (
|
||||
<Badge color="gray" className="rounded-sm px-1 font-normal">
|
||||
{selectedValues.size}
|
||||
</Badge>
|
||||
) : (
|
||||
allOptions
|
||||
.filter((option) => selectedValues.has(option.value))
|
||||
.map((option) => (
|
||||
<Badge color="gray" key={option.value} className="rounded-sm px-1 font-normal">
|
||||
{option.label}
|
||||
</Badge>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-[200px] p-0" align="start">
|
||||
<Command>
|
||||
<CommandInput placeholder={title} />
|
||||
<CommandList>
|
||||
<CommandEmpty>No results found.</CommandEmpty>
|
||||
{navigationPath.length > 0 && (
|
||||
<CommandGroup>
|
||||
<CommandItem onSelect={handleBack}>
|
||||
<Icon name="arrow-left" className="mr-2 h-4 w-4" />
|
||||
Back
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
)}
|
||||
<CommandGroup>
|
||||
{currentOptions.map((option) => {
|
||||
const isSelected = selectedValues.has(option.value);
|
||||
return (
|
||||
<CommandItem key={option.value} onSelect={() => handleSelect(option)}>
|
||||
{!option.options && (
|
||||
<div
|
||||
className={classNames(
|
||||
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
|
||||
isSelected ? "text-emphasis" : "opacity-50 [&_svg]:invisible"
|
||||
)}>
|
||||
<Icon name="check" className={classNames("h-4 w-4")} />
|
||||
</div>
|
||||
)}
|
||||
{option.icon && <Icon name={option.icon} className="text-muted mr-2 h-4 w-4" />}
|
||||
<span>{option.label}</span>
|
||||
{option.options && <Icon name="chevron-right" className="ml-auto h-4 w-4" />}
|
||||
{!option.options && facets?.get(option.value) && (
|
||||
<span className="ml-auto flex h-4 w-4 items-center justify-center font-mono text-xs">
|
||||
{facets.get(option.value)}
|
||||
</span>
|
||||
)}
|
||||
</CommandItem>
|
||||
);
|
||||
})}
|
||||
</CommandGroup>
|
||||
{selectedValues.size > 0 && (
|
||||
<>
|
||||
<CommandSeparator />
|
||||
<CommandGroup>
|
||||
<CommandItem
|
||||
onSelect={() => column?.setFilterValue(undefined)}
|
||||
className="justify-center text-center">
|
||||
Clear filters
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
</>
|
||||
)}
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { Table } from "@tanstack/react-table";
|
||||
|
||||
interface DataTablePaginationProps<TData> {
|
||||
table: Table<TData>;
|
||||
totalDbDataCount: number;
|
||||
}
|
||||
|
||||
export function DataTablePagination<TData>({ table, totalDbDataCount }: DataTablePaginationProps<TData>) {
|
||||
const loadedCount = table.getFilteredRowModel().rows.length;
|
||||
|
||||
return (
|
||||
<p className="text-subtle text-sm tabular-nums">
|
||||
Loaded <span className="text-default font-medium">{`${loadedCount}`}</span> of
|
||||
<span className="text-default font-medium"> {`${totalDbDataCount}`}</span>
|
||||
</p>
|
||||
);
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { Table } from "@tanstack/react-table";
|
||||
import { forwardRef } from "react";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
|
||||
import type { IconName } from "../icon";
|
||||
|
||||
export type ActionItem<TData> =
|
||||
| {
|
||||
type: "action";
|
||||
label: string;
|
||||
onClick: () => void;
|
||||
icon?: IconName;
|
||||
needsXSelected?: number;
|
||||
}
|
||||
| {
|
||||
type: "render";
|
||||
render: (table: Table<TData>) => React.ReactNode;
|
||||
needsXSelected?: number;
|
||||
};
|
||||
|
||||
const Root = forwardRef<
|
||||
HTMLDivElement,
|
||||
React.HTMLAttributes<HTMLDivElement> & { showSelectionCount?: boolean }
|
||||
>(({ children, ...props }, ref) => {
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
className={classNames(
|
||||
"bg-brand-default text-brand fixed bottom-4 left-1/2 flex w-fit -translate-x-1/2 transform items-center space-x-3 rounded-lg px-4 py-2",
|
||||
props.className
|
||||
)}
|
||||
style={{ ...props.style }}
|
||||
{...props}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
Root.displayName = "Root";
|
||||
|
||||
export const DataTableSelectionBar = {
|
||||
Root,
|
||||
};
|
||||
@@ -1,127 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { Table } from "@tanstack/react-table";
|
||||
import { useEffect, useState, forwardRef } from "react";
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
import { useDebounce } from "@calcom/lib/hooks/useDebounce";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
import type { ButtonProps } from "../button";
|
||||
import { Button } from "../button";
|
||||
import { Input } from "../form";
|
||||
|
||||
interface DataTableToolbarProps extends ComponentPropsWithoutRef<"div"> {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const Root = forwardRef<HTMLDivElement, DataTableToolbarProps>(function DataTableToolbar(
|
||||
{ children, className },
|
||||
ref
|
||||
) {
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
className={classNames("grid w-full items-center gap-2 py-4", className)}
|
||||
style={{ gridArea: "header" }}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
interface SearchBarProps<TData> {
|
||||
table: Table<TData>;
|
||||
searchKey?: string;
|
||||
onSearch?: (value: string) => void;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
function SearchBarComponent<TData>(
|
||||
{ table, searchKey, onSearch, className }: SearchBarProps<TData>,
|
||||
ref: React.Ref<HTMLInputElement>
|
||||
) {
|
||||
const [searchTerm, setSearchTerm] = useState("");
|
||||
const debouncedSearchTerm = useDebounce(searchTerm, 500);
|
||||
|
||||
useEffect(() => {
|
||||
onSearch?.(debouncedSearchTerm);
|
||||
}, [debouncedSearchTerm, onSearch]);
|
||||
|
||||
if (onSearch) {
|
||||
return (
|
||||
<Input
|
||||
ref={ref}
|
||||
className={`max-w-64 mb-0 mr-auto rounded-md ${className ?? ""}`}
|
||||
placeholder="Search"
|
||||
value={searchTerm}
|
||||
onChange={(event) => setSearchTerm(event.target.value.trim())}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (!searchKey) {
|
||||
console.error("searchKey is required if onSearch is not provided");
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Input
|
||||
ref={ref}
|
||||
className="max-w-64 mb-0 mr-auto rounded-md"
|
||||
placeholder="Search"
|
||||
value={(table.getColumn(searchKey)?.getFilterValue() as string) ?? ""}
|
||||
onChange={(event) => {
|
||||
return table.getColumn(searchKey)?.setFilterValue(event.target.value.trim());
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const SearchBar = forwardRef(SearchBarComponent) as <TData>(
|
||||
props: SearchBarProps<TData> & { ref?: React.Ref<HTMLInputElement> }
|
||||
) => ReturnType<typeof SearchBarComponent>;
|
||||
|
||||
interface ClearFiltersButtonProps<TData> {
|
||||
table: Table<TData>;
|
||||
}
|
||||
|
||||
function ClearFiltersButtonComponent<TData>(
|
||||
{ table }: ClearFiltersButtonProps<TData>,
|
||||
ref: React.Ref<HTMLButtonElement>
|
||||
) {
|
||||
const { t } = useLocale();
|
||||
const isFiltered = table.getState().columnFilters.length > 0;
|
||||
if (!isFiltered) return null;
|
||||
return (
|
||||
<Button
|
||||
ref={ref}
|
||||
color="minimal"
|
||||
EndIcon="x"
|
||||
onClick={() => table.resetColumnFilters()}
|
||||
className="h-8 px-2 lg:px-3">
|
||||
{t("clear")}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
const ClearFiltersButton = forwardRef(ClearFiltersButtonComponent) as <TData>(
|
||||
props: ClearFiltersButtonProps<TData> & { ref?: React.Ref<HTMLButtonElement> }
|
||||
) => ReturnType<typeof ClearFiltersButtonComponent>;
|
||||
|
||||
function CTAComponent(
|
||||
{ children, onClick, color = "primary", ...rest }: ButtonProps,
|
||||
ref: React.Ref<HTMLButtonElement>
|
||||
) {
|
||||
return (
|
||||
<Button ref={ref} color={color} onClick={onClick} {...rest}>
|
||||
{children}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
const CTA = forwardRef(CTAComponent) as (
|
||||
props: ButtonProps & { ref?: React.Ref<HTMLButtonElement> }
|
||||
) => ReturnType<typeof CTAComponent>;
|
||||
|
||||
export const DataTableToolbar = { Root, SearchBar, ClearFiltersButton, CTA };
|
||||
@@ -1,288 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { type Table } from "@tanstack/react-table";
|
||||
import { forwardRef, useState, useMemo } from "react";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { ButtonProps } from "@calcom/ui";
|
||||
import {
|
||||
Button,
|
||||
buttonClasses,
|
||||
Popover,
|
||||
PopoverTrigger,
|
||||
PopoverContent,
|
||||
Command,
|
||||
CommandInput,
|
||||
CommandList,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandItem,
|
||||
CommandSeparator,
|
||||
Icon,
|
||||
} from "@calcom/ui";
|
||||
|
||||
import { useFiltersSearchState } from "./utils";
|
||||
|
||||
interface ColumnVisiblityProps<TData> {
|
||||
table: Table<TData>;
|
||||
}
|
||||
|
||||
function ColumnVisibilityButtonComponent<TData>(
|
||||
{
|
||||
children,
|
||||
color = "secondary",
|
||||
EndIcon = "sliders-vertical",
|
||||
table,
|
||||
...rest
|
||||
}: ColumnVisiblityProps<TData> & ButtonProps,
|
||||
ref: React.Ref<HTMLButtonElement>
|
||||
) {
|
||||
const { t } = useLocale();
|
||||
const allColumns = table.getAllLeafColumns();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [visibleColumns, setVisibleColumns] = useState(
|
||||
() => new Set(allColumns.filter((col) => col.getIsVisible()).map((col) => col.id))
|
||||
);
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button ref={ref} color={color} EndIcon={EndIcon} {...rest}>
|
||||
{children ? children : t("View")}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-[200px] p-0" align="start">
|
||||
<Command>
|
||||
<CommandInput placeholder={t("search")} />
|
||||
<CommandList>
|
||||
<CommandEmpty>{t("no_columns_found")}</CommandEmpty>
|
||||
<CommandGroup heading={t("toggle_columns")}>
|
||||
{allColumns.map((column) => {
|
||||
const canHide = column.getCanHide();
|
||||
if (!column.columnDef.header || typeof column.columnDef.header !== "string" || !canHide)
|
||||
return null;
|
||||
const isVisible = visibleColumns.has(column.id);
|
||||
return (
|
||||
<CommandItem
|
||||
key={column.id}
|
||||
onSelect={() => {
|
||||
column.toggleVisibility(!isVisible);
|
||||
setVisibleColumns((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (isVisible) {
|
||||
next.delete(column.id);
|
||||
} else {
|
||||
next.add(column.id);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}}>
|
||||
<div
|
||||
className={classNames(
|
||||
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
|
||||
isVisible ? "text-emphasis" : "opacity-50 [&_svg]:invisible"
|
||||
)}>
|
||||
<Icon name="check" className={classNames("h-4 w-4", !isVisible && "invisible")} />
|
||||
</div>
|
||||
{column.columnDef.header}
|
||||
</CommandItem>
|
||||
);
|
||||
})}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
<CommandSeparator />
|
||||
<CommandGroup>
|
||||
<CommandItem
|
||||
onSelect={() => {
|
||||
allColumns.forEach((column) => column.toggleVisibility(true));
|
||||
setVisibleColumns(new Set(allColumns.map((col) => col.id)));
|
||||
}}
|
||||
className={classNames(
|
||||
"w-full justify-center text-center",
|
||||
buttonClasses({ color: "secondary" })
|
||||
)}>
|
||||
{t("show_all_columns")}
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
|
||||
const ColumnVisibilityButton = forwardRef(ColumnVisibilityButtonComponent) as <TData>(
|
||||
props: ColumnVisiblityProps<TData> & ButtonProps & { ref?: React.Ref<HTMLButtonElement> }
|
||||
) => ReturnType<typeof ColumnVisibilityButtonComponent>;
|
||||
|
||||
// Filters
|
||||
interface FilterButtonProps<TData> {
|
||||
table: Table<TData>;
|
||||
omit?: string[];
|
||||
}
|
||||
|
||||
function FilterButtonComponent<TData>(
|
||||
{ table, omit }: FilterButtonProps<TData>,
|
||||
ref: React.Ref<HTMLButtonElement>
|
||||
) {
|
||||
const { t } = useLocale();
|
||||
const [_state, _setState] = useFiltersSearchState();
|
||||
|
||||
const activeFilters = _state.activeFilters;
|
||||
const columns = table
|
||||
.getAllColumns()
|
||||
.filter((column) => column.getCanFilter())
|
||||
.filter((column) => !omit?.includes(column.id));
|
||||
|
||||
const filterableColumns = useMemo(() => {
|
||||
return columns.map((column) => ({
|
||||
id: column.id,
|
||||
title: typeof column.columnDef.header === "string" ? column.columnDef.header : column.id,
|
||||
options: column.getFacetedUniqueValues(),
|
||||
}));
|
||||
}, [columns]);
|
||||
|
||||
const handleAddFilter = (columnId: string) => {
|
||||
if (!activeFilters?.some((filter) => filter.f === columnId)) {
|
||||
_setState({ activeFilters: [...activeFilters, { f: columnId, v: [] }] });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex items-center space-x-2">
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button ref={ref} color="secondary" className="border-dashed">
|
||||
<Icon name="filter" className="mr-2 h-4 w-4" />
|
||||
{t("add_filter")}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-[200px] p-0" align="start">
|
||||
<Command>
|
||||
<CommandInput placeholder={t("search_columns")} />
|
||||
<CommandList>
|
||||
<CommandEmpty>{t("no_columns_found")}</CommandEmpty>
|
||||
{filterableColumns.map((column) => {
|
||||
if (activeFilters?.some((filter) => filter.f === column.id)) return null;
|
||||
return (
|
||||
<CommandItem key={column.id} onSelect={() => handleAddFilter(column.id)}>
|
||||
{column.title}
|
||||
</CommandItem>
|
||||
);
|
||||
})}
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const FilterButton = forwardRef(FilterButtonComponent) as <TData>(
|
||||
props: FilterButtonProps<TData> & { ref?: React.Ref<HTMLButtonElement>; omit?: string[] }
|
||||
) => ReturnType<typeof FilterButtonComponent>;
|
||||
|
||||
// Add the new ActiveFilters component
|
||||
interface ActiveFiltersProps<TData> {
|
||||
table: Table<TData>;
|
||||
}
|
||||
|
||||
function ActiveFilters<TData>({ table }: ActiveFiltersProps<TData>) {
|
||||
const { t } = useLocale();
|
||||
const [_state, _setState] = useFiltersSearchState();
|
||||
|
||||
const columns = table.getAllColumns().filter((column) => column.getCanFilter());
|
||||
|
||||
const filterableColumns = useMemo(() => {
|
||||
return columns.map((column) => {
|
||||
return {
|
||||
id: column.id,
|
||||
title: typeof column.columnDef.header === "string" ? column.columnDef.header : column.id,
|
||||
options: column.getFacetedUniqueValues(),
|
||||
};
|
||||
});
|
||||
}, [columns]);
|
||||
|
||||
const handleRemoveFilter = (columnId: string) => {
|
||||
_setState({ activeFilters: (_state.activeFilters || []).filter((filter) => filter.f !== columnId) });
|
||||
table.getColumn(columnId)?.setFilterValue(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{(_state.activeFilters || []).map((filter) => {
|
||||
const column = filterableColumns.find((col) => col.id === filter.f);
|
||||
if (!column) return null;
|
||||
return (
|
||||
<Popover key={column.id}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button color="secondary">
|
||||
{column.title}
|
||||
<Icon name="chevron-down" className="ml-2 h-4 w-4" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="p-0" align="start">
|
||||
<Command>
|
||||
<CommandInput placeholder={t("search_options")} />
|
||||
<CommandList>
|
||||
<CommandEmpty>{t("no_options_found")}</CommandEmpty>
|
||||
{Array.from(column.options).map(([option]) => {
|
||||
if (!option) return null;
|
||||
return (
|
||||
<CommandItem
|
||||
key={option}
|
||||
onSelect={() => {
|
||||
const newFilterValue = filter.v?.includes(option)
|
||||
? filter.v?.filter((value) => value !== option)
|
||||
: [...(filter.v || []), option];
|
||||
_setState({
|
||||
activeFilters: _state.activeFilters.map((f) =>
|
||||
f.f === filter.f ? { ...f, v: newFilterValue } : f
|
||||
),
|
||||
});
|
||||
table
|
||||
.getColumn(filter.f)
|
||||
?.setFilterValue(newFilterValue.length ? newFilterValue : undefined);
|
||||
}}>
|
||||
<div
|
||||
className={classNames(
|
||||
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
|
||||
Array.isArray(table.getColumn(column.id)?.getFilterValue()) &&
|
||||
(table.getColumn(column.id)?.getFilterValue() as string[])?.includes(option)
|
||||
? "bg-primary"
|
||||
: "opacity-50"
|
||||
)}>
|
||||
{Array.isArray(table.getColumn(column.id)?.getFilterValue()) &&
|
||||
(table.getColumn(column.id)?.getFilterValue() as string[])?.includes(option) && (
|
||||
<Icon name="check" className="text-primary-foreground h-4 w-4" />
|
||||
)}
|
||||
</div>
|
||||
{option}
|
||||
</CommandItem>
|
||||
);
|
||||
})}
|
||||
</CommandList>
|
||||
<CommandSeparator />
|
||||
<CommandGroup>
|
||||
<CommandItem
|
||||
onSelect={() => {
|
||||
handleRemoveFilter(column.id);
|
||||
}}
|
||||
className={classNames(
|
||||
"w-full justify-center text-center",
|
||||
buttonClasses({ color: "secondary" })
|
||||
)}>
|
||||
{t("clear")}
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
// Update the export to include ActiveFilters
|
||||
export const DataTableFilters = { ColumnVisibilityButton, FilterButton, ActiveFilters };
|
||||
@@ -1,17 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { parseAsArrayOf, parseAsJson, useQueryStates } from "nuqs";
|
||||
import { z } from "zod";
|
||||
|
||||
const filterSchema = z.object({
|
||||
f: z.string(),
|
||||
v: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
export const filtersSearchParams = {
|
||||
activeFilters: parseAsArrayOf(parseAsJson(filterSchema.parse)).withDefault([]),
|
||||
};
|
||||
|
||||
export function useFiltersSearchState() {
|
||||
return useQueryStates(filtersSearchParams);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
export { DataTableToolbar } from "./DataTableToolbar";
|
||||
export { DataTableSelectionBar } from "./DataTableSelectionBar";
|
||||
export { DataTablePagination } from "./DataTablePagination";
|
||||
export { DataTableFilters } from "./filters";
|
||||
export { useFetchMoreOnBottomReached } from "./useFetchMoreOnBottomReached";
|
||||
export { DataTable } from "./DataTable";
|
||||
export type { DataTableProps } from "./DataTable";
|
||||
@@ -1,29 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect } from "react";
|
||||
|
||||
export const useFetchMoreOnBottomReached = (
|
||||
tableContainerRef: React.RefObject<HTMLDivElement>,
|
||||
fetchNextPage: () => void,
|
||||
isFetching: boolean,
|
||||
totalFetched: number,
|
||||
totalDBRowCount: number
|
||||
) => {
|
||||
const fetchMoreOnBottomReached = useCallback(
|
||||
(containerRefElement?: HTMLDivElement | null) => {
|
||||
if (containerRefElement) {
|
||||
const { scrollHeight, scrollTop, clientHeight } = containerRefElement;
|
||||
if (scrollHeight - scrollTop - clientHeight < 300 && !isFetching && totalFetched < totalDBRowCount) {
|
||||
fetchNextPage();
|
||||
}
|
||||
}
|
||||
},
|
||||
[fetchNextPage, isFetching, totalFetched, totalDBRowCount]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
fetchMoreOnBottomReached(tableContainerRef.current);
|
||||
}, [fetchMoreOnBottomReached, tableContainerRef]);
|
||||
|
||||
return fetchMoreOnBottomReached;
|
||||
};
|
||||
@@ -152,7 +152,6 @@ export { CreateButton, CreateButtonWithTeamsList } from "./components/createButt
|
||||
export { useCalcomTheme } from "./styles/useCalcomTheme";
|
||||
export { ScrollableArea } from "./components/scrollable";
|
||||
export { WizardLayout } from "./layouts/WizardLayout";
|
||||
export * from "./components/data-table";
|
||||
export * from "./components/sheet";
|
||||
export * from "./components/table";
|
||||
export * from "./components/popover";
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
"./command": "./components/command/index.ts",
|
||||
"./createButton": "./components/createButton/index.ts",
|
||||
"./credits": "./components/credits/index.ts",
|
||||
"./data-table": "./components/data-table/index.ts",
|
||||
"./dialog": "./components/dialog/index.ts",
|
||||
"./disconnect-calendar-integration": "./components/disconnect-calendar-integration/index.ts",
|
||||
"./divider": "./components/divider/index.ts",
|
||||
|
||||
Reference in new issue
Block a user