Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 12 additions & 7 deletions src/Cell/index.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -156,10 +157,14 @@ function Cell<RecordType extends DefaultRecordType>(
return [children];
}

const value = getPathValue<Record<string, unknown> | React.ReactNode, RecordType>(
record,
dataIndex,
);
const path =
dataIndex === null || dataIndex === undefined || dataIndex === ''
? []
: Array.isArray(dataIndex)
? dataIndex
: [dataIndex];

const value: Record<string, unknown> | React.ReactNode = getValue(record, path);

// Customize render node
let returnChildNode = value;
Expand Down
35 changes: 23 additions & 12 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -53,7 +55,6 @@ import useStickyOffsets from './hooks/useStickyOffsets';
import type {
ColumnsType,
ColumnType,
CustomizeComponent,
CustomizeScrollBody,
DefaultRecordType,
ExpandableConfig,
Expand All @@ -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 = [];
Expand Down Expand Up @@ -171,11 +172,15 @@ export interface TableProps<RecordType = unknown>
sticky?: boolean | TableSticky;
}

function defaultEmpty() {
return 'No Data';
}

function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<RecordType>) {
const props = {
rowKey: 'key',
prefixCls: 'rc-table',
emptyText: () => 'No Data',
emptyText: defaultEmpty,
...tableProps,
};

Expand Down Expand Up @@ -235,9 +240,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco

// ==================== Customize =====================
const getComponent = React.useCallback<GetComponent>(
(path, defaultComponent) =>
getPathValue<CustomizeComponent, TableComponents<RecordType>>(components || {}, path) ||
defaultComponent,
(path, defaultComponent) => getValue(components, path) || defaultComponent,
[components],
);

Expand Down Expand Up @@ -893,12 +896,20 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
);
}

Table.EXPAND_COLUMN = EXPAND_COLUMN;
const ImmutableTable = makeImmutable(Table);
type ImmutableTableType = typeof ImmutableTable & {
EXPAND_COLUMN: typeof EXPAND_COLUMN;
Column: typeof Column;
ColumnGroup: typeof ColumnGroup;
Summary: typeof FooterComponents;
};

(ImmutableTable as ImmutableTableType).EXPAND_COLUMN = EXPAND_COLUMN;

Table.Column = Column;
(ImmutableTable as ImmutableTableType).Column = Column;

Table.ColumnGroup = ColumnGroup;
(ImmutableTable as ImmutableTableType).ColumnGroup = ColumnGroup;

Table.Summary = FooterComponents;
(ImmutableTable as ImmutableTableType).Summary = FooterComponents;

export default Table;
export default ImmutableTable as ImmutableTableType;
27 changes: 1 addition & 26 deletions src/utils/valueUtil.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Key, DataIndex } from '../interface';
import type { DataIndex, Key } from '../interface';

const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY';

Expand All @@ -9,31 +9,6 @@ function toArray<T>(arr: T | readonly T[]): T[] {
return (Array.isArray(arr) ? arr : [arr]) as T[];
}

export function getPathValue<ValueType, ObjectType extends object>(
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;
Expand Down