Skip to content

Commit 9d0fbdd

Browse files
authored
refactor: use shared value guards (#1511)
1 parent 7977f2b commit 9d0fbdd

4 files changed

Lines changed: 13 additions & 17 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"dependencies": {
5353
"@rc-component/context": "^2.0.1",
5454
"@rc-component/resize-observer": "^1.0.0",
55-
"@rc-component/util": "^1.11.1",
55+
"@rc-component/util": "^1.13.0",
5656
"@rc-component/virtual-list": "^1.0.1",
5757
"clsx": "^2.1.1"
5858
},

‎src/Cell/useCellRender.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { get as getValue, isEqual, useMemo, warning } from '@rc-component/util';
1+
import { get as getValue, isEqual, isNonNullable, useMemo, warning } from '@rc-component/util';
22
import * as React from 'react';
33
import PerfContext from '../context/PerfContext';
44
import type { CellType, ColumnType, DataIndex, RenderedCell } from '../interface';
5-
import { validateValue } from '../utils/valueUtil';
65
import { useImmutableMark } from '../context/TableContext';
76

87
function isRenderCell<RecordType>(
@@ -26,7 +25,7 @@ export default function useCellRender<RecordType>(
2625
// ======================== Render ========================
2726
const retData = useMemo<[React.ReactNode, CellType<RecordType>] | [React.ReactNode]>(
2827
() => {
29-
if (validateValue(children)) {
28+
if (isNonNullable(children)) {
3029
return [children];
3130
}
3231

‎src/Table.tsx‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import {
3131
get as getValue,
3232
getTargetScrollBarSize,
3333
isEqual,
34+
isNonNullable,
35+
isReactRenderable,
3436
pickAttrs,
3537
useEvent,
3638
useLayoutEffect,
@@ -76,7 +78,7 @@ import Panel from './Panel';
7678
import StickyScrollBar from './stickyScrollBar';
7779
import Column from './sugar/Column';
7880
import ColumnGroup from './sugar/ColumnGroup';
79-
import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil';
81+
import { getColumnsKey, validNumberValue } from './utils/valueUtil';
8082

8183
export type CompareProps<T extends React.ComponentType<any>> = (
8284
prevProps: Readonly<React.ComponentProps<T>>,
@@ -397,8 +399,8 @@ const Table = <RecordType extends DefaultRecordType>(
397399
const pureColWidths = colsKeys.map(columnKey => colsWidths.get(columnKey));
398400
const colWidths = React.useMemo(() => pureColWidths, [pureColWidths.join('_')]);
399401
const stickyOffsets = useStickyOffsets(colWidths, flattenColumns);
400-
const fixHeader = scroll && validateValue(scroll.y);
401-
const horizonScroll = (scroll && validateValue(mergedScrollX)) || Boolean(expandableConfig.fixed);
402+
const fixHeader = scroll && isNonNullable(scroll.y);
403+
const horizonScroll = (scroll && isNonNullable(mergedScrollX)) || Boolean(expandableConfig.fixed);
402404
const fixColumn = horizonScroll && flattenColumns.some(({ fixed }) => fixed);
403405

404406
// Sticky
@@ -675,10 +677,9 @@ const Table = <RecordType extends DefaultRecordType>(
675677
<ColGroup colWidths={flattenColumns.map(({ width }) => width)} columns={flattenColumns} />
676678
);
677679

678-
const captionElement =
679-
caption !== null && caption !== undefined ? (
680-
<caption className={`${prefixCls}-caption`}>{caption}</caption>
681-
) : undefined;
680+
const captionElement = isNonNullable(caption) ? (
681+
<caption className={`${prefixCls}-caption`}>{caption}</caption>
682+
) : undefined;
682683

683684
const dataProps = pickAttrs(props, { data: true });
684685
const ariaProps = pickAttrs(props, { aria: true });
@@ -730,7 +731,7 @@ const Table = <RecordType extends DefaultRecordType>(
730731
{captionElement}
731732
{bodyColGroup}
732733
{bodyTable}
733-
{!fixFooter && summaryNode && (
734+
{!fixFooter && isReactRenderable(summaryNode) && (
734735
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns}>
735736
{summaryNode}
736737
</Footer>
@@ -813,7 +814,7 @@ const Table = <RecordType extends DefaultRecordType>(
813814
{bodyColGroup}
814815
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
815816
{bodyTable}
816-
{summaryNode && (
817+
{isReactRenderable(summaryNode) && (
817818
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns}>
818819
{summaryNode}
819820
</Footer>

‎src/utils/valueUtil.tsx‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ export function getColumnsKey<T = any>(columns: readonly GetColumnKeyColumn<T>[]
3333
return columnKeys;
3434
}
3535

36-
export function validateValue<T>(val: T) {
37-
return val !== null && val !== undefined;
38-
}
39-
4036
export function validNumberValue(value: any) {
4137
return typeof value === 'number' && !Number.isNaN(value);
4238
}

0 commit comments

Comments
 (0)