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(react): add max-width column configuration to Table grids #1770

Merged
merged 1 commit into from
Dec 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
4 changes: 2 additions & 2 deletions docs/pages/components/Table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ function SortableTableExample() {

### Grid Layout

The Table component supports an optional css grid layout that can specify column alignment and width definitions per column.
The Table component supports an optional css grid layout that can specify column alignment and width and max-width definitions per column.

```jsx example
<Table
layout="grid"
columns={[
{ width: 'max-content', align: 'start' },
{ width: 'max-content', align: 'start' },
{ width: '1fr', align: 'end' }
{ width: 'auto', maxWidth: '250', align: 'end' }
]}>
<TableHead>
<TableRow>
Expand Down
24 changes: 23 additions & 1 deletion packages/react/src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TableProvider } from './TableContext';
export type Column = {
align: ColumnAlignment;
width?: ColumnWidth;
maxWidth?: ColumnWidth;
};
export type ColumnAlignment = 'start' | 'center' | 'end';
export type ColumnWidth =
Expand All @@ -31,6 +32,19 @@ type TableGridProps = {
type TableProps = (TableBaseProps | Partial<TableGridProps>) &
React.TableHTMLAttributes<HTMLTableElement>;

function parseColumnWidth(width?: ColumnWidth): string {
const number = Number(width);
if (!isNaN(number)) {
return `${number}px`;
}

if (!width) {
return 'auto';
}

return width;
}

const Table = React.forwardRef<HTMLTableElement, TableProps>(
(
{
Expand Down Expand Up @@ -65,7 +79,15 @@ const Table = React.forwardRef<HTMLTableElement, TableProps>(
}

return columns
.map(({ width }) => width || 'auto')
.map(({ width, maxWidth }) => {
if (maxWidth) {
return `minmax(${parseColumnWidth(width)}, ${parseColumnWidth(
maxWidth
)})`;
}

return parseColumnWidth(width);
})
.join(' ');
}, [layout, columns]);

Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/components/Table/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ test('should support column definitions with grid layout', () => {
expect(tableCells[1]).toHaveStyle('text-align: end');
});

test('should support column definitions with maxWidth with grid layout', () => {
renderDefaultTable({
layout: 'grid',
columns: [
{ width: '1fr', align: 'start' },
{ width: 'min-content', align: 'end' },
{ width: 'min-content', maxWidth: '789', align: 'end' },
{ maxWidth: '789', align: 'end' }
]
});
const table = screen.getByRole('table');
expect(table).toHaveStyle(
'--table-grid-template-columns: 1fr min-content minmax(min-content, 789px) minmax(auto, 789px)'
);
});

test('should apply colspan styles to cells in grid layout', () => {
render(
<Table layout="grid">
Expand Down
Loading