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): added link as Table action element #1290

Merged
merged 2 commits into from
Jan 31, 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
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
Loading