使用antd写了一个table,为了交互上友好;要求如下

scroll属性实现的,只需要把y的值设置为屏幕剩余高度即可
export const getResidueHeightByDom = () => {
const bodyHeight = document.body.offsetHeight; // 网页可见区域高 (包括边线的高)
const headerHeight = 64; // header高度
const breadcrumbHeight = 36 + 16 * 2; // 面包屑高度(包括间距)
const tabHeight = 46 + 16; // tab高度(包括间距)
const actionHeight = (document.getElementById('action') as HTMLElement).offsetHeight; // 操作区域高度
const actionMarginBottomHeight = 16; // 操作区域-底部外边距
const tableHeaderHeight = 55; // table-表头高度
const paginationHeight = 32 + 16 * 2; // 分页器高度(包括间距)
const contentBottomPadding = 24; // content区域的底部padding
const tabContentTopPadding = 24; // tab子元素区域上padding
const tabContentBottomPadding = 24; // tab子元素区域下padding
const residueHeight =
bodyHeight -
headerHeight -
breadcrumbHeight -
tabHeight -
actionHeight -
tableHeaderHeight -
actionMarginBottomHeight -
paginationHeight -
contentBottomPadding -
tabContentTopPadding -
tabContentBottomPadding;
window.console.log('Dom-residueHeight', residueHeight);
return residueHeight;
};

原理:视窗高度-table 距离顶部距离-底部元素高度-底部间距;
优点:变量较少(方案 A 的优化版);
缺点:只能在页面级别生效;
export const getResidueHeightByDOMRect = () => {
const bodyHeight = document.body.offsetHeight; // 网页可见区域高 (包括边线的高)
const tableBodyTop = document
.getElementsByClassName('ant-table-body')[0]
.getBoundingClientRect().top; // tableBody距离顶部距离
const paginationHeight = 32 + 16 * 2; // 分页器高度(包括间距);
const tabContentBottomPadding = 24; // tab子元素区域下padding
const contentBottomPadding = 24; // content区域的底部padding
const residueHeight =
bodyHeight - tableBodyTop - paginationHeight - contentBottomPadding - tabContentBottomPadding;
window.console.log('DOMRect-residueHeight', residueHeight);
return residueHeight;
};

原理:视窗高度-table 距离顶部距离-底部元素高度-底部间距;
优点:变量较少(方案 A 的优化版);
缺点:只能在页面级别生效;
export const getResidueHeightByRef = (ele: HTMLElement) => {
const bodyHeight = document.body.offsetHeight; // 网页可见区域高 (包括边线的高)
const tableHeaderHeight = 55; // table-表头高度
const tableBodyTop = ele.getBoundingClientRect().top; // tableBody距离顶部距离
const paginationHeight = 32 + 16 * 2; // 分页器高度(包括间距);
const tabContentBottomPadding = 24; // tab子元素区域下padding
const contentBottomPadding = 24; // content区域的底部padding
const residueHeight =
bodyHeight -
tableHeaderHeight -
tableBodyTop -
paginationHeight -
contentBottomPadding -
tabContentBottomPadding;
window.console.log('Ref-residueHeight', residueHeight);
return residueHeight;
};
layout 根据 Sider 的个数动态追加了".ant-layout-has-sider"类名,导致子组件挂载时获取不到正确的布局信息
解决方案: 不依赖组件的状态更新hasSider字段,给 组件 添加属性 hasSider
...
const [siders, setSiders] = React.useState<string[]>([]);
const { prefixCls, className, children, hasSider, tagName: Tag, ...others } = props;
const classString = classNames(
prefixCls,
{
[`${prefixCls}-has-sider`]: typeof hasSider === 'boolean' ? hasSider : siders.length > 0,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
...