From febc578e436173f6def8b1fccd293985592c1dbe Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 21 Jun 2016 11:44:09 +0800 Subject: [PATCH] auto match scroll bar height --- assets/index.less | 6 +++--- src/Table.jsx | 10 ++++++++++ src/utils.js | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/utils.js diff --git a/assets/index.less b/assets/index.less index a87b2ca4c..ae02c1345 100644 --- a/assets/index.less +++ b/assets/index.less @@ -13,7 +13,7 @@ .@{tablePrefixCls}-scroll { overflow: auto; - } + } .@{tablePrefixCls}-header { overflow: hidden; @@ -27,10 +27,10 @@ &-fixed-header &-body-inner { height: 100%; - overflow-y: scroll; + overflow: scroll; } - &-fixed-header &-header { + &-fixed-header &-scroll &-header { overflow-x: scroll; padding-bottom: 20px; margin-bottom: -20px; diff --git a/src/Table.jsx b/src/Table.jsx index c819ae028..9094d1fc5 100644 --- a/src/Table.jsx +++ b/src/Table.jsx @@ -1,5 +1,6 @@ import React from 'react'; import TableRow from './TableRow'; +import { measureScrollbar } from './utils'; const Table = React.createClass({ propTypes: { @@ -344,6 +345,7 @@ const Table = React.createClass({ const { prefixCls, scroll = {} } = this.props; let { useFixedHeader } = this.props; const bodyStyle = { ...this.props.bodyStyle }; + const headStyle = {}; let tableClassName = ''; if (scroll.x || columns) { @@ -355,6 +357,13 @@ const Table = React.createClass({ bodyStyle.height = bodyStyle.height || scroll.y; bodyStyle.overflowY = bodyStyle.overflowY || 'auto'; useFixedHeader = true; + + // Add negative margin bottom for scroll bar overflow bug + const scrollbarWidth = measureScrollbar(); + if (scrollbarWidth > 0) { + (fixed ? bodyStyle : headStyle).marginBottom = `-${scrollbarWidth}px`; + (fixed ? bodyStyle : headStyle).paddingBottom = '0px'; + } } const renderTable = (hasHead = true, hasBody = true) => { @@ -386,6 +395,7 @@ const Table = React.createClass({
{renderTable(true, false)} diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 000000000..a981a7ce7 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,27 @@ +let scrollbarWidth; + +// Measure scrollbar width for padding body during modal show/hide +const scrollbarMeasure = { + position: 'absolute', + top: '-9999px', + width: '50px', + height: '50px', + overflow: 'scroll', +}; + +export function measureScrollbar() { + if (scrollbarWidth) { + return scrollbarWidth; + } + const scrollDiv = document.createElement('div'); + for (const scrollProp in scrollbarMeasure) { + if (scrollbarMeasure.hasOwnProperty(scrollProp)) { + scrollDiv.style[scrollProp] = scrollbarMeasure[scrollProp]; + } + } + document.body.appendChild(scrollDiv); + const width = scrollDiv.offsetWidth - scrollDiv.clientWidth; + document.body.removeChild(scrollDiv); + scrollbarWidth = width; + return scrollbarWidth; +}