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 487daba87..32c595133 100644 --- a/src/Cell/index.tsx +++ b/src/Cell/index.tsx @@ -1,14 +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 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, @@ -20,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) { @@ -156,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 3226b7bd3..b3eb3a725 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -24,15 +24,17 @@ * - 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 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 Body from './Body'; import ColGroup from './ColGroup'; import { EXPAND_COLUMN } from './constant'; @@ -53,7 +55,6 @@ import useStickyOffsets from './hooks/useStickyOffsets'; import type { ColumnsType, ColumnType, - CustomizeComponent, CustomizeScrollBody, DefaultRecordType, ExpandableConfig, @@ -77,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 = []; @@ -171,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, }; @@ -235,9 +240,7 @@ function Table(tableProps: TableProps( - (path, defaultComponent) => - getPathValue>(components || {}, path) || - defaultComponent, + (path, defaultComponent) => getValue(components, path) || defaultComponent, [components], ); @@ -893,12 +896,20 @@ function Table(tableProps: TableProps(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;