Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): affixed header 1px #1312

Merged
merged 4 commits into from
Aug 18, 2022
Merged
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
7 changes: 4 additions & 3 deletions src/table/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const BaseTable = forwardRef((props: TBaseTableProps, ref) => {
tableHtmlElement: tableElmRef.current,
tableContentElement: tableContentRef.current,
affixHeaderElement: affixHeaderRef.current,
refreshTable,
}));

const onFixedChange = () => {
Expand Down Expand Up @@ -219,11 +220,11 @@ const BaseTable = forwardRef((props: TBaseTableProps, ref) => {
marginTop: onlyVirtualScrollBordered ? `${borderWidth}px` : 0,
};
// 多级表头左边线缺失
const affixedMultipleHeaderLeftBorder = props.bordered && isMultipleHeader ? 1 : 0;
const affixedLeftBorder = props.bordered ? 1 : 0;
const affixedHeader = Boolean(props.headerAffixedTop && tableWidth) && (
<div
ref={affixHeaderRef}
style={{ width: `${tableWidth - affixedMultipleHeaderLeftBorder}px`, opacity: headerOpacity }}
style={{ width: `${tableWidth - affixedLeftBorder}px`, opacity: headerOpacity }}
className={classNames(['scrollbar', { [tableBaseClass.affixedHeaderElm]: props.headerAffixedTop || isVirtual }])}
>
<table className={classNames(tableElmClasses)} style={{ ...tableElementStyles, width: `${tableElmWidth}px` }}>
Expand Down Expand Up @@ -269,7 +270,7 @@ const BaseTable = forwardRef((props: TBaseTableProps, ref) => {
>
<div
ref={affixFooterRef}
style={{ width: `${tableWidth}px`, opacity: Number(showAffixFooter) }}
style={{ width: `${tableWidth - affixedLeftBorder}px`, opacity: Number(showAffixFooter) }}
className={classNames([
'scrollbar',
{ [tableBaseClass.affixedFooterElm]: props.footerAffixedBottom || isVirtual },
Expand Down
1 change: 1 addition & 0 deletions src/table/PrimaryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const PrimaryTable = forwardRef((props: TPrimaryTableProps, ref) => {
validateRowData,
validateTableData,
clearValidateData,
...primaryTableRef.current,
}));

// 1. 影响列数量的因素有:自定义列配置、展开/收起行、多级表头;2. 影响表头内容的因素有:排序图标、筛选图标
Expand Down
24 changes: 12 additions & 12 deletions src/table/hooks/useFixed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState, useMemo, useRef, WheelEvent } from 'react';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import log from '../../_common/js/log';
import { ClassName, Styles } from '../../common';
import { BaseTableCol, TableRowData, TdBaseTableProps } from '../type';
Expand Down Expand Up @@ -295,11 +296,11 @@ export default function useFixed(props: TdBaseTableProps, finalColumns: BaseTabl
};

let shadowLastScrollLeft: number;
const updateColumnFixedShadow = (target: HTMLElement) => {
const updateColumnFixedShadow = (target: HTMLElement, extra?: { skipScrollLimit?: boolean }) => {
if (!isFixedColumn || !target) return;
const { scrollLeft } = target;
// 只有左右滚动,需要更新固定列阴影
if (shadowLastScrollLeft === scrollLeft) return;
if (shadowLastScrollLeft === scrollLeft && (!extra || !extra.skipScrollLimit)) return;
shadowLastScrollLeft = scrollLeft;
const isShowRight = target.clientWidth + scrollLeft < target.scrollWidth;
setShowColumnShadow({
Expand Down Expand Up @@ -465,15 +466,15 @@ export default function useFixed(props: TdBaseTableProps, finalColumns: BaseTabl
],
);

const refreshTable = () => {
const refreshTable = debounce(() => {
updateTableWidth();
updateFixedHeader();
updateThWidthListHandler();
if (isFixedColumn || isFixedHeader) {
updateFixedStatus();
updateColumnFixedShadow(tableContentRef.current);
updateColumnFixedShadow(tableContentRef.current, { skipScrollLimit: true });
}
};
}, 30);

const onResize = refreshTable;

Expand All @@ -491,18 +492,17 @@ export default function useFixed(props: TdBaseTableProps, finalColumns: BaseTabl
if (columnResizable && recalculateColWidth.current) {
recalculateColWidth.current(finalColumns, thWidthList, tableLayout, tableElmWidth);
}
const isWatchResize = isFixedColumn || isFixedHeader || !notNeedThWidthList || !data.length;
if (isWatchResize) {
on(window, 'resize', onResize);
}
clearTimeout(timer);
});
if (isFixedColumn || isFixedHeader || !notNeedThWidthList) {
on(window, 'resize', onResize);
}
return () => {
if (isFixedColumn || isFixedHeader || !notNeedThWidthList) {
off(window, 'resize', onResize);
}
off(window, 'resize', onResize);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [isFixedColumn]);

return {
tableWidth,
Expand Down