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 width property #1907

Merged
merged 2 commits into from
Oct 18, 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
1 change: 1 addition & 0 deletions src/components/Table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Additional functionality is enabled via HOCs:
| edgePadding | Adds horizontal padding for edge cells | `boolean` | |
| stickyHorizontalScroll | A horizontal sticky scroll in a table. NB: A table cannot have a fixed height and a sticky scroll at the same time. A sticky scroll will not work if the table has an overflow. | `boolean` | `false` |
| stickyHorizontalScrollBreakpoint | The threshold that the parent block should reach before making a scroll sticky. This is useful in the console, for example, when the groupActions bar closes the scroll. | `number` | `0` |
| `width` | Table width | `"auto"` `"max"` | "auto" |

### DescriptorType

Expand Down
4 changes: 4 additions & 0 deletions src/components/Table/Table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
border-spacing: 0;
// fix border disappear in Firefox:
border-collapse: separate;

&_width_max {
width: 100%;
}
}

&__cell {
Expand Down
6 changes: 5 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface DescriptorType {
disabled?: boolean;
}

export type TableWidth = 'auto' | 'max';

// TODO: Replace @default in props description with defaultProps in order to work with Storybook.
export interface TableProps<I> extends QAProps {
/** Data */
Expand Down Expand Up @@ -165,6 +167,7 @@ export interface TableProps<I> extends QAProps {
className?: string;
/** Adds horizontal padding for edge cells. */
edgePadding?: boolean;
width?: TableWidth;
}

interface TableDefaultProps {
Expand Down Expand Up @@ -451,8 +454,9 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
}

private renderTable() {
const {width = 'auto'} = this.props;
return (
<table ref={this.tableRef} className={b('table')}>
<table ref={this.tableRef} className={b('table', {width})}>
{this.renderHead()}
{this.renderBody()}
</table>
Expand Down
8 changes: 7 additions & 1 deletion src/components/Table/__tests__/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import userEvent from '@testing-library/user-event';

import {render, screen, within} from '../../../../test-utils/utils';
import type {TableColumnConfig, TableProps} from '../Table';
import type {TableColumnConfig, TableProps, TableWidth} from '../Table';
import {Table} from '../Table';

import type {DataItem} from './utils';
Expand Down Expand Up @@ -34,6 +34,12 @@ describe('Table', () => {
});
},
);
test.each(new Array<TableWidth>('max', 'auto'))('render with given "%s" width', (width) => {
render(<Table data={data} columns={columns} width={width} />);
const table = screen.getByRole('table');

expect(table).toHaveClass(`g-table__table_width_${width}`);
});

test('render table with no data (default)', () => {
const emptyText = 'No data';
Expand Down
Loading