Skip to content

Commit f2cc27d

Browse files
yykoypjyangpj17
andauthored
fix: no scrollbar size issue (#816)
* fix: no scrollbar size issue * test: add test case of scrollbar size * refactor Co-authored-by: yangpj17 <[email protected]>
1 parent d0d9669 commit f2cc27d

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/Table.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
384384
const fullTableRef = React.useRef<HTMLDivElement>();
385385
const scrollHeaderRef = React.useRef<HTMLDivElement>();
386386
const scrollBodyRef = React.useRef<HTMLDivElement>();
387+
const scrollBodyContainerRef = React.useRef<HTMLDivElement>();
387388
const scrollSummaryRef = React.useRef<HTMLDivElement>();
388389
const [pingedLeft, setPingedLeft] = React.useState(false);
389390
const [pingedRight, setPingedRight] = React.useState(false);
@@ -534,7 +535,11 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
534535
const [supportSticky, setSupportSticky] = React.useState(true); // Only IE not support, we mark as support first
535536

536537
React.useEffect(() => {
537-
setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.current).width);
538+
if (scrollBodyRef.current instanceof Element) {
539+
setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.current).width);
540+
} else {
541+
setScrollbarSize(getTargetScrollBarSize(scrollBodyContainerRef.current).width);
542+
}
538543
setSupportSticky(isStyleSupport('position', 'sticky'));
539544
}, []);
540545

@@ -781,7 +786,9 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
781786
props={{ ...props, stickyOffsets, mergedExpandedKeys }}
782787
>
783788
{title && <Panel className={`${prefixCls}-title`}>{title(mergedData)}</Panel>}
784-
<div className={`${prefixCls}-container`}>{groupTableNode}</div>
789+
<div ref={scrollBodyContainerRef} className={`${prefixCls}-container`}>
790+
{groupTableNode}
791+
</div>
785792
{footer && <Panel className={`${prefixCls}-footer`}>{footer(mergedData)}</Panel>}
786793
</MemoTableContent>
787794
</div>

tests/Table.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,4 +1020,18 @@ describe('Table.Basic', () => {
10201020
expect(wrapper.find('td.rc-table-cell-row-hover')).toHaveLength(1);
10211021
});
10221022
});
1023+
it('should get scrollbar size', () => {
1024+
const tColumns = [{ title: 'Name', dataIndex: 'name', key: 'name', width: 100 }];
1025+
const wrapper = mount(
1026+
createTable({
1027+
columns: tColumns,
1028+
scroll: { y: 100 },
1029+
components: {
1030+
body: () => <React.Fragment />,
1031+
},
1032+
}),
1033+
);
1034+
expect(wrapper.render()).toMatchSnapshot();
1035+
expect(wrapper.find('col')).toHaveLength(tColumns.length + 1);
1036+
});
10231037
});
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export default () => 15;
22

3-
export const getTargetScrollBarSize = () => ({ width: 15, height: 15 });
3+
export const getTargetScrollBarSize = (target: HTMLElement) => {
4+
if (!target || !(target instanceof Element)) {
5+
return { width: 0, height: 0 };
6+
}
7+
return { width: 15, height: 15 };
8+
};

tests/__snapshots__/Table.spec.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,48 @@ exports[`Table.Basic renders rowSpan correctly 1`] = `
735735
</div>
736736
`;
737737

738+
exports[`Table.Basic should get scrollbar size 1`] = `
739+
<div
740+
class="rc-table rc-table-fixed-header"
741+
>
742+
<div
743+
class="rc-table-container"
744+
>
745+
<div
746+
class="rc-table-header"
747+
style="overflow: hidden;"
748+
>
749+
<table
750+
style="table-layout: fixed;"
751+
>
752+
<colgroup>
753+
<col
754+
style="width: 85px;"
755+
/>
756+
<col
757+
style="width: 15px;"
758+
/>
759+
</colgroup>
760+
<thead
761+
class="rc-table-thead"
762+
>
763+
<tr>
764+
<th
765+
class="rc-table-cell"
766+
>
767+
Name
768+
</th>
769+
<th
770+
class="rc-table-cell rc-table-cell-scrollbar"
771+
/>
772+
</tr>
773+
</thead>
774+
</table>
775+
</div>
776+
</div>
777+
</div>
778+
`;
779+
738780
exports[`Table.Basic syntactic sugar 1`] = `
739781
<div
740782
class="rc-table"

0 commit comments

Comments
 (0)