Skip to content

Commit

Permalink
feat(Table): added link as Table action element (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkirik authored Jan 31, 2024
1 parent 1760166 commit de63cef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/components/Table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ type TableActionConfig = TableAction | TableActionGroup;

#### TableAction

| Name | Description | Type | Default |
| :------- | :-------------- | :----------------------------------: | :--------: |
| text | Text | `string` | |
| handler | Click handler | `(item: any, index: number) => void` | |
| disabled | Action disabled | `boolean ` | |
| theme | Theme | `"normal"` `"danger"` | `"normal"` |
| Name | Description | Type | Default |
| :------- | :----------------------------------------------------------------- | :----------------------------------: | :--------: |
| text | Text | `string` | |
| handler | Click handler | `(item: any, index: number) => void` | |
| disabled | Action disabled | `boolean` | |
| href | Menu item with this prop becomes a link to the specified location. | `string` | |
| target | Same as the `target` attribute of the `<a>` tag. | `string` | |
| rel | Same as the `rel` attribute of the `<a>` tag. | `string` | |
| theme | Theme | `"normal"` `"danger"` | `"normal"` |

#### TableActionGroup

Expand Down
8 changes: 8 additions & 0 deletions src/components/Table/__stories__/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ const WithTableActionsTemplate: StoryFn<TableProps<DataItem>> = (args) => {
action('danger')(handlerArgs);
},
},
{
text: 'with href',
theme: 'normal',
href: 'https://cloud.yandex.com',
target: '_blank',
rel: 'noopener noreferrer',
handler: () => {},
},
];
return <TableWithAction {...args} getRowActions={getRowActions} />;
};
Expand Down
19 changes: 12 additions & 7 deletions src/components/Table/hoc/withTableActions/withTableActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export interface TableAction<I> {
index: number,
event: React.MouseEvent<HTMLDivElement | HTMLAnchorElement, MouseEvent>,
) => void;
href?: string;
target?: string;
rel?: string;
disabled?: boolean;
theme?: MenuItemProps['theme'];
icon?: MenuItemProps['iconStart'];
Expand Down Expand Up @@ -174,16 +177,17 @@ export function withTableActions<I extends TableDataItem, E extends {} = {}>(
</Menu.Group>
);
} else {
const {text, icon, handler, ...restProps} = action;

return (
<Menu.Item
key={index}
disabled={action.disabled}
onClick={this.handleActionClick.bind(this, action, popupData!)}
theme={action.theme}
iconStart={action.icon}
onClick={this.handleActionClick.bind(this, handler, popupData!)}
iconStart={icon}
className={bPopup('menu-item')}
{...restProps}
>
{action.text}
{text}
</Menu.Item>
);
}
Expand All @@ -204,11 +208,12 @@ export function withTableActions<I extends TableDataItem, E extends {} = {}>(
};

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

this.closePopup();
};

Expand Down

0 comments on commit de63cef

Please sign in to comment.