Skip to content
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: 4 additions & 0 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {

// Fill scrollX
if (typeof scrollX !== 'number') {
if (process.env.NODE_ENV !== 'production') {
warning(!scrollX, '`scroll.x` in virtual table must be number.');
}

scrollX = 1;
}

Expand Down
10 changes: 6 additions & 4 deletions src/hooks/useColumns/useWidthColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ export default function useWidthColumns(
return clone;
});

const maxFitWidth = Math.max(scrollWidth, clientWidth);

// If realTotal is less than clientWidth,
// We need extend column width
if (realTotal < clientWidth) {
const scale = clientWidth / realTotal;
if (realTotal < maxFitWidth) {
const scale = maxFitWidth / realTotal;

restWidth = clientWidth;
restWidth = maxFitWidth;

filledColumns.forEach((col: any, index) => {
const colWidth = Math.floor(col.width * scale);
Expand All @@ -83,7 +85,7 @@ export default function useWidthColumns(
});
}

return [filledColumns, Math.max(realTotal, clientWidth)];
return [filledColumns, Math.max(realTotal, maxFitWidth)];
}

return [flattenColumns, scrollWidth];
Expand Down
28 changes: 26 additions & 2 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ describe('Table.Virtual', () => {
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

getTable({
scroll: {} as any,
scroll: {
x: true,
} as any,
});

expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.x` in virtual table must be number.');
expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.y` in virtual table must be number.');
});

Expand Down Expand Up @@ -199,7 +202,7 @@ describe('Table.Virtual', () => {
},
],
scroll: {
x: 1128,
x: 100,
y: 10,
},
data: [{}],
Expand Down Expand Up @@ -244,4 +247,25 @@ describe('Table.Virtual', () => {
expect(container.querySelectorAll('col')[0]).toHaveStyle({ width: '100px' });
expect(container.querySelectorAll('col')[1]).toHaveStyle({ width: '100px' });
});

it('should fill width as scrollX if scrollX is larger', async () => {
const { container } = getTable({
columns: [
{
width: 100,
},
],
scroll: {
x: 1128,
y: 10,
},
getContainerWidth: () => 200,
data: [{}],
});

expect(container.querySelector('.rc-virtual-list')).toHaveAttribute(
'data-scroll-width',
'1128',
);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
},
"types": ["vitest/globals"]
},
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
}