2019-01-12 03:33:27 +00:00
|
|
|
let scrollbarVerticalSize;
|
|
|
|
let scrollbarHorizontalSize;
|
2018-03-24 14:02:24 +00:00
|
|
|
|
|
|
|
// Measure scrollbar width for padding body during modal show/hide
|
|
|
|
const scrollbarMeasure = {
|
|
|
|
position: 'absolute',
|
|
|
|
top: '-9999px',
|
|
|
|
width: '50px',
|
|
|
|
height: '50px',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-24 14:02:24 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
export const INTERNAL_COL_DEFINE = 'RC_TABLE_INTERNAL_COL_DEFINE';
|
|
|
|
|
|
|
|
export function measureScrollbar({ direction = 'vertical', prefixCls }) {
|
2018-03-24 14:02:24 +00:00
|
|
|
if (typeof document === 'undefined' || typeof window === 'undefined') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return 0;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const isVertical = direction === 'vertical';
|
2019-01-01 14:30:06 +00:00
|
|
|
if (isVertical && scrollbarVerticalSize) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return scrollbarVerticalSize;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
if (!isVertical && scrollbarHorizontalSize) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return scrollbarHorizontalSize;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const scrollDiv = document.createElement('div');
|
2018-09-05 13:28:54 +00:00
|
|
|
Object.keys(scrollbarMeasure).forEach(scrollProp => {
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollDiv.style[scrollProp] = scrollbarMeasure[scrollProp];
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
// apply hide scrollbar className ahead
|
|
|
|
scrollDiv.className = `${prefixCls}-hide-scrollbar scroll-div-append-to-body`;
|
|
|
|
|
2019-01-01 14:30:06 +00:00
|
|
|
// Append related overflow style
|
|
|
|
if (isVertical) {
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollDiv.style.overflowY = 'scroll';
|
2019-01-01 14:30:06 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollDiv.style.overflowX = 'scroll';
|
2019-01-01 14:30:06 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
document.body.appendChild(scrollDiv);
|
|
|
|
let size = 0;
|
2019-01-01 14:30:06 +00:00
|
|
|
if (isVertical) {
|
2019-01-12 03:33:27 +00:00
|
|
|
size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
|
|
|
|
scrollbarVerticalSize = size;
|
2020-03-07 11:45:13 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
size = scrollDiv.offsetHeight - scrollDiv.clientHeight;
|
|
|
|
scrollbarHorizontalSize = size;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
document.body.removeChild(scrollDiv);
|
|
|
|
return size;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export function debounce(func, wait, immediate) {
|
|
|
|
let timeout;
|
|
|
|
function debounceFunc(...args) {
|
|
|
|
const context = this;
|
2018-03-24 14:02:24 +00:00
|
|
|
// https://fb.me/react-event-pooling
|
|
|
|
if (args[0] && args[0].persist) {
|
2019-01-12 03:33:27 +00:00
|
|
|
args[0].persist();
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
const later = () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
timeout = null;
|
2018-03-24 14:02:24 +00:00
|
|
|
if (!immediate) {
|
2019-01-12 03:33:27 +00:00
|
|
|
func.apply(context, args);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const callNow = immediate && !timeout;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
2018-03-24 14:02:24 +00:00
|
|
|
if (callNow) {
|
2019-01-12 03:33:27 +00:00
|
|
|
func.apply(context, args);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
debounceFunc.cancel = function cancel() {
|
2018-03-24 14:02:24 +00:00
|
|
|
if (timeout) {
|
2019-01-12 03:33:27 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return debounceFunc;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export function remove(array, item) {
|
|
|
|
const index = array.indexOf(item);
|
|
|
|
const front = array.slice(0, index);
|
|
|
|
const last = array.slice(index + 1, array.length);
|
|
|
|
return front.concat(last);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2020-07-23 10:34:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns only data- and aria- key/value pairs
|
|
|
|
* @param {object} props
|
|
|
|
*/
|
|
|
|
export function getDataAndAriaProps(props) {
|
|
|
|
return Object.keys(props).reduce((memo, key) => {
|
|
|
|
if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-') {
|
|
|
|
memo[key] = props[key];
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, {});
|
|
|
|
}
|