-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathHeaderCell.tsx
149 lines (127 loc) · 4.76 KB
/
HeaderCell.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as React from 'react';
import cs from 'clsx';
/** @jsxRuntime classic */
/** @jsx jsx */
import { css, jsx } from '@emotion/react';
import { HeaderCellContainer } from '@table-library/react-table-library/common/components/Cell';
import { ThemeContext } from '@table-library/react-table-library/common/context/Theme';
import {
LayoutContext,
propagateResizedLayout,
setResizedLayout,
} from '@table-library/react-table-library/common/context';
import { resizerStyle } from '@table-library/react-table-library/resize/styles';
import { useResize } from '@table-library/react-table-library/resize/useResize';
import {
DataColumn,
toDataColumn,
getHeaderColumns,
} from '@table-library/react-table-library/common/util/columns';
import { HeaderCellProps } from '@table-library/react-table-library/types/table';
import { Nullish } from '@table-library/react-table-library/types/common';
const getPreservedColumn = (index: number, preservedDataColumns: DataColumn[]) => {
const findPreservedDataColumn = (dataColumn: DataColumn) => dataColumn.index === index;
const preservedDataColumn = preservedDataColumns.find(findPreservedDataColumn)!;
return preservedDataColumn;
};
const useUpdateLayout = (index: number, hide: boolean | Nullish) => {
const context = React.useContext(LayoutContext);
if (!context) {
throw new Error('No Layout Context.');
}
const { layout, tableElementRef, tableMemoryRef } = context;
React.useLayoutEffect(() => {
const preservedDataColumns = tableMemoryRef.current!.dataColumns;
const dataColumns = getHeaderColumns(tableElementRef).map(toDataColumn);
const thisPreservedDataColumn = getPreservedColumn(index, preservedDataColumns);
const hideStatusDidNotChange = thisPreservedDataColumn?.isHide === !!hide;
if (!preservedDataColumns?.length || hideStatusDidNotChange) return;
const visibleDataColumns = dataColumns.filter((dataColumn) => !dataColumn.isHide);
const getPartialResizedLayout = (dataColumn: DataColumn) => {
if (dataColumn.isStiff || layout?.horizontalScroll) {
const preservedDataColumn = getPreservedColumn(dataColumn.index, preservedDataColumns);
if (!preservedDataColumn) return 'minmax(0px, 1fr)';
return `${preservedDataColumn.width || preservedDataColumn.minWidth * 2}px`;
} else {
return 'minmax(0px, 1fr)';
}
};
const resizedLayout = visibleDataColumns.map(getPartialResizedLayout).join(' ');
setResizedLayout(resizedLayout, tableElementRef, tableMemoryRef);
propagateResizedLayout(resizedLayout, layout);
const newPreservedDataColumns = getHeaderColumns(tableElementRef).map(toDataColumn);
tableMemoryRef.current!.dataColumns = newPreservedDataColumns;
}, [index, hide, layout, tableElementRef, tableMemoryRef]);
};
export const HeaderCell: React.FC<HeaderCellProps> = ({
index,
className,
hide,
pinLeft,
pinRight,
stiff,
isFooter,
includePreviousColSpan,
previousColSpans,
gridColumnStart,
gridColumnEnd,
resize,
role = 'columnheader',
children,
style,
...rest
}: HeaderCellProps) => {
const theme = React.useContext(ThemeContext);
useUpdateLayout(index!, hide);
const { cellRef, resizeRef } = useResize(index!, hide);
const hasColSpan = gridColumnStart && gridColumnEnd;
const colSpan = hasColSpan ? gridColumnEnd - gridColumnStart - 1 : 0;
const computedGridColumnStart = includePreviousColSpan
? gridColumnStart + previousColSpans
: gridColumnStart;
const computedGridColumnEnd = includePreviousColSpan
? gridColumnEnd + previousColSpans
: gridColumnEnd;
return (
<>
<HeaderCellContainer
role={role}
data-table-library_th=""
data-hide={!!hide}
data-resize-min-width={
typeof resize === 'boolean' || resize?.minWidth == null ? 75 : resize.minWidth
}
style={{
...(hasColSpan
? { gridColumnStart: computedGridColumnStart, gridColumnEnd: computedGridColumnEnd }
: {}),
...style,
}}
css={css`
${theme?.BaseCell}
${isFooter ? theme?.FooterCell : theme?.HeaderCell}
`}
className={cs('th', className, {
stiff,
hide,
resize,
'pin-left': pinLeft,
'pin-right': pinRight,
})}
ref={cellRef}
{...rest}
>
<div>{children}</div>
{resize && !hide && (
<div className="resizer-area" ref={resizeRef} css={resizerStyle(resize).area}>
<span className="resizer-handle" css={resizerStyle(resize).handle} />
</div>
)}
</HeaderCellContainer>
{/* column grouping */}
{Array.from({ length: colSpan }, () => (
<HeaderCellContainer className={cs('th', 'hide', 'colspan')} />
))}
</>
);
};