Skip to content

Commit 0a49fc2

Browse files
authored
refactor: Header & Footer & Fixed Table with responsiveImmutable (#922)
* refactor: FixedHeader cache * refactor: all FixedHolder * refactor: Header * docs: update demo * refactor: Footer
1 parent 357e5f3 commit 0a49fc2

File tree

6 files changed

+55
-29
lines changed

6 files changed

+55
-29
lines changed

docs/examples/simple.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const Demo = () => {
2929
<>
3030
<button
3131
onClick={() => {
32-
setCount(count => count + 1);
32+
setCount(val => val + 1);
3333
}}
3434
>
3535
Click {count} times

src/FixedHolder/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useMemo } from 'react';
66
import ColGroup from '../ColGroup';
77
import TableContext from '../context/TableContext';
88
import type { HeaderProps } from '../Header/Header';
9-
import useRenderTimes from '../hooks/useRenderTimes';
9+
import devRenderTimes from '../hooks/useRenderTimes';
1010
import type { ColumnsType, ColumnType } from '../interface';
1111

1212
function useColumnWidth(colWidths: readonly number[], columCount: number) {
@@ -40,7 +40,9 @@ export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
4040
}
4141

4242
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>((props, ref) => {
43-
useRenderTimes(props);
43+
if (process.env.NODE_ENV !== 'production') {
44+
devRenderTimes(props);
45+
}
4446

4547
const {
4648
className,

src/Footer/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { responseImmutable, useContext } from '@rc-component/context';
12
import * as React from 'react';
23
import TableContext from '../context/TableContext';
3-
import { useContext } from '@rc-component/context';
4+
import devRenderTimes from '../hooks/useRenderTimes';
45
import type { ColumnType, StickyOffsets } from '../interface';
56
import Summary from './Summary';
67
import SummaryContext from './SummaryContext';
@@ -13,7 +14,13 @@ export interface FooterProps<RecordType> {
1314
flattenColumns: FlattenColumns<RecordType>;
1415
}
1516

16-
function Footer<RecordType>({ children, stickyOffsets, flattenColumns }: FooterProps<RecordType>) {
17+
function Footer<RecordType>(props: FooterProps<RecordType>) {
18+
if (process.env.NODE_ENV !== 'production') {
19+
devRenderTimes(props);
20+
}
21+
22+
const { children, stickyOffsets, flattenColumns } = props;
23+
1724
const prefixCls = useContext(TableContext, 'prefixCls');
1825

1926
const lastColumnIndex = flattenColumns.length - 1;
@@ -35,6 +42,6 @@ function Footer<RecordType>({ children, stickyOffsets, flattenColumns }: FooterP
3542
);
3643
}
3744

38-
export default Footer;
45+
export default responseImmutable(Footer);
3946

4047
export const FooterComponents = Summary;

src/Header/Header.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { responseImmutable, useContext } from '@rc-component/context';
12
import * as React from 'react';
23
import TableContext from '../context/TableContext';
3-
import { useContext } from '@rc-component/context';
4+
import devRenderTimes from '../hooks/useRenderTimes';
45
import type {
56
CellType,
67
ColumnGroupType,
@@ -89,16 +90,14 @@ export interface HeaderProps<RecordType> {
8990
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
9091
}
9192

92-
function Header<RecordType>({
93-
stickyOffsets,
94-
columns,
95-
flattenColumns,
96-
onHeaderRow,
97-
}: HeaderProps<RecordType>): React.ReactElement {
98-
const { prefixCls, getComponent } = useContext(TableContext, [
99-
'prefixCls',
100-
'getComponent',
101-
]);
93+
function Header<RecordType>(props: HeaderProps<RecordType>): React.ReactElement {
94+
if (process.env.NODE_ENV !== 'production') {
95+
devRenderTimes(props);
96+
}
97+
98+
const { stickyOffsets, columns, flattenColumns, onHeaderRow } = props;
99+
100+
const { prefixCls, getComponent } = useContext(TableContext, ['prefixCls', 'getComponent']);
102101
const rows: CellType<RecordType>[][] = React.useMemo(() => parseHeaderRows(columns), [columns]);
103102

104103
const WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
@@ -129,4 +128,4 @@ function Header<RecordType>({
129128
);
130129
}
131130

132-
export default Header;
131+
export default responseImmutable(Header);

src/Table.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Body from './Body';
4040
import ColGroup from './ColGroup';
4141
import { EXPAND_COLUMN } from './constant';
4242
import TableContext from './context/TableContext';
43-
import FixedHolder from './FixedHolder';
43+
import FixedHolder, { FixedHeaderProps } from './FixedHolder';
4444
import Footer, { FooterComponents } from './Footer';
4545
import type { SummaryProps } from './Footer/Summary';
4646
import Summary from './Footer/Summary';
@@ -322,7 +322,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
322322
useSticky(sticky, prefixCls);
323323

324324
// Footer (Fix footer must fixed header)
325-
const summaryNode = summary?.(mergedData);
325+
const summaryNode = React.useMemo(() => summary?.(mergedData), [summary, mergedData]);
326326
const fixFooter =
327327
(fixHeader || isSticky) &&
328328
React.isValidElement(summaryNode) &&
@@ -473,7 +473,26 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
473473
}
474474
});
475475

476-
// ====================== Render ======================
476+
// ========================================================================
477+
// == Render ==
478+
// ========================================================================
479+
// =================== Render: Func ===================
480+
const renderFixedHeaderTable = React.useCallback<FixedHeaderProps<RecordType>['children']>(
481+
fixedHolderPassProps => (
482+
<>
483+
<Header {...fixedHolderPassProps} />
484+
{fixFooter === 'top' && <Footer {...fixedHolderPassProps}>{summaryNode}</Footer>}
485+
</>
486+
),
487+
[fixFooter, summaryNode],
488+
);
489+
490+
const renderFixedFooterTable = React.useCallback<FixedHeaderProps<RecordType>['children']>(
491+
fixedHolderPassProps => <Footer {...fixedHolderPassProps}>{summaryNode}</Footer>,
492+
[summaryNode],
493+
);
494+
495+
// =================== Render: Node ===================
477496
const TableComponent = getComponent(['table'], 'table');
478497

479498
// Table layout
@@ -629,12 +648,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
629648
className={`${prefixCls}-header`}
630649
ref={scrollHeaderRef}
631650
>
632-
{fixedHolderPassProps => (
633-
<>
634-
<Header {...fixedHolderPassProps} />
635-
{fixFooter === 'top' && <Footer {...fixedHolderPassProps}>{summaryNode}</Footer>}
636-
</>
637-
)}
651+
{renderFixedHeaderTable}
638652
</FixedHolder>
639653
)}
640654

@@ -649,7 +663,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
649663
className={`${prefixCls}-summary`}
650664
ref={scrollSummaryRef}
651665
>
652-
{fixedHolderPassProps => <Footer {...fixedHolderPassProps}>{summaryNode}</Footer>}
666+
{renderFixedFooterTable}
653667
</FixedHolder>
654668
)}
655669

src/hooks/useRenderTimes.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* istanbul ignore file */
22
import * as React from 'react';
33

4-
function useRenderTimes<T extends object>(props?: T) {
4+
function useRenderTimes<T extends object>(props?: T, debug?: string) {
55
// Render times
66
const timesRef = React.useRef(0);
77
timesRef.current += 1;
@@ -25,6 +25,10 @@ function useRenderTimes<T extends object>(props?: T) {
2525
React.useDebugValue(timesRef.current);
2626
React.useDebugValue(keysRef.current.join(', '));
2727

28+
if (debug) {
29+
console.log(`${debug}:`, timesRef.current, keysRef.current);
30+
}
31+
2832
return timesRef.current;
2933
}
3034

0 commit comments

Comments
 (0)