Skip to content

Commit f94ce30

Browse files
authored
refactor: Part refactor (#918)
* chore: init * chore: make default stable * chore: fix refactor * refactor: use rc-util instead of internal get
1 parent 932df1b commit f94ce30

File tree

4 files changed

+37
-46
lines changed

4 files changed

+37
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@rc-component/context": "^1.0.0",
5757
"classnames": "^2.2.5",
5858
"rc-resize-observer": "^1.1.0",
59-
"rc-util": "^5.27.0"
59+
"rc-util": "^5.27.1"
6060
},
6161
"devDependencies": {
6262
"@types/enzyme": "^3.10.5",

src/Cell/index.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import { useContext } from '@rc-component/context';
12
import classNames from 'classnames';
3+
import isEqual from 'rc-util/lib/isEqual';
24
import { supportRef } from 'rc-util/lib/ref';
5+
import getValue from 'rc-util/lib/utils/get';
36
import warning from 'rc-util/lib/warning';
47
import * as React from 'react';
5-
import isEqual from 'rc-util/lib/isEqual';
68
import BodyContext from '../context/BodyContext';
79
import type { HoverContextProps } from '../context/HoverContext';
810
import HoverContext from '../context/HoverContext';
911
import PerfContext from '../context/PerfContext';
1012
import StickyContext from '../context/StickyContext';
11-
import { useContext } from '@rc-component/context';
1213
import type {
1314
AlignType,
1415
CellEllipsisType,
@@ -20,7 +21,7 @@ import type {
2021
RenderedCell,
2122
ScopeType,
2223
} from '../interface';
23-
import { getPathValue, validateValue } from '../utils/valueUtil';
24+
import { validateValue } from '../utils/valueUtil';
2425

2526
/** Check if cell is in hover range */
2627
function inHoverRange(cellStartRow: number, cellRowSpan: number, startRow: number, endRow: number) {
@@ -156,10 +157,14 @@ function Cell<RecordType extends DefaultRecordType>(
156157
return [children];
157158
}
158159

159-
const value = getPathValue<Record<string, unknown> | React.ReactNode, RecordType>(
160-
record,
161-
dataIndex,
162-
);
160+
const path =
161+
dataIndex === null || dataIndex === undefined || dataIndex === ''
162+
? []
163+
: Array.isArray(dataIndex)
164+
? dataIndex
165+
: [dataIndex];
166+
167+
const value: Record<string, unknown> | React.ReactNode = getValue(record, path);
163168

164169
// Customize render node
165170
let returnChildNode = value;

src/Table.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
* - All expanded props, move into expandable
2525
*/
2626

27+
import { makeImmutable } from '@rc-component/context';
2728
import classNames from 'classnames';
2829
import ResizeObserver from 'rc-resize-observer';
2930
import isVisible from 'rc-util/lib/Dom/isVisible';
3031
import { isStyleSupport } from 'rc-util/lib/Dom/styleChecker';
3132
import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize';
33+
import isEqual from 'rc-util/lib/isEqual';
3234
import pickAttrs from 'rc-util/lib/pickAttrs';
35+
import getValue from 'rc-util/lib/utils/get';
3336
import warning from 'rc-util/lib/warning';
3437
import * as React from 'react';
35-
import isEqual from 'rc-util/lib/isEqual';
3638
import Body from './Body';
3739
import ColGroup from './ColGroup';
3840
import { EXPAND_COLUMN } from './constant';
@@ -53,7 +55,6 @@ import useStickyOffsets from './hooks/useStickyOffsets';
5355
import type {
5456
ColumnsType,
5557
ColumnType,
56-
CustomizeComponent,
5758
CustomizeScrollBody,
5859
DefaultRecordType,
5960
ExpandableConfig,
@@ -77,7 +78,7 @@ import ColumnGroup from './sugar/ColumnGroup';
7778
import { findAllChildrenKeys, renderExpandIcon } from './utils/expandUtil';
7879
import { getCellFixedInfo } from './utils/fixUtil';
7980
import { getExpandableProps } from './utils/legacyUtil';
80-
import { getColumnsKey, getPathValue, validateValue } from './utils/valueUtil';
81+
import { getColumnsKey, validateValue } from './utils/valueUtil';
8182

8283
// Used for conditions cache
8384
const EMPTY_DATA = [];
@@ -171,11 +172,15 @@ export interface TableProps<RecordType = unknown>
171172
sticky?: boolean | TableSticky;
172173
}
173174

175+
function defaultEmpty() {
176+
return 'No Data';
177+
}
178+
174179
function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<RecordType>) {
175180
const props = {
176181
rowKey: 'key',
177182
prefixCls: 'rc-table',
178-
emptyText: () => 'No Data',
183+
emptyText: defaultEmpty,
179184
...tableProps,
180185
};
181186

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

236241
// ==================== Customize =====================
237242
const getComponent = React.useCallback<GetComponent>(
238-
(path, defaultComponent) =>
239-
getPathValue<CustomizeComponent, TableComponents<RecordType>>(components || {}, path) ||
240-
defaultComponent,
243+
(path, defaultComponent) => getValue(components, path) || defaultComponent,
241244
[components],
242245
);
243246

@@ -893,12 +896,20 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
893896
);
894897
}
895898

896-
Table.EXPAND_COLUMN = EXPAND_COLUMN;
899+
const ImmutableTable = makeImmutable(Table);
900+
type ImmutableTableType = typeof ImmutableTable & {
901+
EXPAND_COLUMN: typeof EXPAND_COLUMN;
902+
Column: typeof Column;
903+
ColumnGroup: typeof ColumnGroup;
904+
Summary: typeof FooterComponents;
905+
};
906+
907+
(ImmutableTable as ImmutableTableType).EXPAND_COLUMN = EXPAND_COLUMN;
897908

898-
Table.Column = Column;
909+
(ImmutableTable as ImmutableTableType).Column = Column;
899910

900-
Table.ColumnGroup = ColumnGroup;
911+
(ImmutableTable as ImmutableTableType).ColumnGroup = ColumnGroup;
901912

902-
Table.Summary = FooterComponents;
913+
(ImmutableTable as ImmutableTableType).Summary = FooterComponents;
903914

904-
export default Table;
915+
export default ImmutableTable as ImmutableTableType;

src/utils/valueUtil.tsx

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Key, DataIndex } from '../interface';
1+
import type { DataIndex, Key } from '../interface';
22

33
const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY';
44

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

12-
export function getPathValue<ValueType, ObjectType extends object>(
13-
record: ObjectType,
14-
path: DataIndex,
15-
): ValueType {
16-
// Skip if path is empty
17-
if (!path && typeof path !== 'number') {
18-
return record as unknown as ValueType;
19-
}
20-
21-
const pathList = toArray(path);
22-
23-
let current: ValueType | ObjectType = record;
24-
25-
for (let i = 0; i < pathList.length; i += 1) {
26-
if (!current) {
27-
return null;
28-
}
29-
30-
const prop = pathList[i];
31-
current = current[prop];
32-
}
33-
34-
return current as ValueType;
35-
}
36-
3712
interface GetColumnKeyColumn {
3813
key?: Key;
3914
dataIndex?: DataIndex;

0 commit comments

Comments
 (0)