|
| 1 | +import { Menu, MenuItem, MenuPanel, MenuProps } from "@salt-ds/core"; |
| 2 | +import { ReactElement, useMemo } from "react"; |
| 3 | + |
| 4 | +export type FilterPermissions = { |
| 5 | + /** Closing a filter removes it from current display, without deleting*/ |
| 6 | + allowClose?: boolean; |
| 7 | + /** Edit the details of the filter itself */ |
| 8 | + allowEdit?: boolean; |
| 9 | + /** Rename the filter */ |
| 10 | + allowRename?: boolean; |
| 11 | + /** Removing a filter deletes it entirely */ |
| 12 | + allowRemove?: boolean; |
| 13 | +}; |
| 14 | + |
| 15 | +const defaultPermissions: FilterPermissions = { |
| 16 | + allowClose: true, |
| 17 | + allowEdit: true, |
| 18 | + allowRename: true, |
| 19 | + allowRemove: true, |
| 20 | +}; |
| 21 | + |
| 22 | +export interface FilterMenuProps |
| 23 | + extends Pick<MenuProps, "getVirtualElement" | "onOpenChange" | "open"> { |
| 24 | + filterId: string; |
| 25 | + onMenuAction: FilterMenuActionHandler; |
| 26 | + permissions?: FilterPermissions; |
| 27 | +} |
| 28 | + |
| 29 | +export type FilterAction = "close" | "remove" | "edit" | "rename"; |
| 30 | + |
| 31 | +export type FilterMenuActionHandler = <T extends FilterAction = FilterAction>( |
| 32 | + filterId: string, |
| 33 | + filterAction: T, |
| 34 | +) => void; |
| 35 | + |
| 36 | +export const FilterMenu = ({ |
| 37 | + filterId, |
| 38 | + getVirtualElement, |
| 39 | + onMenuAction, |
| 40 | + onOpenChange, |
| 41 | + open, |
| 42 | + permissions = defaultPermissions, |
| 43 | +}: FilterMenuProps) => { |
| 44 | + const { |
| 45 | + allowClose = defaultPermissions.allowClose, |
| 46 | + allowEdit = defaultPermissions.allowEdit, |
| 47 | + allowRename = defaultPermissions.allowRename, |
| 48 | + allowRemove = defaultPermissions.allowRemove, |
| 49 | + } = permissions; |
| 50 | + |
| 51 | + const menuItems = useMemo<ReactElement[]>(() => { |
| 52 | + const items: ReactElement[] = []; |
| 53 | + if (allowClose) { |
| 54 | + items.push( |
| 55 | + <MenuItem key="close" onClick={() => onMenuAction(filterId, "close")}> |
| 56 | + Close |
| 57 | + </MenuItem>, |
| 58 | + ); |
| 59 | + } |
| 60 | + if (allowEdit) { |
| 61 | + items.push( |
| 62 | + <MenuItem key="edit" onClick={() => onMenuAction(filterId, "edit")}> |
| 63 | + Edit Filter |
| 64 | + </MenuItem>, |
| 65 | + ); |
| 66 | + } |
| 67 | + if (allowRename) { |
| 68 | + items.push( |
| 69 | + <MenuItem key="rename" onClick={() => onMenuAction(filterId, "rename")}> |
| 70 | + Rename |
| 71 | + </MenuItem>, |
| 72 | + ); |
| 73 | + } |
| 74 | + if (allowRemove) { |
| 75 | + items.push( |
| 76 | + <MenuItem key="delete" onClick={() => onMenuAction(filterId, "remove")}> |
| 77 | + Delete |
| 78 | + </MenuItem>, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return items; |
| 83 | + }, [allowClose, allowEdit, allowRemove, allowRename, filterId, onMenuAction]); |
| 84 | + return ( |
| 85 | + <Menu |
| 86 | + getVirtualElement={getVirtualElement} |
| 87 | + open={open} |
| 88 | + onOpenChange={onOpenChange} |
| 89 | + > |
| 90 | + <MenuPanel>{menuItems}</MenuPanel> |
| 91 | + </Menu> |
| 92 | + ); |
| 93 | +}; |
0 commit comments