Skip to content

Commit 88b0575

Browse files
authored
fix(Table): fix table scroll width not excluding scroll bar width (#461)
* fix(Table): fix table scroll width not excluding scroll bar width * fix: update test
1 parent a21d9eb commit 88b0575

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/utils/usePosition.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ const usePosition = (props: PositionProps) => {
100100

101101
updateWheelElementPosition(true);
102102

103+
// Whether to show the horizontal scroll bar
104+
const hasHorizontalScrollbar = contentWidth.current > tableWidth.current;
105+
const scrollbarWidth = hasHorizontalScrollbar ? 0 : SCROLLBAR_WIDTH;
106+
103107
const leftShadowClassName = prefix('cell-group-left-shadow');
104108
const rightShadowClassName = prefix('cell-group-right-shadow');
105109
const showLeftShadow = scrollX.current < 0;
106110
const showRightShadow =
107-
tableWidth.current - contentWidth.current - SCROLLBAR_WIDTH !== scrollX.current;
111+
tableWidth.current - contentWidth.current - scrollbarWidth !== scrollX.current;
108112

109113
toggleClass(fixedLeftGroups as unknown as HTMLElement[], leftShadowClassName, showLeftShadow);
110114
toggleClass(

src/utils/useTableDimension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,17 @@ const useTableDimension = <Row extends RowDataType, Key>(props: TableDimensionPr
181181
const row = table?.querySelector(`.${prefix('row')}:not(.virtualized)`);
182182
const nextContentWidth = row ? getWidth(row) : 0;
183183

184-
contentWidth.current = nextContentWidth - (autoHeight ? SCROLLBAR_WIDTH : 0);
184+
// Whether to show the horizontal scroll bar
185+
const hasHorizontalScrollbar = contentWidth.current > tableWidth.current;
186+
const scrollbarWidth = hasHorizontalScrollbar ? 0 : SCROLLBAR_WIDTH;
187+
188+
contentWidth.current = nextContentWidth - (autoHeight ? scrollbarWidth : 0);
185189
columnCount.current = row?.querySelectorAll(`.${prefix('cell')}`).length || 0;
186190

187191
// The value of SCROLLBAR_WIDTH is subtracted so that the scroll bar does not block the content part.
188192
// There is no vertical scroll bar after autoHeight.
189193
const minScrollWidth =
190-
-(nextContentWidth - tableWidth.current) - (autoHeight ? 0 : SCROLLBAR_WIDTH);
194+
-(nextContentWidth - tableWidth.current) - (autoHeight ? 0 : scrollbarWidth);
191195

192196
if (minScrollX.current !== minScrollWidth) {
193197
minScrollX.current = minScrollWidth;

test/TableSpec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,9 @@ describe('Table', () => {
330330
</Table>
331331
);
332332

333-
const SCROLLBAR_WIDTH = 10;
334333
const tableWidth = 200;
335334
const contextWidth = 400;
336-
const width = Math.floor((tableWidth / (contextWidth - SCROLLBAR_WIDTH)) * tableWidth);
335+
const width = Math.floor((tableWidth / contextWidth) * tableWidth);
337336

338337
const scrollbarHandleWidth = Math.floor(
339338
getWidth(instance.querySelector('.rs-table-scrollbar-handle'))

0 commit comments

Comments
 (0)