Skip to content

Commit 0078a39

Browse files
authored
feat: column minWidth (#1125)
* feat: column minWidth * chore: simplify code
1 parent 9662833 commit 0078a39

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ React.render(<Table columns={columns} data={data} />, mountNode);
129129
| title | React Node | | title of this column |
130130
| dataIndex | String | | display field of the data record |
131131
| width | String \| Number | | width of the specific proportion calculation according to the width of the columns |
132+
| minWidth | Number | | the minimum width of the column, only worked when tableLayout is auto |
132133
| fixed | String \| Boolean | | this column will be fixed when table scroll horizontally: true or 'left' or 'right' |
133134
| align | String | | specify how cell content is aligned |
134135
| ellipsis | Boolean | | specify whether cell content be ellipsized |

src/ColGroup.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import type { ColumnType } from './interface';
33
import { INTERNAL_COL_DEFINE } from './utils/legacyUtil';
4+
import { useContext } from '@rc-component/context';
5+
import TableContext from './context/TableContext';
46

57
export interface ColGroupProps<RecordType> {
68
colWidths: readonly (number | string)[];
@@ -9,6 +11,8 @@ export interface ColGroupProps<RecordType> {
911
}
1012

1113
function ColGroup<RecordType>({ colWidths, columns, columCount }: ColGroupProps<RecordType>) {
14+
const { tableLayout } = useContext(TableContext, ['tableLayout']);
15+
1216
const cols: React.ReactElement[] = [];
1317
const len = columCount || columns.length;
1418

@@ -18,11 +22,20 @@ function ColGroup<RecordType>({ colWidths, columns, columCount }: ColGroupProps<
1822
for (let i = len - 1; i >= 0; i -= 1) {
1923
const width = colWidths[i];
2024
const column = columns && columns[i];
21-
const additionalProps = column && column[INTERNAL_COL_DEFINE];
25+
let additionalProps;
26+
let minWidth: number;
27+
if (column) {
28+
additionalProps = column[INTERNAL_COL_DEFINE];
29+
30+
// fixed will cause layout problems
31+
if (tableLayout === 'auto') {
32+
minWidth = column.minWidth;
33+
}
34+
}
2235

23-
if (width || additionalProps || mustInsert) {
36+
if (width || minWidth || additionalProps || mustInsert) {
2437
const { columnType, ...restAdditionalProps } = additionalProps || {};
25-
cols.unshift(<col key={i} style={{ width }} {...restAdditionalProps} />);
38+
cols.unshift(<col key={i} style={{ width, minWidth }} {...restAdditionalProps} />);
2639
mustInsert = true;
2740
}
2841
}

src/interface.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ export type Direction = 'ltr' | 'rtl';
7070
// SpecialString will be removed in antd@6
7171
export type SpecialString<T> = T | (string & {});
7272

73-
export type DataIndex<T = any> = DeepNamePath<T> | SpecialString<T> | number | (SpecialString<T> | number)[];
73+
export type DataIndex<T = any> =
74+
| DeepNamePath<T>
75+
| SpecialString<T>
76+
| number
77+
| (SpecialString<T> | number)[];
7478

7579
export type CellEllipsisType = { showTitle?: boolean } | boolean;
7680

@@ -109,6 +113,7 @@ export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
109113
shouldCellUpdate?: (record: RecordType, prevRecord: RecordType) => boolean;
110114
rowSpan?: number;
111115
width?: number | string;
116+
minWidth?: number;
112117
onCell?: GetComponentProps<RecordType>;
113118
/** @deprecated Please use `onCell` instead */
114119
onCellClick?: (record: RecordType, e: React.MouseEvent<HTMLElement>) => void;

tests/Colgroup.spec.jsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,29 @@ describe('Table.ColGroup', () => {
2626
const wrapper = mount(<Table columns={columns} />);
2727
expect(String(wrapper.find('colgroup col').key())).toEqual('0');
2828
});
29+
30+
it('minWidth should be worked', () => {
31+
const columns = [
32+
{
33+
key: 0,
34+
minWidth: 100,
35+
},
36+
];
37+
38+
const wrapper = mount(<Table columns={columns} />);
39+
expect(wrapper.find('colgroup col').at(0).props().style.minWidth).toEqual(100);
40+
});
41+
42+
it('should not have minWidth when tableLayout is fixed', () => {
43+
const columns = [
44+
{
45+
key: 0,
46+
width: 100,
47+
minWidth: 100,
48+
},
49+
];
50+
51+
const wrapper = mount(<Table columns={columns} tableLayout="fixed" />);
52+
expect(wrapper.find('colgroup col').at(0).props().style.minWidth).toBeFalsy();
53+
});
2954
});

0 commit comments

Comments
 (0)