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