Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Table): add default minWidth for proportional columns (#891)

Cindy Zhangc66330cc033e
diff --git a/packages/core/src/Table/XDSBaseTable.tsx b/packages/core/src/Table/XDSBaseTable.tsx
index 3591d9159f..85bc847070 100644
--- a/packages/core/src/Table/XDSBaseTable.tsx
+++ b/packages/core/src/Table/XDSBaseTable.tsx
@@ -25,7 +25,11 @@ import type {
TableCellComponentProps,
TableHeaderCellComponentProps,
} from './types';
-import {generateColumns, defaultCellRenderer} from './columnUtils';
+import {
+ generateColumns,
+ defaultCellRenderer,
+ DEFAULT_MIN_COLUMN_WIDTH,
+} from './columnUtils';
import {XDSTableRow} from './XDSTableRow';
import {XDSTableCell} from './XDSTableCell';
import {XDSTableHeaderCell} from './XDSTableHeaderCell';
@@ -198,7 +202,7 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
const HeaderCellComponent = (components?.HeaderCell ??
XDSTableHeaderCell) as React.ComponentType<TableHeaderCellComponentProps>;
- // Resolve columns: explicit > auto-generated from data
+ // Resolve columns: explicit > auto-generated from data.
const resolvedColumns: XDSTableColumn<T>[] =
columnsProp ?? (data ? generateColumns(data) : []);
@@ -217,10 +221,31 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
col,
);
+ // Apply column min-width on the <th>. With table-layout: fixed,
+ // header cell sizing controls column widths.
+ // Proportional columns (or columns without explicit width) get a default minWidth.
+ const colWidth = col.width;
+ const minW =
+ !colWidth || colWidth.type === 'proportional'
+ ? (colWidth?.minWidth ?? DEFAULT_MIN_COLUMN_WIDTH)
+ : undefined;
+ const minWidthStyle = minW != null ? {minWidth: `${minW}px`} : undefined;
+ const existingStyle = cellRenderProps.htmlProps.style as
+ | React.CSSProperties
+ | undefined;
+ const mergedHtmlProps = minWidthStyle
+ ? {
+ ...cellRenderProps.htmlProps,
+ style: existingStyle
+ ? {...existingStyle, ...minWidthStyle}
+ : minWidthStyle,
+ }
+ : cellRenderProps.htmlProps;
+
return (
<HeaderCellComponent
key={col.key}
- {...cellRenderProps.htmlProps}
+ {...mergedHtmlProps}
xstyle={cellRenderProps.styles}>
{col.header ?? col.key}
</HeaderCellComponent>
diff --git a/packages/core/src/Table/XDSTable.test.tsx b/packages/core/src/Table/XDSTable.test.tsx
index 551dc7c3f3..00908e3ce1 100644
--- a/packages/core/src/Table/XDSTable.test.tsx
+++ b/packages/core/src/Table/XDSTable.test.tsx
@@ -13,7 +13,13 @@ import {XDSBaseTable} from './XDSBaseTable';
import {XDSTable} from './XDSTable';
import {XDSTableRow} from './XDSTableRow';
import {XDSTableCell} from './XDSTableCell';
-import {proportional, pixel, generateColumns, capitalize} from './columnUtils';
+import {
+ proportional,
+ pixel,
+ generateColumns,
+ capitalize,
+ DEFAULT_MIN_COLUMN_WIDTH,
+} from './columnUtils';
import type {TablePlugin, XDSTableColumn} from './types';
// =============================================================================
@@ -44,14 +50,22 @@ const columns: XDSTableColumn<User>[] = [
describe('columnUtils', () => {
describe('proportional', () => {
- it('creates a proportional width with default value 1', () => {
+ it('creates a proportional width with default value 1 and default minWidth', () => {
const w = proportional();
- expect(w).toEqual({type: 'proportional', value: 1});
+ expect(w).toEqual({
+ type: 'proportional',
+ value: 1,
+ minWidth: DEFAULT_MIN_COLUMN_WIDTH,
+ });
});
- it('creates a proportional width with custom value', () => {
+ it('creates a proportional width with custom value and default minWidth', () => {
const w = proportional(3);
- expect(w).toEqual({type: 'proportional', value: 3});
+ expect(w).toEqual({
+ type: 'proportional',
+ value: 3,
+ minWidth: DEFAULT_MIN_COLUMN_WIDTH,
+ });
});
});
@@ -92,13 +106,34 @@ describe('columnUtils', () => {
expect(generateColumns([])).toEqual([]);
});
- it('assigns proportional(1) width to each column', () => {
+ it('assigns proportional(1) width with default minWidth to each column', () => {
const cols = generateColumns(users);
for (const col of cols) {
- expect(col.width).toEqual({type: 'proportional', value: 1});
+ expect(col.width).toEqual({
+ type: 'proportional',
+ value: 1,
+ minWidth: DEFAULT_MIN_COLUMN_WIDTH,
+ });
}
});
});
+
+ describe('proportional with minWidth', () => {
+ it('creates a proportional width with explicit minWidth', () => {
+ const w = proportional(1, {minWidth: 200});
+ expect(w).toEqual({type: 'proportional', value: 1, minWidth: 200});
+ });
+
+ it('uses DEFAULT_MIN_COLUMN_WIDTH when no minWidth provided', () => {
+ const w = proportional(2);
+ expect(w).toEqual({
+ type: 'proportional',
+ value: 2,
+ minWidth: DEFAULT_MIN_COLUMN_WIDTH,
+ });
+ expect(w.minWidth).toBe(DEFAULT_MIN_COLUMN_WIDTH);
+ });
+ });
});
// =============================================================================
@@ -386,6 +421,72 @@ describe('XDSBaseTable', () => {
expect(screen.getByRole('table')).toHaveAttribute('data-step', '1,2');
});
});
+
+ describe('column min-widths', () => {
+ it('applies default minWidth on header cells for proportional columns', () => {
+ const cols: XDSTableColumn<User>[] = [
+ {key: 'name', header: 'Name', width: proportional(1)},
+ {key: 'age', header: 'Age', width: proportional(1)},
+ ];
+ render(<XDSBaseTable data={users} columns={cols} />);
+ const headers = screen.getAllByRole('columnheader');
+ expect(headers[0]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ expect(headers[1]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ });
+
+ it('applies explicit minWidth on proportional columns', () => {
+ const cols: XDSTableColumn<User>[] = [
+ {key: 'name', header: 'Name', width: proportional(1, {minWidth: 200})},
+ {key: 'age', header: 'Age', width: proportional(1)},
+ ];
+ render(<XDSBaseTable data={users} columns={cols} />);
+ const headers = screen.getAllByRole('columnheader');
+ expect(headers[0]).toHaveStyle({minWidth: '200px'});
+ expect(headers[1]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ });
+
+ it('does not set minWidth on pixel columns', () => {
+ const cols: XDSTableColumn<User>[] = [
+ {key: 'name', header: 'Name', width: pixel(80)},
+ {key: 'age', header: 'Age', width: proportional(1)},
+ ];
+ render(<XDSBaseTable data={users} columns={cols} />);
+ const headers = screen.getAllByRole('columnheader');
+ expect(headers[0].style.minWidth).toBe('');
+ expect(headers[1]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ });
+
+ it('applies default minWidth on auto-generated columns', () => {
+ render(<XDSBaseTable data={users} />);
+ const headers = screen.getAllByRole('columnheader');
+ for (const header of headers) {
+ expect(header).toHaveStyle({minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`});
+ }
+ });
+
+ it('applies default minWidth on columns with no explicit width', () => {
+ const cols: XDSTableColumn<User>[] = [
+ {key: 'name', header: 'Name'},
+ {key: 'age', header: 'Age'},
+ ];
+ render(<XDSBaseTable data={users} columns={cols} />);
+ const headers = screen.getAllByRole('columnheader');
+ expect(headers[0]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ expect(headers[1]).toHaveStyle({
+ minWidth: `${DEFAULT_MIN_COLUMN_WIDTH}px`,
+ });
+ });
+ });
});
// =============================================================================
diff --git a/packages/core/src/Table/XDSTableHeaderCell.tsx b/packages/core/src/Table/XDSTableHeaderCell.tsx
index 0e6a827dce..0682b45586 100644
--- a/packages/core/src/Table/XDSTableHeaderCell.tsx
+++ b/packages/core/src/Table/XDSTableHeaderCell.tsx
@@ -11,7 +11,6 @@
* - /packages/core/src/Table/index.ts
*/
-
import {useContext, type ReactNode} from 'react';
import type {XDSBaseProps} from '../XDSBaseProps';
import * as stylex from '@stylexjs/stylex';
@@ -110,6 +109,8 @@ export function XDSTableHeaderCell({
children,
xstyle,
ref,
+ className: incomingClassName,
+ style: incomingStyle,
...props
}: XDSTableHeaderCellProps) {
const ctx = useContext(XDSTableContext);
@@ -122,6 +123,8 @@ export function XDSTableHeaderCell({
{...mergeProps(
xdsClassName('table-header-cell'),
stylex.props(xstyle),
+ incomingClassName,
+ incomingStyle as React.CSSProperties,
)}>
{children}
</th>
@@ -153,6 +156,8 @@ export function XDSTableHeaderCell({
{...mergeProps(
xdsClassName('table-header-cell'),
stylex.props(...cellStyles),
+ incomingClassName,
+ incomingStyle as React.CSSProperties,
)}>
{children}
</th>
diff --git a/packages/core/src/Table/columnUtils.ts b/packages/core/src/Table/columnUtils.ts
index edf85e3eb9..0805e22827 100644
--- a/packages/core/src/Table/columnUtils.ts
+++ b/packages/core/src/Table/columnUtils.ts
@@ -12,17 +12,29 @@
import type {ReactNode} from 'react';
import type {XDSTableColumn, ProportionalWidth, PixelWidth} from './types';
+/** Default minimum width (in px) for proportional columns. */
+export const DEFAULT_MIN_COLUMN_WIDTH = 120;
+
/**
* Create a proportional column width (fr-like).
* Columns share available space proportionally.
+ * Applies `DEFAULT_MIN_COLUMN_WIDTH` when no explicit minWidth is provided.
*
* @example
* ```
* proportional(2) // twice as wide as proportional(1)
+ * proportional(1, { minWidth: 200 }) // explicit min
* ```
*/
-export function proportional(value: number = 1): ProportionalWidth {
- return {type: 'proportional', value};
+export function proportional(
+ value: number = 1,
+ options?: {minWidth?: number},
+): ProportionalWidth {
+ return {
+ type: 'proportional',
+ value,
+ minWidth: options?.minWidth ?? DEFAULT_MIN_COLUMN_WIDTH,
+ };
}
/**
diff --git a/packages/core/src/Table/index.ts b/packages/core/src/Table/index.ts
index c17fbd8510..4eea9202cf 100644
--- a/packages/core/src/Table/index.ts
+++ b/packages/core/src/Table/index.ts
@@ -17,7 +17,12 @@ export {XDSTableHeaderCell} from './XDSTableHeaderCell';
export {XDSTableContext} from './XDSTableContext';
export {useXDSTableSelection} from './plugins/selection';
export {useXDSBaseTablePlugins} from './useXDSBaseTablePlugins';
-export {proportional, pixel, generateColumns} from './columnUtils';
+export {
+ proportional,
+ pixel,
+ generateColumns,
+ DEFAULT_MIN_COLUMN_WIDTH,
+} from './columnUtils';
export type {
XDSTableColumn,
ColumnWidth,
diff --git a/packages/core/src/Table/types.ts b/packages/core/src/Table/types.ts
index 515cd8dfa2..7ec1213809 100644
--- a/packages/core/src/Table/types.ts
+++ b/packages/core/src/Table/types.ts
@@ -29,6 +29,8 @@ import type {StyleXStyles} from '../theme/types';
export interface ProportionalWidth {
type: 'proportional';
value: number;
+ /** Minimum width in pixels. Prevents the column from shrinking below this size. */
+ minWidth?: number;
}
/**