Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Table): added ActionsColumn prop to control close on click #10179

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions packages/react-table/src/components/Table/ActionsColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface ActionsColumnProps extends Omit<React.HTMLProps<HTMLElement>, '
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;
Comment on lines +33 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per above I've added this prop. From what I can tell since ActionsColumn has its own isOpen state, being able to pass a custom onOpenChange callback wouldn't be as easy without introducing other props to customize things, and at that point I'd wonder if it wouldjust make more sense to allow passing a custom Dropdown itself (rather than right now we only really allow a custom actionsToggle)

}

const ActionsColumnBase: React.FunctionComponent<ActionsColumnProps> = ({
Expand All @@ -44,6 +46,7 @@ const ActionsColumnBase: React.FunctionComponent<ActionsColumnProps> = ({
},
innerRef,
firstActionItemRef,
isOnOpenChangeDisabled = false,
...props
}: ActionsColumnProps) => {
const [isOpen, setIsOpen] = React.useState(false);
Expand Down Expand Up @@ -91,7 +94,7 @@ const ActionsColumnBase: React.FunctionComponent<ActionsColumnProps> = ({

<Dropdown
isOpen={isOpen}
onOpenChange={(isOpen: boolean) => setIsOpen(isOpen)}
onOpenChange={!isOnOpenChangeDisabled ? (isOpen: boolean) => setIsOpen(isOpen) : undefined}
toggle={(toggleRef: any) =>
actionsToggle ? (
actionsToggle({ onToggle, isOpen, isDisabled, toggleRef })
Expand All @@ -116,35 +119,37 @@ const ActionsColumnBase: React.FunctionComponent<ActionsColumnProps> = ({
<DropdownList>
{items
.filter((item) => !item.isOutsideDropdown)
.map(({ title, itemKey, onClick, tooltipProps, isSeparator, ...props }, index) => {
if (isSeparator) {
return <Divider key={itemKey || index} data-key={itemKey || index} />;
}
const item = (
<DropdownItem
onClick={(event: any) => {
onActionClick(event, onClick);
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>
.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>
);
} else {
return item;

if (tooltipProps?.content) {
return (
<Tooltip key={itemKey || index} {...tooltipProps}>
{item}
</Tooltip>
);
} else {
return item;
}
}
})}
)}
</DropdownList>
</Dropdown>
</React.Fragment>
Expand Down
2 changes: 2 additions & 0 deletions packages/react-table/src/components/Table/TableTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export interface IAction extends Omit<DropdownItemProps, 'title' | 'onClick'>, P
onClick?: (event: React.MouseEvent, rowIndex: number, rowData: IRowData, extraData: IExtraData) => void;
/** Flag indicating this action should be placed outside the actions menu, beside the toggle */
isOutsideDropdown?: boolean;
/** Flag indicating whether the actions dropdown should close after an item is clicked. */
shouldCloseOnClick?: boolean;
}

export interface ISeparator extends IAction {
Expand Down
Loading