Skip to content

Commit 3dca17f

Browse files
committed
fix(ColSpan): includePrevious
1 parent 2e6ea66 commit 3dca17f

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
3+
export const getPreviousColSpans = (children: React.ReactNode, index: number) => {
4+
return React.Children.toArray(children).reduce((acc, value, key) => {
5+
if (!React.isValidElement(value)) return acc;
6+
if (key >= index) return acc;
7+
if (!value.props.gridColumnStart && !value.props.gridColumnEnd) return acc;
8+
9+
return acc + value.props.gridColumnEnd - value.props.gridColumnStart - 1;
10+
}, 0);
11+
};

src/table/Cell/Cell.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export const Cell: React.FC<CellProps> = ({
1616
pinLeft,
1717
pinRight,
1818
stiff,
19+
includePreviousColSpan,
20+
previousColSpans,
1921
gridColumnStart,
2022
gridColumnEnd,
2123
onClick,
@@ -27,13 +29,24 @@ export const Cell: React.FC<CellProps> = ({
2729

2830
const hasColSpan = gridColumnStart && gridColumnEnd;
2931
const colSpan = hasColSpan ? gridColumnEnd - gridColumnStart - 1 : 0;
32+
const computedGridColumnStart = includePreviousColSpan
33+
? gridColumnStart + previousColSpans
34+
: gridColumnStart;
35+
const computedGridColumnEnd = includePreviousColSpan
36+
? gridColumnEnd + previousColSpans
37+
: gridColumnEnd;
3038

3139
return (
3240
<>
3341
<CellContainer
3442
role="gridcell"
3543
data-table-library_td=""
36-
style={{ ...(hasColSpan ? { gridColumnStart, gridColumnEnd } : {}), ...style }}
44+
style={{
45+
...(hasColSpan
46+
? { gridColumnStart: computedGridColumnStart, gridColumnEnd: computedGridColumnEnd }
47+
: {}),
48+
...style,
49+
}}
3750
css={css`
3851
${theme?.BaseCell}
3952
${theme?.Cell}

src/table/Cell/HeaderCell.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export const HeaderCell: React.FC<HeaderCellProps> = ({
7676
pinRight,
7777
stiff,
7878
isFooter,
79+
includePreviousColSpan,
80+
previousColSpans,
7981
gridColumnStart,
8082
gridColumnEnd,
8183
resize,
@@ -92,6 +94,12 @@ export const HeaderCell: React.FC<HeaderCellProps> = ({
9294

9395
const hasColSpan = gridColumnStart && gridColumnEnd;
9496
const colSpan = hasColSpan ? gridColumnEnd - gridColumnStart - 1 : 0;
97+
const computedGridColumnStart = includePreviousColSpan
98+
? gridColumnStart + previousColSpans
99+
: gridColumnStart;
100+
const computedGridColumnEnd = includePreviousColSpan
101+
? gridColumnEnd + previousColSpans
102+
: gridColumnEnd;
95103

96104
return (
97105
<>
@@ -102,7 +110,12 @@ export const HeaderCell: React.FC<HeaderCellProps> = ({
102110
data-resize-min-width={
103111
typeof resize === 'boolean' || resize?.minWidth == null ? 75 : resize.minWidth
104112
}
105-
style={{ ...(hasColSpan ? { gridColumnStart, gridColumnEnd } : {}), ...style }}
113+
style={{
114+
...(hasColSpan
115+
? { gridColumnStart: computedGridColumnStart, gridColumnEnd: computedGridColumnEnd }
116+
: {}),
117+
...style,
118+
}}
106119
css={css`
107120
${theme?.BaseCell}
108121
${isFooter ? theme?.FooterCell : theme?.HeaderCell}

src/table/Row/HeaderRow.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
getHeaderColumns,
1717
} from '@table-library/react-table-library/common/util/columns';
1818
import { isReactFragment } from '@table-library/react-table-library/common/util/isFragment';
19+
import { getPreviousColSpans } from '@table-library/react-table-library/common/util/getPreviousColSpans';
1920

2021
import { HeaderRowProps } from '@table-library/react-table-library/types/table';
2122

@@ -96,6 +97,7 @@ export const HeaderRow: React.FC<HeaderRowProps> = ({
9697
extraProps = {
9798
...extraProps,
9899
index,
100+
previousColSpans: getPreviousColSpans(children, index),
99101
};
100102
}
101103

src/table/Row/Row.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { RowContainer } from '@table-library/react-table-library/common/componen
1010
import { ThemeContext } from '@table-library/react-table-library/common/context/Theme';
1111
import { useFeatures } from '@table-library/react-table-library/common/context/Feature';
1212
import { isReactFragment } from '@table-library/react-table-library/common/util/isFragment';
13+
import { getPreviousColSpans } from '@table-library/react-table-library/common/util/getPreviousColSpans';
1314

1415
import { Nullish } from '@table-library/react-table-library/types/common';
1516
import {
@@ -126,6 +127,7 @@ export const Row: React.FC<RowProps> = (props: RowProps) => {
126127
extraProps = {
127128
...extraProps,
128129
index,
130+
previousColSpans: getPreviousColSpans(children, index),
129131
};
130132
}
131133

0 commit comments

Comments
 (0)