-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acc5c7f
commit 30d0dee
Showing
5 changed files
with
79 additions
and
27 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/features/data-table/components/filters/FilterPopover.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import startCase from "lodash/startCase"; | ||
|
||
import { Button, Icon, Popover, PopoverContent, PopoverTrigger } from "@calcom/ui"; | ||
|
||
import { useFilterValue } from "../../hooks"; | ||
import { type FilterableColumn, type FilterValue, ZFilterValue } from "../../lib/types"; | ||
import { | ||
isSingleSelectFilterValue, | ||
isMultiSelectFilterValue, | ||
isTextFilterValue, | ||
isNumberFilterValue, | ||
} from "../../lib/utils"; | ||
import { FilterOptions } from "./FilterOptions"; | ||
|
||
const FILTER_ICONS = { | ||
text: "file-text", | ||
number: "binary", | ||
multi_select: "layers", | ||
single_select: "disc", | ||
} as const; | ||
|
||
type FilterPopoverProps = { | ||
column: FilterableColumn; | ||
}; | ||
|
||
const useSelectedLabels = ({ | ||
column, | ||
filterValue, | ||
}: { | ||
column: FilterableColumn; | ||
filterValue: FilterValue; | ||
}) => { | ||
if (isTextFilterValue(filterValue)) { | ||
return { text: filterValue.data.operand, moreCount: 0 }; | ||
} else if (isNumberFilterValue(filterValue)) { | ||
return { text: filterValue.data.operand, moreCount: 0 }; | ||
} else if (isSingleSelectFilterValue(filterValue)) { | ||
const text = Array.from(column.options.keys()).find((opt) => opt.value === filterValue.data)?.label; | ||
return { text, moreCount: 0 }; | ||
} else if (isMultiSelectFilterValue(filterValue)) { | ||
const text = Array.from(column.options.keys()).find((opt) => opt.value === filterValue.data[0])?.label; | ||
return { text, moreCount: filterValue.data.length - 1 }; | ||
} else { | ||
return { text: "", moreCount: 0 }; | ||
} | ||
}; | ||
|
||
export function FilterPopover({ column }: FilterPopoverProps) { | ||
const icon = column.icon || FILTER_ICONS[column.type]; | ||
const filterValue = useFilterValue(column.id, ZFilterValue); | ||
const { text, moreCount } = useSelectedLabels({ column, filterValue }); | ||
return ( | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button color="secondary" className="items-center"> | ||
<Icon name={icon} className="mr-2 h-4 w-4" /> | ||
<span>{startCase(column.title)}</span> | ||
{text && <span className="bg-subtle ml-2 rounded-md px-2">{text}</span>} | ||
{moreCount > 0 && <span className="bg-subtle ml-2 rounded-md px-2">+{moreCount}</span>} | ||
<Icon name="chevron-down" className="ml-2 h-4 w-4" /> | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="p-0" align="start"> | ||
<FilterOptions column={column} /> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters