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

feat(Table): pass click event to action handler #461

Merged
merged 2 commits into from
Jan 10, 2023
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
11 changes: 9 additions & 2 deletions src/components/Table/__stories__/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DataItem,
} from './utils';
import {TableAction, TableSettingsData} from '..';
import {action} from '@storybook/addon-actions';

export default {
title: 'Components/Table',
Expand Down Expand Up @@ -79,7 +80,10 @@ const WithTableActionsTemplate: Story<TableProps<DataItem>> = (args) => {
const getRowActions = (item: DataItem, index: number): TableAction<DataItem>[] => [
{
text: 'default',
handler: () => alert(JSON.stringify(item)),
handler: (...handlerArgs) => {
alert(JSON.stringify(item));
action('default')(handlerArgs);
},
},
{
text: 'disabled',
Expand All @@ -89,7 +93,10 @@ const WithTableActionsTemplate: Story<TableProps<DataItem>> = (args) => {
{
text: 'danger theme',
theme: 'danger',
handler: () => alert(index),
handler: (...handlerArgs) => {
alert(index);
action('danger')(handlerArgs);
},
},
];
return <TableWithAction {...args} getRowActions={getRowActions} />;
Expand Down
14 changes: 11 additions & 3 deletions src/components/Table/hoc/withTableActions/withTableActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ interface PopupData<I> {

export interface TableAction<I> {
text: string;
handler: (item: I, index: number) => void;
handler: (
item: I,
index: number,
event: React.MouseEvent<HTMLDivElement | HTMLAnchorElement, MouseEvent>,
) => void;
disabled?: boolean;
theme?: MenuItemProps['theme'];
}
Expand Down Expand Up @@ -183,8 +187,12 @@ export function withTableActions<I extends TableDataItem, E extends {} = {}>(
}
};

private handleActionClick = (action: TableAction<I>, data: PopupData<I>) => {
action.handler(data.item, data.index);
private handleActionClick = (
action: TableAction<I>,
data: PopupData<I>,
event: React.MouseEvent<HTMLDivElement | HTMLAnchorElement, MouseEvent>,
) => {
action.handler(data.item, data.index, event);
this.closePopup();
};

Expand Down