153 lines
5.1 KiB
React
153 lines
5.1 KiB
React
import React, {
|
|
forwardRef,
|
|
useEffect,
|
|
useLayoutEffect,
|
|
useRef,
|
|
useState,
|
|
} from 'react';
|
|
import { Table, theme } from 'antd';
|
|
import { colord } from 'colord';
|
|
import { isCenterTableScope, prepareCenterColumns } from './centerTablePolicy';
|
|
import './center-table.css';
|
|
|
|
const CenterTable = forwardRef(function CenterTable(
|
|
{ columns, className, scroll, style, minScrollableWidth, ...props },
|
|
ref,
|
|
) {
|
|
const { token } = theme.useToken();
|
|
const base = colord(token.colorBgContainer).toRgb();
|
|
const solidColor = (color) => {
|
|
const value = colord(color).toRgb();
|
|
return colord({
|
|
r: value.r * value.a + base.r * (1 - value.a),
|
|
g: value.g * value.a + base.g * (1 - value.a),
|
|
b: value.b * value.a + base.b * (1 - value.a),
|
|
a: 1,
|
|
}).toRgbString();
|
|
};
|
|
const tableRef = useRef(null);
|
|
const [width, setWidth] = useState(0);
|
|
const [measuredActionWidths, setMeasuredActionWidths] = useState({});
|
|
const scoped = isCenterTableScope(
|
|
typeof window === 'undefined' ? '' : window.location.pathname,
|
|
);
|
|
const prepared =
|
|
scoped && columns
|
|
? prepareCenterColumns(
|
|
columns,
|
|
width,
|
|
measuredActionWidths,
|
|
minScrollableWidth,
|
|
)
|
|
: columns;
|
|
const enabled = prepared !== columns;
|
|
useLayoutEffect(() => {
|
|
if (!enabled) return;
|
|
const root = tableRef.current?.nativeElement;
|
|
if (!root) return;
|
|
const scroller = root.querySelector('.ant-table-content,.ant-table-body');
|
|
const previousTabIndex = scroller?.getAttribute('tabindex');
|
|
if (scroller && previousTabIndex === null) scroller.tabIndex = 0;
|
|
const observer = new ResizeObserver(([entry]) => {
|
|
// ResizeObserver already measured the box; avoid a synchronous layout read.
|
|
setWidth(entry.contentRect.width);
|
|
});
|
|
observer.observe(root);
|
|
return () => {
|
|
observer.disconnect();
|
|
if (scroller && previousTabIndex === null)
|
|
scroller.removeAttribute('tabindex');
|
|
};
|
|
}, [enabled]);
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
const root = tableRef.current?.nativeElement;
|
|
if (!root) return;
|
|
const measureActionWidths = () => {
|
|
const next = {};
|
|
root.querySelectorAll('th.center-action-cell').forEach((header) => {
|
|
const marker = [...header.classList].find((name) =>
|
|
name.startsWith('center-fixed-index-'),
|
|
);
|
|
if (!marker) return;
|
|
const cells = [...root.querySelectorAll(`td.${marker}`)].filter(
|
|
(element) => element.getBoundingClientRect().width > 0,
|
|
);
|
|
const widths = cells.map((cell) => {
|
|
const controlsWithWidths = [
|
|
...cell.querySelectorAll('button, a, [role="button"]'),
|
|
]
|
|
.map((element) => ({
|
|
element,
|
|
width: element.getBoundingClientRect().width,
|
|
}))
|
|
.filter(({ width }) => width > 0);
|
|
const controls = controlsWithWidths.map(({ element }) => element);
|
|
if (!controls.length) return 0;
|
|
let group = controls[0].parentElement;
|
|
while (
|
|
group !== cell &&
|
|
!controls.every((control) => group.contains(control))
|
|
)
|
|
group = group.parentElement;
|
|
const cellStyle = getComputedStyle(cell);
|
|
const padding =
|
|
(Number.parseFloat(cellStyle.paddingLeft) || 0) +
|
|
(Number.parseFloat(cellStyle.paddingRight) || 0);
|
|
const gap = Number.parseFloat(getComputedStyle(group).columnGap) || 8;
|
|
return (
|
|
controlsWithWidths.reduce((total, { width }) => total + width, 0) +
|
|
gap * Math.max(0, controls.length - 1) +
|
|
padding
|
|
);
|
|
});
|
|
const measured = Math.max(...widths, 0);
|
|
if (measured > 0)
|
|
next[marker.replace('center-fixed-index-', '')] = measured;
|
|
});
|
|
setMeasuredActionWidths((current) => {
|
|
const same =
|
|
Object.keys(next).length === Object.keys(current).length &&
|
|
Object.keys(next).every(
|
|
(key) => Math.abs(next[key] - current[key]) < 1,
|
|
);
|
|
return same ? current : next;
|
|
});
|
|
};
|
|
const frame = requestAnimationFrame(measureActionWidths);
|
|
return () => {
|
|
cancelAnimationFrame(frame);
|
|
};
|
|
}, [enabled, width, columns, props.dataSource]);
|
|
return (
|
|
<Table
|
|
{...props}
|
|
ref={(instance) => {
|
|
tableRef.current = instance;
|
|
if (typeof ref === 'function') ref(instance);
|
|
else if (ref) ref.current = instance;
|
|
}}
|
|
columns={prepared}
|
|
className={[className, enabled && 'center-action-table']
|
|
.filter(Boolean)
|
|
.join(' ')}
|
|
scroll={enabled ? { ...scroll, x: scroll?.x || 'max-content' } : scroll}
|
|
style={
|
|
enabled
|
|
? {
|
|
'--center-table-bg': token.colorBgContainer,
|
|
'--center-table-head-bg': solidColor(token.colorFillAlter),
|
|
'--center-table-hover-bg': solidColor(token.controlItemBgHover),
|
|
'--center-table-selected-bg': solidColor(
|
|
token.controlItemBgActive,
|
|
),
|
|
...style,
|
|
}
|
|
: style
|
|
}
|
|
/>
|
|
);
|
|
});
|
|
|
|
export default CenterTable;
|