Skip to content

refactor: Support all type of shadow #1238

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

Merged
merged 20 commits into from
Mar 7, 2025
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
84 changes: 22 additions & 62 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -56,76 +56,37 @@
}

// ================== Cell ==================
&-fixed-column-gapped {
.@{tablePrefixCls}-cell-fix-left-last::after,
.@{tablePrefixCls}-cell-fix-right-first::after {
display: none !important;
}
}

&-cell {
background: #f4f4f4;

&-fix-left,
&-fix-right {
z-index: 2;
&-fix {
position: sticky !important;
z-index: calc(2 + var(--z-offset));
}

&-fix-right:last-child:not(&-fix-sticky) {
border-right-color: transparent;
}

.@{tablePrefixCls}-rtl & {
&-fix-right:last-child {
border-right-color: @border-color;
}
&-fix-left:last-child {
border-left-color: transparent;
&-fix-start-shadow,
&-fix-end-shadow {
&::after {
position: absolute;
top: 0;
bottom: 0;
width: 5px;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
content: '';
transition: opacity 0.3s;
}
}

&-fix-left-first {
.@{tablePrefixCls}-rtl & {
box-shadow: 1px 0 0 @border-color;
&-show::after {
opacity: 1;
}
}

&-fix-left-first::after,
&-fix-left-last::after {
position: absolute;
top: 0;
right: -1px;
bottom: -1px;
width: 20px;
transform: translateX(100%);
transition: box-shadow 0.3s;
content: '';
pointer-events: none;
}

&-fix-left-all::after {
display: none;
&-fix-start-shadow::after {
inset-inline-start: 100%;
}

&-fix-right-first,
&-fix-right-last {
box-shadow: -1px 0 0 @border-color;

.@{tablePrefixCls}-rtl & {
box-shadow: none;
}

&::after {
position: absolute;
top: 0;
bottom: -1px;
left: -1px;
width: 20px;
transform: translateX(-100%);
transition: box-shadow 0.3s;
content: '';
pointer-events: none;
}
&-fix-end-shadow::after {
inset-inline-end: 100%;
}

&&-ellipsis {
Expand All @@ -134,9 +95,8 @@
text-overflow: ellipsis;

// Fixed first or last should special process
&.@{tablePrefixCls}-cell-fix-left-first,
&.@{tablePrefixCls}-cell-fix-left-last,
&.@{tablePrefixCls}-cell-fix-right-first &.@{tablePrefixCls}-cell-fix-right-last {
&.@{tablePrefixCls}-cell-fix-start-shadow,
&.@{tablePrefixCls}-cell-fix-end-shadow {
overflow: visible;

.@{tablePrefixCls}-cell-content {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/fixedColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const columns: ColumnType<RecordType>[] = [
{ title: 'title8', dataIndex: 'b', key: 'h' },
{ title: 'title9', dataIndex: 'b', key: 'i' },
{ title: 'title10', dataIndex: 'b', key: 'j' },
{ title: 'title11', dataIndex: 'b', key: 'k', width: 50 },
{ title: 'title11', dataIndex: 'b', key: 'k', width: 50, fixed: 'right' },
{ title: 'title12', dataIndex: 'b', key: 'l', width: 100, fixed: 'right' },
];

Expand Down
27 changes: 22 additions & 5 deletions docs/examples/scrollXY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import Table from 'rc-table';
import '../../assets/index.less';

const columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'start' },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100, fixed: 'start' },
{ title: 'title4', dataIndex: 'b', key: 'd', width: 100 },
{ title: 'title5', dataIndex: 'b', key: 'e', width: 100 },
{ title: 'title6', dataIndex: 'b', key: 'f', width: 100 },
{ title: 'title7', dataIndex: 'b', key: 'g', width: 100 },
{ title: 'title8', dataIndex: 'b', key: 'h', width: 100 },
{ title: 'title9', dataIndex: 'b', key: 'i', width: 100 },
{ title: 'title10', dataIndex: 'b', key: 'j', width: 100 },
{ title: 'title10', dataIndex: 'b', key: 'j', width: 100, fixed: 'end' },
{ title: 'title11', dataIndex: 'b', key: 'k', width: 100 },
{ title: 'title12', dataIndex: 'b', key: 'l', width: 100 },
{ title: 'title12', dataIndex: 'b', key: 'l', width: 100, fixed: 'end' },
];

const data = [
Expand All @@ -32,6 +32,8 @@ const data = [

const Demo = () => {
const [visible, setVisible] = React.useState(true);
const [rtl, setRtl] = React.useState(true);

return (
<div>
<h2>Scroll X/Y</h2>
Expand All @@ -43,8 +45,23 @@ const Demo = () => {
>
Trigger Visible
</button>
<button
type="button"
onClick={() => {
setRtl(!rtl);
}}
>
{rtl ? 'RTL' : 'LTR'}
</button>

<div style={{ display: visible ? undefined : 'none' }}>
<Table style={{ width: 800 }} scroll={{ x: 1500, y: 300 }} columns={columns} data={data} />
<Table
direction={rtl ? 'rtl' : 'ltr'}
style={{ width: 800 }}
scroll={{ x: 1500, y: 300 }}
columns={columns}
data={data}
/>
</div>
</div>
);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@types/styled-components": "^5.1.32",
"@umijs/fabric": "^4.0.1",
"@vitest/coverage-v8": "^2.0.5",
"cheerio": "1.0.0-rc.12",
"cross-env": "^7.0.0",
"dumi": "^2.1.3",
"eslint": "^8.54.0",
Expand Down
78 changes: 47 additions & 31 deletions src/Cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ export interface CellProps<RecordType extends DefaultRecordType> {
shouldCellUpdate?: (record: RecordType, prevRecord: RecordType) => boolean;

// Fixed
fixLeft?: number | false;
fixRight?: number | false;
firstFixLeft?: boolean;
lastFixLeft?: boolean;
firstFixRight?: boolean;
lastFixRight?: boolean;
fixStart?: number | false;
fixEnd?: number | false;
fixedStartShadow?: boolean;
fixedEndShadow?: boolean;
zIndex?: number;
allColsFixedLeft?: boolean;

// ====================== Private Props ======================
Expand Down Expand Up @@ -104,12 +103,11 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
rowSpan,

// Fixed
fixLeft,
fixRight,
firstFixLeft,
lastFixLeft,
firstFixRight,
lastFixRight,
fixStart,
fixEnd,
fixedStartShadow,
fixedEndShadow,
zIndex,

// Private
appendNode,
Expand All @@ -118,8 +116,7 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
} = props;

const cellPrefixCls = `${prefixCls}-cell`;
const { supportSticky, allColumnsFixedLeft, rowHoverable } = useContext(TableContext, [
'supportSticky',
const { allColumnsFixedLeft, rowHoverable } = useContext(TableContext, [
'allColumnsFixedLeft',
'rowHoverable',
]);
Expand All @@ -136,16 +133,31 @@ function Cell<RecordType>(props: CellProps<RecordType>) {

// ====================== Fixed =======================
const fixedStyle: React.CSSProperties = {};
const isFixLeft = typeof fixLeft === 'number' && supportSticky;
const isFixRight = typeof fixRight === 'number' && supportSticky;
const isFixStart = typeof fixStart === 'number' && !allColumnsFixedLeft;
const isFixEnd = typeof fixEnd === 'number' && !allColumnsFixedLeft;

if (isFixLeft) {
fixedStyle.position = 'sticky';
fixedStyle.left = fixLeft as number;
const [showFixStartShadow, showFixEndShadow] = useContext(TableContext, ({ scrollInfo }) => {
if (!isFixStart && !isFixEnd) {
return [false, false];
}

const [scroll, scrollWidth] = scrollInfo;

const absScroll = Math.abs(scroll);

const showStartShadow = isFixStart && fixedStartShadow && absScroll > fixStart;
const showEndShadow = isFixEnd && fixedEndShadow && scrollWidth - absScroll > fixEnd;

return [showStartShadow, showEndShadow];
});

if (isFixStart) {
fixedStyle.insetInlineStart = fixStart as number;
fixedStyle['--z-offset'] = zIndex;
}
if (isFixRight) {
fixedStyle.position = 'sticky';
fixedStyle.right = fixRight as number;
if (isFixEnd) {
fixedStyle.insetInlineEnd = fixEnd as number;
fixedStyle['--z-offset'] = zIndex;
}

// ================ RowSpan & ColSpan =================
Expand Down Expand Up @@ -190,16 +202,20 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
cellPrefixCls,
className,
{
[`${cellPrefixCls}-fix-left`]: isFixLeft && supportSticky,
[`${cellPrefixCls}-fix-left-first`]: firstFixLeft && supportSticky,
[`${cellPrefixCls}-fix-left-last`]: lastFixLeft && supportSticky,
[`${cellPrefixCls}-fix-left-all`]: lastFixLeft && allColumnsFixedLeft && supportSticky,
[`${cellPrefixCls}-fix-right`]: isFixRight && supportSticky,
[`${cellPrefixCls}-fix-right-first`]: firstFixRight && supportSticky,
[`${cellPrefixCls}-fix-right-last`]: lastFixRight && supportSticky,
// Fixed
[`${cellPrefixCls}-fix`]: isFixStart || isFixEnd,
[`${cellPrefixCls}-fix-start`]: isFixStart,
[`${cellPrefixCls}-fix-end`]: isFixEnd,

// Fixed shadow
[`${cellPrefixCls}-fix-start-shadow`]: fixedStartShadow,
[`${cellPrefixCls}-fix-start-shadow-show`]: fixedStartShadow && showFixStartShadow,
[`${cellPrefixCls}-fix-end-shadow`]: fixedEndShadow,
[`${cellPrefixCls}-fix-end-shadow-show`]: fixedEndShadow && showFixEndShadow,

[`${cellPrefixCls}-ellipsis`]: ellipsis,
[`${cellPrefixCls}-with-append`]: appendNode,
[`${cellPrefixCls}-fix-sticky`]: (isFixLeft || isFixRight) && isSticky && supportSticky,
[`${cellPrefixCls}-fix-sticky`]: (isFixStart || isFixEnd) && isSticky,
[`${cellPrefixCls}-row-hover`]: !legacyCellProps && hovering,
},
additionalProps.className,
Expand Down Expand Up @@ -233,7 +249,7 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
mergedChildNode = null;
}

if (ellipsis && (lastFixLeft || firstFixRight)) {
if (ellipsis && (fixedStartShadow || fixedEndShadow)) {
mergedChildNode = <span className={`${cellPrefixCls}-content`}>{mergedChildNode}</span>;
}

Expand Down
29 changes: 23 additions & 6 deletions src/FixedHolder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,22 @@
function onWheel(e: WheelEvent) {
const { currentTarget, deltaX } = e as unknown as React.WheelEvent<HTMLDivElement>;
if (deltaX) {
onScroll({ currentTarget, scrollLeft: currentTarget.scrollLeft + deltaX });
const { scrollLeft, scrollWidth, clientWidth } = currentTarget;
const maxScrollWidth = scrollWidth - clientWidth;
let nextScroll = scrollLeft + deltaX;

if (direction === 'rtl') {
nextScroll = Math.max(-maxScrollWidth, nextScroll);
nextScroll = Math.min(0, nextScroll);

Check warning on line 94 in src/FixedHolder/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/FixedHolder/index.tsx#L93-L94

Added lines #L93 - L94 were not covered by tests
} else {
nextScroll = Math.min(maxScrollWidth, nextScroll);
nextScroll = Math.max(0, nextScroll);
}

onScroll({
currentTarget,
scrollLeft: nextScroll,
});
e.preventDefault();
}
}
Expand Down Expand Up @@ -124,13 +139,15 @@

// Calculate the sticky offsets
const headerStickyOffsets = useMemo(() => {
const { right, left } = stickyOffsets;
const { start, end } = stickyOffsets;
return {
...stickyOffsets,
left:
direction === 'rtl' ? [...left.map(width => width + combinationScrollBarSize), 0] : left,
right:
direction === 'rtl' ? right : [...right.map(width => width + combinationScrollBarSize), 0],
// left:
// direction === 'rtl' ? [...left.map(width => width + combinationScrollBarSize), 0] : left,
// right:
// direction === 'rtl' ? right : [...right.map(width => width + combinationScrollBarSize), 0],
start: start,
end: [...end.map(width => width + combinationScrollBarSize), 0],
isSticky,
};
}, [combinationScrollBarSize, stickyOffsets, isSticky]);
Expand Down
11 changes: 4 additions & 7 deletions src/Footer/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ export default function SummaryCell({
rowSpan,
align,
}: SummaryCellProps) {
const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']);
const { prefixCls } = useContext(TableContext, ['prefixCls']);
const { scrollColumnIndex, stickyOffsets, flattenColumns } = React.useContext(SummaryContext);
const lastIndex = index + colSpan - 1;
const mergedColSpan = lastIndex + 1 === scrollColumnIndex ? colSpan + 1 : colSpan;

const fixedInfo = getCellFixedInfo(
index,
index + mergedColSpan - 1,
flattenColumns,
stickyOffsets,
direction,
const fixedInfo = React.useMemo(
() => getCellFixedInfo(index, index + mergedColSpan - 1, flattenColumns, stickyOffsets),
[index, mergedColSpan, flattenColumns, stickyOffsets],
);

return (
Expand Down
3 changes: 1 addition & 2 deletions src/Header/HeaderRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
onHeaderRow,
index,
} = props;
const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']);
const { prefixCls } = useContext(TableContext, ['prefixCls']);
let rowProps: React.HTMLAttributes<HTMLElement>;
if (onHeaderRow) {
rowProps = onHeaderRow(
Expand All @@ -52,7 +52,6 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
cell.colEnd,
flattenColumns,
stickyOffsets,
direction,
);

let additionalProps: React.HTMLAttributes<HTMLElement>;
Expand Down
Loading