Skip to content
Closed
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
34 changes: 19 additions & 15 deletions src/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ export default class Table extends React.Component {
<div
key="headTable"
className={`${prefixCls}-header`}
ref={fixed ? null : 'headTable'}
ref={fixed ? null : this.saveRef('headTable')}
style={headStyle}
onScroll={this.handleBodyScrollLeft}
>
Expand All @@ -518,7 +518,7 @@ export default class Table extends React.Component {
key="bodyTable"
className={`${prefixCls}-body`}
style={bodyStyle}
ref="bodyTable"
ref={this.saveRef('bodyTable')}
onScroll={this.handleBodyScroll}
>
{renderTable(!useFixedHeader)}
Expand All @@ -543,7 +543,7 @@ export default class Table extends React.Component {
<div
className={`${prefixCls}-body-inner`}
style={innerBodyStyle}
ref={refName}
ref={this.saveRef(refName)}
onScroll={this.handleBodyScroll}
>
{renderTable(!useFixedHeader)}
Expand Down Expand Up @@ -617,7 +617,7 @@ export default class Table extends React.Component {
}

setScrollPositionClassName(target) {
const node = target || this.refs.bodyTable;
const node = target || this.bodyTable;
const scrollToLeft = node.scrollLeft === 0;
const scrollToRight = node.scrollLeft + 1 >=
node.children[0].getBoundingClientRect().width -
Expand Down Expand Up @@ -646,10 +646,10 @@ export default class Table extends React.Component {
return;
}
const { prefixCls } = this.props;
const headRows = this.refs.headTable ?
this.refs.headTable.querySelectorAll('thead') :
this.refs.bodyTable.querySelectorAll('thead');
const bodyRows = this.refs.bodyTable.querySelectorAll(`.${prefixCls}-row`) || [];
const headRows = this.headTable ?
this.headTable.querySelectorAll('thead') :
this.bodyTable.querySelectorAll('thead');
const bodyRows = this.bodyTable.querySelectorAll(`.${prefixCls}-row`) || [];
const fixedColumnsHeadRowsHeight = [].map.call(
headRows, row => row.getBoundingClientRect().height || 'auto'
);
Expand All @@ -667,11 +667,11 @@ export default class Table extends React.Component {
}

resetScrollX() {
if (this.refs.headTable) {
this.refs.headTable.scrollLeft = 0;
if (this.headTable) {
this.headTable.scrollLeft = 0;
}
if (this.refs.bodyTable) {
this.refs.bodyTable.scrollLeft = 0;
if (this.bodyTable) {
this.bodyTable.scrollLeft = 0;
}
}

Expand All @@ -692,7 +692,7 @@ export default class Table extends React.Component {
handleBodyScrollLeft = (e) => {
const target = e.target;
const { scroll = {} } = this.props;
const { headTable, bodyTable } = this.refs;
const { headTable, bodyTable } = this;
if (target.scrollLeft !== this.lastScrollLeft && scroll.x) {
if (target === bodyTable && headTable) {
headTable.scrollLeft = target.scrollLeft;
Expand All @@ -708,7 +708,7 @@ export default class Table extends React.Component {
handleBodyScrollTop = (e) => {
const target = e.target;
const { scroll = {} } = this.props;
const { headTable, bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this.refs;
const { headTable, bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this;
if (target.scrollTop !== this.lastScrollTop && scroll.y && target !== headTable) {
const scrollTop = target.scrollTop;
if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
Expand Down Expand Up @@ -736,6 +736,10 @@ export default class Table extends React.Component {
});
}

saveRef = (name) => (node) => {
this[name] = node;
}

render() {
const props = this.props;
const prefixCls = props.prefixCls;
Expand Down Expand Up @@ -767,7 +771,7 @@ export default class Table extends React.Component {
: content;

return (
<div ref={node => (this.tableNode = node)} className={className} style={props.style}>
<div ref={this.saveRef('tableNode')} className={className} style={props.style}>
{this.getTitle()}
<div className={`${prefixCls}-content`}>
{scrollTable}
Expand Down
8 changes: 4 additions & 4 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ describe('Table', () => {
/>
);
const inst = wrapper.instance();
const headTable = wrapper.ref('headTable');
const bodyTable = wrapper.ref('bodyTable');
const fixedColumnsBodyLeft = wrapper.ref('fixedColumnsBodyLeft');
const fixedColumnsBodyRight = wrapper.ref('fixedColumnsBodyRight');
const headTable = wrapper.wrap(inst.headTable);
const bodyTable = wrapper.wrap(inst.bodyTable);
const fixedColumnsBodyLeft = wrapper.wrap(inst.fixedColumnsBodyLeft);
const fixedColumnsBodyRight = wrapper.wrap(inst.fixedColumnsBodyRight);

expect(inst.lastScrollLeft).toBe(undefined);

Expand Down