Skip to content

Commit

Permalink
feat(react): add max-width column configuration to Table grids (#1770)
Browse files Browse the repository at this point in the history
  • Loading branch information
scurker authored Dec 18, 2024
1 parent 6773975 commit dc7e4ac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
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

0 comments on commit dc7e4ac

Please sign in to comment.