tdesign-react
Version:
TDesign Component for React
222 lines (218 loc) • 10.1 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _slicedToArray } from '../../_chunks/dep-48805ab8.js';
import { useRef, useState, useMemo, useEffect } from 'react';
import { on, off } from '../../_util/listener.js';
import '../../_chunks/dep-026a4c6b.js';
import '../../_chunks/dep-3a09424a.js';
import 'lodash-es';
function useAffix(props, _ref) {
var showElement = _ref.showElement;
var tableContentRef = useRef(null);
var affixHeaderRef = useRef(null);
var affixFooterRef = useRef(null);
var horizontalScrollbarRef = useRef(null);
var paginationRef = useRef(null);
var _useState = useState(true),
_useState2 = _slicedToArray(_useState, 2),
showAffixHeader = _useState2[0],
setShowAffixHeader = _useState2[1];
var _useState3 = useState(true),
_useState4 = _slicedToArray(_useState3, 2),
showAffixFooter = _useState4[0],
setShowAffixFooter = _useState4[1];
var _useState5 = useState(true),
_useState6 = _slicedToArray(_useState5, 2),
showAffixPagination = _useState6[0],
setShowAffixPagination = _useState6[1];
var isVirtualScroll = useMemo(function () {
return props.scroll && props.scroll.type === "virtual" && (props.scroll.threshold || 100) < props.data.length;
}, [props.data.length, props.scroll]);
var isAffixed = useMemo(function () {
return !!(props.headerAffixedTop || props.footerAffixedBottom || props.horizontalScrollAffixedBottom);
}, [props.footerAffixedBottom, props.headerAffixedTop, props.horizontalScrollAffixedBottom]);
var lastScrollLeft = 0;
var onHorizontalScroll = function onHorizontalScroll(scrollElement) {
if (!isAffixed && !isVirtualScroll) return;
var target = scrollElement;
if (!target && tableContentRef.current) {
lastScrollLeft = 0;
target = tableContentRef.current;
}
if (!target) return;
var left = target.scrollLeft;
if (lastScrollLeft === left) return;
lastScrollLeft = left;
var toUpdateScrollElement = [tableContentRef.current, affixHeaderRef.current, affixFooterRef.current, horizontalScrollbarRef.current];
for (var i = 0, len = toUpdateScrollElement.length; i < len; i++) {
if (toUpdateScrollElement[i] && scrollElement !== toUpdateScrollElement[i]) {
toUpdateScrollElement[i].scrollLeft = left;
}
}
};
var isAffixedBottomElementShow = function isAffixedBottomElementShow(elementRect, tableRect, headerHeight) {
return tableRect.top + headerHeight < elementRect.top && elementRect.top > elementRect.height;
};
var getOffsetTop = function getOffsetTop(props2) {
if (typeof props2 === "boolean") return 0;
return props2.offsetTop || 0;
};
var updateAffixHeaderOrFooter = function updateAffixHeaderOrFooter() {
var _tableContentRef$curr, _tableContentRef$curr2, _affixFooterRef$curre;
if (!isAffixed && !isVirtualScroll) return;
var pos = (_tableContentRef$curr = tableContentRef.current) === null || _tableContentRef$curr === void 0 ? void 0 : _tableContentRef$curr.getBoundingClientRect();
if (!pos) return;
var headerRect = (_tableContentRef$curr2 = tableContentRef.current) === null || _tableContentRef$curr2 === void 0 || (_tableContentRef$curr2 = _tableContentRef$curr2.querySelector("thead")) === null || _tableContentRef$curr2 === void 0 ? void 0 : _tableContentRef$curr2.getBoundingClientRect();
var headerHeight = (headerRect === null || headerRect === void 0 ? void 0 : headerRect.height) || 0;
var footerRect = (_affixFooterRef$curre = affixFooterRef.current) === null || _affixFooterRef$curre === void 0 ? void 0 : _affixFooterRef$curre.getBoundingClientRect();
if ((props.headerAffixedTop || isVirtualScroll) && affixHeaderRef.current) {
var offsetTop = getOffsetTop(props.headerAffixProps || props.headerAffixedTop);
var footerHeight = (footerRect === null || footerRect === void 0 ? void 0 : footerRect.height) || 0;
var r = Math.abs(pos.top) < pos.height - headerHeight - offsetTop - footerHeight;
if (isVirtualScroll) {
r = pos.top > -1 * headerRect.height;
}
setShowAffixHeader(r);
}
if (props.footerAffixedBottom && affixFooterRef.current) {
var show = isAffixedBottomElementShow(footerRect, pos, headerHeight);
setShowAffixFooter(show);
} else if (props.horizontalScrollAffixedBottom && horizontalScrollbarRef.current) {
var horizontalScrollbarRect = horizontalScrollbarRef.current.getBoundingClientRect();
var _show = isAffixedBottomElementShow(horizontalScrollbarRect, pos, headerHeight);
setShowAffixFooter(_show);
}
if (props.paginationAffixedBottom && paginationRef.current) {
var pageRect = paginationRef.current.getBoundingClientRect();
var _show2 = isAffixedBottomElementShow(pageRect, pos, headerHeight);
setShowAffixPagination(_show2);
}
};
var onDocumentScroll = function onDocumentScroll() {
updateAffixHeaderOrFooter();
};
var onFootScroll = function onFootScroll() {
onHorizontalScroll(affixFooterRef.current);
};
var onHeaderScroll = function onHeaderScroll() {
onHorizontalScroll(affixHeaderRef.current);
};
var horizontalScrollbarScroll = function horizontalScrollbarScroll() {
onHorizontalScroll(horizontalScrollbarRef.current);
};
var onTableContentScroll = function onTableContentScroll() {
onHorizontalScroll(tableContentRef.current);
};
var onFootMouseEnter = function onFootMouseEnter() {
on(affixFooterRef.current, "scroll", onFootScroll);
};
var onFootMouseLeave = function onFootMouseLeave() {
off(affixFooterRef.current, "scroll", onFootScroll);
};
var onHeaderMouseEnter = function onHeaderMouseEnter() {
on(affixHeaderRef.current, "scroll", onHeaderScroll);
};
var onHeaderMouseLeave = function onHeaderMouseLeave() {
off(affixHeaderRef.current, "scroll", onHeaderScroll);
};
var onScrollbarMouseEnter = function onScrollbarMouseEnter() {
on(horizontalScrollbarRef.current, "scroll", horizontalScrollbarScroll);
};
var onScrollbarMouseLeave = function onScrollbarMouseLeave() {
off(horizontalScrollbarRef.current, "scroll", horizontalScrollbarScroll);
};
var onTableContentMouseEnter = function onTableContentMouseEnter() {
on(tableContentRef.current, "scroll", onTableContentScroll);
};
var onTableContentMouseLeave = function onTableContentMouseLeave() {
off(tableContentRef.current, "scroll", onTableContentScroll);
};
var addHorizontalScrollListeners = function addHorizontalScrollListeners() {
if (affixHeaderRef.current) {
on(affixHeaderRef.current, "mouseenter", onHeaderMouseEnter);
on(affixHeaderRef.current, "mouseleave", onHeaderMouseLeave);
}
if (props.footerAffixedBottom && affixFooterRef.current) {
on(affixFooterRef.current, "mouseenter", onFootMouseEnter);
on(affixFooterRef.current, "mouseleave", onFootMouseLeave);
}
if (props.horizontalScrollAffixedBottom && horizontalScrollbarRef.current) {
on(horizontalScrollbarRef.current, "mouseenter", onScrollbarMouseEnter);
on(horizontalScrollbarRef.current, "mouseleave", onScrollbarMouseLeave);
}
if ((isAffixed || isVirtualScroll) && tableContentRef.current) {
on(tableContentRef.current, "mouseenter", onTableContentMouseEnter);
on(tableContentRef.current, "mouseleave", onTableContentMouseLeave);
}
};
var removeHorizontalScrollListeners = function removeHorizontalScrollListeners() {
if (affixHeaderRef.current) {
off(affixHeaderRef.current, "mouseenter", onHeaderMouseEnter);
off(affixHeaderRef.current, "mouseleave", onHeaderMouseLeave);
}
if (affixFooterRef.current) {
off(affixFooterRef.current, "mouseenter", onFootMouseEnter);
off(affixFooterRef.current, "mouseleave", onFootMouseLeave);
}
if (tableContentRef.current) {
off(tableContentRef.current, "mouseenter", onTableContentMouseEnter);
off(tableContentRef.current, "mouseleave", onTableContentMouseLeave);
}
if (horizontalScrollbarRef.current) {
off(horizontalScrollbarRef.current, "mouseenter", onScrollbarMouseEnter);
off(horizontalScrollbarRef.current, "mouseleave", onScrollbarMouseLeave);
}
};
var addVerticalScrollListener = function addVerticalScrollListener() {
if (typeof document === "undefined") return;
if (!isAffixed && !props.paginationAffixedBottom) return;
var timer = setTimeout(function () {
if (isAffixed || props.paginationAffixedBottom) {
on(document, "scroll", onDocumentScroll);
} else {
off(document, "scroll", onDocumentScroll);
}
clearTimeout(timer);
});
};
useEffect(function () {
var timer = setTimeout(function () {
addHorizontalScrollListeners();
onHorizontalScroll();
updateAffixHeaderOrFooter();
clearTimeout(timer);
});
return removeHorizontalScrollListeners;
}, [affixHeaderRef, affixFooterRef, horizontalScrollbarRef, tableContentRef, showElement]);
useEffect(function () {
addVerticalScrollListener();
return function () {
off(document, "scroll", onDocumentScroll);
};
}, [isAffixed]);
useEffect(function () {
addHorizontalScrollListeners();
onHorizontalScroll();
}, [props.data, props.columns, props.headerAffixedTop, props.footerAffixedBottom, props.horizontalScrollAffixedBottom, props.lazyLoad]);
var setTableContentRef = function setTableContentRef(tableContent) {
tableContentRef.current = tableContent;
addVerticalScrollListener();
};
return {
showAffixHeader: showAffixHeader,
showAffixFooter: showAffixFooter,
showAffixPagination: showAffixPagination,
affixHeaderRef: affixHeaderRef,
affixFooterRef: affixFooterRef,
horizontalScrollbarRef: horizontalScrollbarRef,
paginationRef: paginationRef,
onHorizontalScroll: onHorizontalScroll,
setTableContentRef: setTableContentRef,
updateAffixHeaderOrFooter: updateAffixHeaderOrFooter
};
}
export { useAffix as default };
//# sourceMappingURL=useAffix.js.map