| 1 |
import { cloneElement, forwardRef, Fragment, useState } from 'react'; |
| 2 |
import { Dropdown, DropdownItem, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown'; |
| 3 |
import { Button } from '@patternfly/react-core/dist/esm/components/Button'; |
| 4 |
import { Divider } from '@patternfly/react-core/dist/esm/components/Divider'; |
| 5 |
import { MenuToggle } from '@patternfly/react-core/dist/esm/components/MenuToggle'; |
| 6 |
import { IAction, IExtraData, IRowData } from './TableTypes'; |
| 7 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 8 |
import { Tooltip } from '@patternfly/react-core/dist/esm/components/Tooltip'; |
| 9 |
|
| 10 |
export interface CustomActionsToggleProps { |
| 11 |
onToggle: (event: React.MouseEvent | React.KeyboardEvent) => void; |
| 12 |
isOpen: boolean; |
| 13 |
isDisabled: boolean; |
| 14 |
toggleRef: React.Ref<any>; |
| 15 |
} |
| 16 |
export interface ActionsColumnProps extends Omit<React.HTMLProps<HTMLElement>, 'label'> { |
| 17 |
/** Actions to be rendered within or without the action dropdown */ |
| 18 |
items: IAction[]; |
| 19 |
/** Indicates whether the actions dropdown is disabled */ |
| 20 |
isDisabled?: boolean; |
| 21 |
/** Data of the row the action dropdown is located */ |
| 22 |
rowData?: IRowData; |
| 23 |
/** Extra data of a row */ |
| 24 |
extraData?: IExtraData; |
| 25 |
/** Custom actions toggle for the actions dropdown */ |
| 26 |
actionsToggle?: (props: CustomActionsToggleProps) => React.ReactNode; |
| 27 |
/** Additional properties for the actions dropdown popper */ |
| 28 |
popperProps?: any; |
| 29 |
/** @hide Forwarded ref */ |
| 30 |
innerRef?: React.Ref<any>; |
| 31 |
/** Ref to forward to the first item in the popup menu */ |
| 32 |
firstActionItemRef?: React.Ref<HTMLButtonElement>; |
| 33 |
/** Flag indicating that the dropdown's onOpenChange callback should not be called. */ |
| 34 |
isOnOpenChangeDisabled?: boolean; |
| 35 |
} |
| 36 |
|
| 37 |
const ActionsColumnBase: React.FunctionComponent<ActionsColumnProps> = ({ |
| 38 |
items, |
| 39 |
isDisabled, |
| 40 |
rowData, |
| 41 |
extraData, |
| 42 |
actionsToggle, |
| 43 |
popperProps = { |
| 44 |
position: 'end', |
| 45 |
direction: 'down' |
| 46 |
}, |
| 47 |
innerRef, |
| 48 |
firstActionItemRef, |
| 49 |
isOnOpenChangeDisabled = false, |
| 50 |
...props |
| 51 |
}: ActionsColumnProps) => { |
| 52 |
const [isOpen, setIsOpen] = useState(false); |
| 53 |
|
| 54 |
const onToggle = () => { |
| 55 |
setIsOpen(!isOpen); |
| 56 |
}; |
| 57 |
|
| 58 |
const onActionClick = ( |
| 59 |
event: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent, |
| 60 |
onClick: |
| 61 |
| ((event: React.MouseEvent, rowIndex: number | undefined, rowData: IRowData, extraData: IExtraData) => void) |
| 62 |
| undefined |
| 63 |
): void => { |
| 64 |
// Only prevent default if onClick is provided. This allows href support. |
| 65 |
if (onClick) { |
| 66 |
event.preventDefault(); |
| 67 |
// tslint:disable-next-line:no-unused-expression |
| 68 |
onClick(event as React.MouseEvent, extraData && extraData.rowIndex, rowData, extraData); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
return ( |
| 73 |
<Fragment> |
| 74 |
{items |
| 75 |
.filter((item) => item.isOutsideDropdown) |
| 76 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 77 |
.map(({ title, itemKey, onClick, isOutsideDropdown, ...props }, key) => |
| 78 |
typeof title === 'string' ? ( |
| 79 |
<Button |
| 80 |
onClick={(event: MouseEvent | React.MouseEvent<any, MouseEvent> | React.KeyboardEvent<Element>) => |
| 81 |
onActionClick(event, onClick) |
| 82 |
} |
| 83 |
{...(props as any)} |
| 84 |
isDisabled={isDisabled} |
| 85 |
key={itemKey || `outside_dropdown_${key}`} |
| 86 |
data-key={itemKey || `outside_dropdown_${key}`} |
| 87 |
> |
| 88 |
{title} |
| 89 |
</Button> |
| 90 |
) : ( |
| 91 |
cloneElement(title as React.ReactElement<any>, { onClick, isDisabled, ...props }) |
| 92 |
) |
| 93 |
)} |
| 94 |
<Dropdown |
| 95 |
isOpen={isOpen} |
| 96 |
onOpenChange={!isOnOpenChangeDisabled ? (isOpen: boolean) => setIsOpen(isOpen) : undefined} |
| 97 |
toggle={(toggleRef: any) => |
| 98 |
actionsToggle ? ( |
| 99 |
actionsToggle({ onToggle, isOpen, isDisabled, toggleRef }) |
| 100 |
) : ( |
| 101 |
<MenuToggle |
| 102 |
aria-label="Kebab toggle" |
| 103 |
ref={toggleRef} |
| 104 |
onClick={onToggle} |
| 105 |
isExpanded={isOpen} |
| 106 |
isDisabled={isDisabled} |
| 107 |
variant="plain" |
| 108 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 109 |
/> |
| 110 |
) |
| 111 |
} |
| 112 |
{...(rowData && rowData.actionProps)} |
| 113 |
ref={innerRef} |
| 114 |
{...props} |
| 115 |
popperProps={popperProps} |
| 116 |
> |
| 117 |
<DropdownList> |
| 118 |
{items |
| 119 |
.filter((item) => !item.isOutsideDropdown) |
| 120 |
.map( |
| 121 |
({ title, itemKey, onClick, tooltipProps, isSeparator, shouldCloseOnClick = true, ...props }, index) => { |
| 122 |
if (isSeparator) { |
| 123 |
return <Divider key={itemKey || index} data-key={itemKey || index} />; |
| 124 |
} |
| 125 |
const item = ( |
| 126 |
<DropdownItem |
| 127 |
onClick={(event: any) => { |
| 128 |
onActionClick(event, onClick); |
| 129 |
shouldCloseOnClick && onToggle(); |
| 130 |
}} |
| 131 |
{...props} |
| 132 |
key={itemKey || index} |
| 133 |
data-key={itemKey || index} |
| 134 |
ref={index === 0 ? firstActionItemRef : undefined} |
| 135 |
> |
| 136 |
{title} |
| 137 |
</DropdownItem> |
| 138 |
); |
| 139 |
|
| 140 |
if (tooltipProps?.content) { |
| 141 |
return ( |
| 142 |
<Tooltip key={itemKey || index} {...tooltipProps}> |
| 143 |
{item} |
| 144 |
</Tooltip> |
| 145 |
); |
| 146 |
} else { |
| 147 |
return item; |
| 148 |
} |
| 149 |
} |
| 150 |
)} |
| 151 |
</DropdownList> |
| 152 |
</Dropdown> |
| 153 |
</Fragment> |
| 154 |
); |
| 155 |
}; |
| 156 |
|
| 157 |
export const ActionsColumn = forwardRef((props: ActionsColumnProps, ref: React.Ref<HTMLElement>) => ( |
| 158 |
<ActionsColumnBase {...props} innerRef={ref} /> |
| 159 |
)); |
| 160 |
ActionsColumn.displayName = 'ActionsColumn'; |