From fa383b4d4bfa89c3858efee4adc1fde30deeae0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 28 Dec 2022 11:02:36 +0800 Subject: [PATCH 1/4] chore: init --- src/Table.tsx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Table.tsx b/src/Table.tsx index 3226b7bd3..7d5d55f3a 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -24,15 +24,16 @@ * - All expanded props, move into expandable */ +import { makeImmutable } from '@rc-component/context'; import classNames from 'classnames'; import ResizeObserver from 'rc-resize-observer'; import isVisible from 'rc-util/lib/Dom/isVisible'; import { isStyleSupport } from 'rc-util/lib/Dom/styleChecker'; import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize'; +import isEqual from 'rc-util/lib/isEqual'; import pickAttrs from 'rc-util/lib/pickAttrs'; import warning from 'rc-util/lib/warning'; import * as React from 'react'; -import isEqual from 'rc-util/lib/isEqual'; import Body from './Body'; import ColGroup from './ColGroup'; import { EXPAND_COLUMN } from './constant'; @@ -893,12 +894,20 @@ function Table(tableProps: TableProps Date: Wed, 28 Dec 2022 11:19:40 +0800 Subject: [PATCH 2/4] chore: make default stable --- src/Table.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Table.tsx b/src/Table.tsx index 7d5d55f3a..da29bdb22 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -172,11 +172,15 @@ export interface TableProps sticky?: boolean | TableSticky; } +function defaultEmpty() { + return () => 'No Data'; +} + function Table(tableProps: TableProps) { const props = { rowKey: 'key', prefixCls: 'rc-table', - emptyText: () => 'No Data', + emptyText: defaultEmpty, ...tableProps, }; From 94ec5792fe603eadad1a484ec7a2e7a1b3927717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 28 Dec 2022 11:43:54 +0800 Subject: [PATCH 3/4] chore: fix refactor --- src/Cell/index.tsx | 1 + src/Table.tsx | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Cell/index.tsx b/src/Cell/index.tsx index 487daba87..0d29783f0 100644 --- a/src/Cell/index.tsx +++ b/src/Cell/index.tsx @@ -1,6 +1,7 @@ import classNames from 'classnames'; import { supportRef } from 'rc-util/lib/ref'; import warning from 'rc-util/lib/warning'; +import getValue from 'rc-util/lib/utils/get'; import * as React from 'react'; import isEqual from 'rc-util/lib/isEqual'; import BodyContext from '../context/BodyContext'; diff --git a/src/Table.tsx b/src/Table.tsx index da29bdb22..98af975f0 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -173,7 +173,7 @@ export interface TableProps } function defaultEmpty() { - return () => 'No Data'; + return 'No Data'; } function Table(tableProps: TableProps) { From aa9c94b632e3934e2d5aeab47eb38dd4fad211d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 28 Dec 2022 12:32:32 +0800 Subject: [PATCH 4/4] refactor: use rc-util instead of internal get --- package.json | 2 +- src/Cell/index.tsx | 20 ++++++++++++-------- src/Table.tsx | 8 +++----- src/utils/valueUtil.tsx | 27 +-------------------------- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 15975d153..189135d2f 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "@rc-component/context": "^1.0.0", "classnames": "^2.2.5", "rc-resize-observer": "^1.1.0", - "rc-util": "^5.27.0" + "rc-util": "^5.27.1" }, "devDependencies": { "@types/enzyme": "^3.10.5", diff --git a/src/Cell/index.tsx b/src/Cell/index.tsx index 0d29783f0..32c595133 100644 --- a/src/Cell/index.tsx +++ b/src/Cell/index.tsx @@ -1,15 +1,15 @@ +import { useContext } from '@rc-component/context'; import classNames from 'classnames'; +import isEqual from 'rc-util/lib/isEqual'; import { supportRef } from 'rc-util/lib/ref'; -import warning from 'rc-util/lib/warning'; import getValue from 'rc-util/lib/utils/get'; +import warning from 'rc-util/lib/warning'; import * as React from 'react'; -import isEqual from 'rc-util/lib/isEqual'; import BodyContext from '../context/BodyContext'; import type { HoverContextProps } from '../context/HoverContext'; import HoverContext from '../context/HoverContext'; import PerfContext from '../context/PerfContext'; import StickyContext from '../context/StickyContext'; -import { useContext } from '@rc-component/context'; import type { AlignType, CellEllipsisType, @@ -21,7 +21,7 @@ import type { RenderedCell, ScopeType, } from '../interface'; -import { getPathValue, validateValue } from '../utils/valueUtil'; +import { validateValue } from '../utils/valueUtil'; /** Check if cell is in hover range */ function inHoverRange(cellStartRow: number, cellRowSpan: number, startRow: number, endRow: number) { @@ -157,10 +157,14 @@ function Cell( return [children]; } - const value = getPathValue | React.ReactNode, RecordType>( - record, - dataIndex, - ); + const path = + dataIndex === null || dataIndex === undefined || dataIndex === '' + ? [] + : Array.isArray(dataIndex) + ? dataIndex + : [dataIndex]; + + const value: Record | React.ReactNode = getValue(record, path); // Customize render node let returnChildNode = value; diff --git a/src/Table.tsx b/src/Table.tsx index 98af975f0..b3eb3a725 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -32,6 +32,7 @@ import { isStyleSupport } from 'rc-util/lib/Dom/styleChecker'; import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize'; import isEqual from 'rc-util/lib/isEqual'; import pickAttrs from 'rc-util/lib/pickAttrs'; +import getValue from 'rc-util/lib/utils/get'; import warning from 'rc-util/lib/warning'; import * as React from 'react'; import Body from './Body'; @@ -54,7 +55,6 @@ import useStickyOffsets from './hooks/useStickyOffsets'; import type { ColumnsType, ColumnType, - CustomizeComponent, CustomizeScrollBody, DefaultRecordType, ExpandableConfig, @@ -78,7 +78,7 @@ import ColumnGroup from './sugar/ColumnGroup'; import { findAllChildrenKeys, renderExpandIcon } from './utils/expandUtil'; import { getCellFixedInfo } from './utils/fixUtil'; import { getExpandableProps } from './utils/legacyUtil'; -import { getColumnsKey, getPathValue, validateValue } from './utils/valueUtil'; +import { getColumnsKey, validateValue } from './utils/valueUtil'; // Used for conditions cache const EMPTY_DATA = []; @@ -240,9 +240,7 @@ function Table(tableProps: TableProps( - (path, defaultComponent) => - getPathValue>(components || {}, path) || - defaultComponent, + (path, defaultComponent) => getValue(components, path) || defaultComponent, [components], ); diff --git a/src/utils/valueUtil.tsx b/src/utils/valueUtil.tsx index fa29af583..90b298e0b 100644 --- a/src/utils/valueUtil.tsx +++ b/src/utils/valueUtil.tsx @@ -1,4 +1,4 @@ -import type { Key, DataIndex } from '../interface'; +import type { DataIndex, Key } from '../interface'; const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY'; @@ -9,31 +9,6 @@ function toArray(arr: T | readonly T[]): T[] { return (Array.isArray(arr) ? arr : [arr]) as T[]; } -export function getPathValue( - record: ObjectType, - path: DataIndex, -): ValueType { - // Skip if path is empty - if (!path && typeof path !== 'number') { - return record as unknown as ValueType; - } - - const pathList = toArray(path); - - let current: ValueType | ObjectType = record; - - for (let i = 0; i < pathList.length; i += 1) { - if (!current) { - return null; - } - - const prop = pathList[i]; - current = current[prop]; - } - - return current as ValueType; -} - interface GetColumnKeyColumn { key?: Key; dataIndex?: DataIndex;