|
| 1 | +import * as React from "react"; |
| 2 | +import { useTranslate, useResourceContext } from "ra-core"; |
| 3 | +import { Columns } from "lucide-react"; |
| 4 | +import * as PopoverPrimitive from "@radix-ui/react-popover"; |
| 5 | +import { useIsMobile } from "@/hooks/use-mobile"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Tooltip, |
| 9 | + TooltipContent, |
| 10 | + TooltipTrigger, |
| 11 | +} from "@/components/ui/tooltip"; |
| 12 | +import { Popover, PopoverTrigger } from "@/components/ui/popover"; |
| 13 | +import { cn } from "@/lib/utils"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Renders a button that lets users show / hide columns in a DataTable |
| 17 | + * |
| 18 | + * @example |
| 19 | + * import { ColumnsButton, DataTable } from 'shadcn-admin-kit'; |
| 20 | + * |
| 21 | + * const PostListActions = () => ( |
| 22 | + * <TopToolbar> |
| 23 | + <ColumnsButton /> |
| 24 | + <FilterButton /> |
| 25 | + * </TopToolbar> |
| 26 | + * ); |
| 27 | + * |
| 28 | + * const PostList = () => ( |
| 29 | + * <List actions={<PostListActions />}> |
| 30 | + * <DataTable> |
| 31 | + * <DataTable.Col source="title" /> |
| 32 | + * <DataTable.Col source="author" /> |
| 33 | + ... |
| 34 | + * </DataTable> |
| 35 | + * </List> |
| 36 | + * ); |
| 37 | + */ |
| 38 | +export const ColumnsButton = (props: ColumnsButtonProps) => { |
| 39 | + const { className, ...rest } = props; |
| 40 | + const resource = useResourceContext(props); |
| 41 | + const storeKey = props.storeKey || `${resource}.datatable`; |
| 42 | + |
| 43 | + const [open, setOpen] = React.useState(false); |
| 44 | + const isMobile = useIsMobile(); |
| 45 | + const translate = useTranslate(); |
| 46 | + |
| 47 | + const title = translate("ra.action.select_columns", { _: "Columns" }); |
| 48 | + |
| 49 | + return ( |
| 50 | + <span className={cn("inline-flex", className)}> |
| 51 | + <Popover open={open} onOpenChange={setOpen}> |
| 52 | + <PopoverTrigger asChild> |
| 53 | + {isMobile ? ( |
| 54 | + <Tooltip> |
| 55 | + <TooltipTrigger asChild> |
| 56 | + <Button |
| 57 | + variant="ghost" |
| 58 | + size="icon" |
| 59 | + aria-label={title} |
| 60 | + {...rest} |
| 61 | + > |
| 62 | + <Columns className="size-4" /> |
| 63 | + </Button> |
| 64 | + </TooltipTrigger> |
| 65 | + <TooltipContent>{title}</TooltipContent> |
| 66 | + </Tooltip> |
| 67 | + ) : ( |
| 68 | + <Button variant="outline" className="cursor-pointer" {...rest}> |
| 69 | + <Columns /> |
| 70 | + {title} |
| 71 | + </Button> |
| 72 | + )} |
| 73 | + </PopoverTrigger> |
| 74 | + <PopoverPrimitive.Portal forceMount> |
| 75 | + <div className={open ? "block" : "hidden"}> |
| 76 | + <PopoverPrimitive.Content |
| 77 | + data-slot="popover-content" |
| 78 | + sideOffset={4} |
| 79 | + align="start" |
| 80 | + className="bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border shadow-md outline-hidden p-0 min-w-[200px]" |
| 81 | + > |
| 82 | + <div id={`${storeKey}-columnsSelector`} className="p-2" /> |
| 83 | + </PopoverPrimitive.Content> |
| 84 | + </div> |
| 85 | + </PopoverPrimitive.Portal> |
| 86 | + </Popover> |
| 87 | + </span> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export interface ColumnsButtonProps |
| 92 | + extends React.ComponentProps<typeof Button> { |
| 93 | + resource?: string; |
| 94 | + storeKey?: string; |
| 95 | +} |
0 commit comments