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): add renderRowActions property #1353

Merged
merged 5 commits into from
Feb 16, 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
34 changes: 30 additions & 4 deletions src/components/Table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ Adds a special column with actions to table columns.

### Properties

| Name | Description | Type |
| :------------- | :------------------------------------------ | :-------------------------------------------------: |
| getRowActions | Array of action configs for each row | `(item: any, index: number) => TableActionConfig[]` |
| rowActionsSize | Size of actions button and popup menu items | `"s"` `"m"` `"l"` `"xl"` |
| Name | Description | Type |
| :--------------- | :------------------------------------------ | :------------------------------------------------------: |
| getRowActions | Array of action configs for each row | `(item: any, index: number) => TableActionConfig[]` |
| renderRowActions | render function for Actions Cell | `(props: {item: any; index: number}) => React.ReactNode` |
| rowActionsSize | Size of actions button and popup menu items | `"s"` `"m"` `"l"` `"xl"` |

### TableActionConfig

Expand Down Expand Up @@ -129,6 +130,31 @@ const getRowActions = () => {
const table = <MyTable data={data} columns={columns} getRowActions={getRowActions} />;
```

```jsx
import {Table, withTableActions, RenderRowActionsProps} from '@gravity-ui/uikit';

const MyTable = withTableActions(Table);
type Item = {id: number; text: string};

const data: Item[] = [
{id: 1, text: 'Hello'},
{id: 2, text: 'World'},
];
const columns = [{id: 'id'}, {id: 'text'}];

const RowAction = ({item}: RenderRowActionsProps<Item>) => {
return <React.Fragment>{`Action for - ${item.text}`}</React.Fragment>;
};

const table = (
<MyTable
data={data}
columns={columns}
renderRowActions={RowAction}
/>
);
```

## Usage with HOC `withTableCopy`

Allows the contents of a cell or any text to be copied.
Expand Down
21 changes: 20 additions & 1 deletion src/components/Table/__stories__/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _cloneDeep from 'lodash/cloneDeep';

import type {TableAction, TableSettingsData} from '..';
import {Icon} from '../../Icon';
import {TreeSelect} from '../../TreeSelect/TreeSelect';
import {Table} from '../Table';
import type {TableProps} from '../Table';

Expand Down Expand Up @@ -127,7 +128,25 @@ const WithTableActionsTemplate: StoryFn<TableProps<DataItem>> = (args) => {
handler: () => {},
},
];
return <TableWithAction {...args} getRowActions={getRowActions} />;
return (
<React.Fragment>
<h3>{'with getRowActions property'}</h3>
<TableWithAction {...args} getRowActions={getRowActions} />
<br />
<h3>{'with renderRowActions property'}</h3>
<TableWithAction
{...args}
renderRowActions={({index}) => {
if (index % 2) {
return null;
}

const items = ['action 1', 'action 2', 'action 3'];
return <TreeSelect items={items} size="s" />;
}}
/>
</React.Fragment>
);
};
export const HOCWithTableActions = WithTableActionsTemplate.bind({});

Expand Down
1 change: 1 addition & 0 deletions src/components/Table/hoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {withTableSelection} from './withTableSelection/withTableSelection';
export type {WithTableSelectionProps} from './withTableSelection/withTableSelection';
export {withTableActions} from './withTableActions/withTableActions';
export type {
RenderRowActionsProps,
WithTableActionsProps,
TableActionConfig,
TableAction,
Expand Down
Loading
Loading