Skip to content

Commit

Permalink
feat(Table): pass click event to action handler (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
ierehon1905 authored Jan 10, 2023
1 parent cf898d4 commit 620c0e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
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

0 comments on commit 620c0e1

Please sign in to comment.