Skip to content

Commit

Permalink
fix: scroll.x not working when no fixed column exists (#1023)
Browse files Browse the repository at this point in the history
* fix: table col cal

* test: add test case
  • Loading branch information
zombieJ authored Sep 19, 2023
1 parent aa2aa44 commit 4ffafff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
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"]
}

1 comment on commit 4ffafff

@vercel
Copy link

@vercel vercel bot commented on 4ffafff Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

table – ./

table-react-component.vercel.app
table-git-master-react-component.vercel.app

Please sign in to comment.