Skip to content

Commit

Permalink
chore(*): remove useless event bind for table
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Jan 2, 2020
1 parent 03244b8 commit c8d873c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
57 changes: 34 additions & 23 deletions src/table/lock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function lock(BaseComponent) {
getTableInstance: PropTypes.func,
getLockNode: PropTypes.func,
onLockBodyScroll: PropTypes.func,
onLockBodyWheel: PropTypes.func,
onLockBodyLRScroll: PropTypes.func,
onRowMouseEnter: PropTypes.func,
onRowMouseLeave: PropTypes.func,
};
Expand All @@ -48,8 +48,8 @@ export default function lock(BaseComponent) {
return {
getTableInstance: this.getTableInstance,
getLockNode: this.getNode,
onLockBodyWheel: this.onLockBodyWheel,
onLockBodyScroll: this.onLockBodyScroll,
onLockBodyLRScroll: this.onLockBodyLRScroll,
onRowMouseEnter: this.onRowMouseEnter,
onRowMouseLeave: this.onRowMouseLeave,
};
Expand Down Expand Up @@ -254,30 +254,39 @@ export default function lock(BaseComponent) {
}
}

onLockBodyWheel = e => {
const y = e.deltaY;

onLockBodyLRScroll = (event, lockType) => {
if (this.isLock()) {
const lockRightBody = this.bodyRightNode,
lockLeftBody = this.bodyLeftNode,
scrollNode = this.bodyNode,
{ scrollTop } = scrollNode;

if (lockLeftBody) {
lockLeftBody.scrollTop = y;
}
if (lockRightBody) {
lockRightBody.scrollTop = y;
bodyNode = this.bodyNode;

let arr = [],
distScrollTop = 0;

// scroll on lock left columns
if (lockType === 'left') {
arr = [bodyNode];
distScrollTop = lockLeftBody.scrollTop;
// scroll on lock left columns
} else if (lockType === 'right') {
arr = [bodyNode];
distScrollTop = lockRightBody.scrollTop;
}
scrollNode.scrollTop = scrollTop + y;

arr.forEach(node => {
if (node && node.scrollTop !== distScrollTop) {
node.scrollTop = distScrollTop;
}
});
}
};

onLockBodyScroll = () => {
onLockBodyScroll = event => {
if (this.isLock()) {
const { rtl } = this.props;
const lockRightBody = this.bodyRightNode,
lockLeftBody = this.bodyLeftNode,
bodyNode = this.bodyNode,
lockRightTable = rtl
? this.getWrapperNode('left')
: this.getWrapperNode('right'),
Expand All @@ -286,14 +295,16 @@ export default function lock(BaseComponent) {
: this.getWrapperNode('left'),
shadowClassName = 'shadow';

const x = this.bodyNode.scrollLeft,
y = this.bodyNode.scrollTop;
if (lockLeftBody) {
lockLeftBody.scrollTop = y;
}
if (lockRightBody) {
lockRightBody.scrollTop = y;
}
const x = this.bodyNode.scrollLeft;
const arr = [lockLeftBody, lockRightBody],
distScrollTop = bodyNode.scrollTop;

arr.forEach(node => {
if (node && node.scrollTop !== distScrollTop) {
node.scrollTop = distScrollTop;
}
});

if (x === 0) {
lockLeftTable &&
dom.removeClass(lockLeftTable, shadowClassName);
Expand Down
24 changes: 10 additions & 14 deletions src/table/lock/body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class LockBody extends React.Component {
...FixedBody.contextTypes,
getLockNode: PropTypes.func,
onLockBodyScroll: PropTypes.func,
onLockBodyWheel: PropTypes.func,
onLockBodyLRScroll: PropTypes.func,
lockType: PropTypes.oneOf(['left', 'right']),
};

Expand All @@ -25,24 +25,20 @@ export default class LockBody extends React.Component {
);
}

onBodyScroll = () => {
this.context.onLockBodyScroll();
onBodyScroll = event => {
this.context.onLockBodyScroll(event);
};

onBodyWheel = e => {
this.context.onLockBodyWheel(e);
onBodyLRScroll = event => {
const { lockType } = this.context;
this.context.onLockBodyLRScroll(event, lockType);
};

render() {
const { lockType } = this.context;

const events = lockType
? {
onWheel: this.onBodyWheel,
}
: {
onScroll: this.onBodyScroll,
};
return <FixedBody {...this.props} {...events} />;
const event = {
onScroll: lockType ? this.onBodyLRScroll : this.onBodyScroll,
};
return <FixedBody {...this.props} {...event} />;
}
}
23 changes: 7 additions & 16 deletions src/table/virtual/body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class VirtualBody extends React.Component {
onScroll: PropTypes.func,
onVirtualScroll: PropTypes.func,
onLockBodyScroll: PropTypes.func,
onLockBodyWheel: PropTypes.func,
onLockBodyLRScroll: PropTypes.func,
bodyHeight: PropTypes.number,
innerTop: PropTypes.number,
getNode: PropTypes.func,
Expand Down Expand Up @@ -49,35 +49,26 @@ export default class VirtualBody extends React.Component {
};

onScroll = current => {
const { lockType } = this.context;
// for fixed
this.context.onScroll(current);
// for lock
this.context.onLockBodyScroll();
lockType
? this.context.onLockBodyLRScroll(current, lockType)
: this.context.onLockBodyScroll(current);
// for virtual
this.context.onVirtualScroll();
};

onWheel = e => {
this.context.onLockBodyWheel(e);
};

render() {
const { prefix, className, colGroup, ...others } = this.props;
const { maxBodyHeight, bodyHeight, innerTop, lockType } = this.context;

const events = lockType
? {
onWheel: this.onWheel,
}
: {
onScroll: this.onScroll,
};
const { maxBodyHeight, bodyHeight, innerTop } = this.context;

return (
<div
style={{ maxHeight: maxBodyHeight }}
className={className}
{...events}
onScroll={this.onScroll}
>
<div
style={{
Expand Down

0 comments on commit c8d873c

Please sign in to comment.