Skip to content

Commit 4ae49a4

Browse files
authored
enhance: Let column.dataIndex support RecordType (#1090)
* chor: keyof check * chore: fix type
1 parent 9dc42af commit 4ae49a4

File tree

6 files changed

+14
-10
lines changed

6 files changed

+14
-10
lines changed

src/Body/BodyRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
158158
);
159159

160160
return (
161-
<Cell
161+
<Cell<RecordType>
162162
className={columnClassName}
163163
ellipsis={column.ellipsis}
164164
align={column.align}

src/Cell/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface CellProps<RecordType extends DefaultRecordType> {
2424
index?: number;
2525
/** the index of the record. For the render(value, record, renderIndex) */
2626
renderIndex?: number;
27-
dataIndex?: DataIndex;
27+
dataIndex?: DataIndex<RecordType>;
2828
render?: ColumnType<RecordType>['render'];
2929
component?: CustomizeComponent;
3030
children?: React.ReactNode;
@@ -256,4 +256,4 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
256256
);
257257
}
258258

259-
export default React.memo(Cell);
259+
export default React.memo(Cell) as typeof Cell;

src/Cell/useCellRender.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function isRenderCell<RecordType>(
1616

1717
export default function useCellRender<RecordType>(
1818
record: RecordType,
19-
dataIndex: DataIndex,
19+
dataIndex: DataIndex<RecordType>,
2020
renderIndex: number,
2121
children?: React.ReactNode,
2222
render?: ColumnType<RecordType>['render'],
@@ -37,8 +37,8 @@ export default function useCellRender<RecordType>(
3737
dataIndex === null || dataIndex === undefined || dataIndex === ''
3838
? []
3939
: Array.isArray(dataIndex)
40-
? dataIndex
41-
: [dataIndex];
40+
? dataIndex
41+
: [dataIndex];
4242

4343
const value: React.ReactNode = getValue(record, path);
4444

src/FixedHolder/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
3939
children: (info: HeaderProps<RecordType>) => React.ReactNode;
4040
}
4141

42-
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>((props, ref) => {
42+
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((props, ref) => {
4343
if (process.env.NODE_ENV !== 'production') {
4444
devRenderTimes(props);
4545
}

src/VirtualTable/VirtualCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { ColumnType, CustomizeComponent } from '../interface';
88
import { GridContext } from './context';
99

1010
export interface VirtualCellProps<RecordType> {
11-
rowInfo: ReturnType<typeof useRowInfo>;
11+
rowInfo: ReturnType<typeof useRowInfo<RecordType>>;
1212
column: ColumnType<RecordType>;
1313
colIndex: number;
1414
indent: number;

src/interface.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export interface RenderedCell<RecordType> {
6666

6767
export type Direction = 'ltr' | 'rtl';
6868

69-
export type DataIndex = string | number | readonly (string | number)[];
69+
export type DataIndex<RecordType = any> =
70+
| keyof RecordType
71+
| string
72+
| number
73+
| readonly (string | number)[];
7074

7175
export type CellEllipsisType = { showTitle?: boolean } | boolean;
7276

@@ -96,7 +100,7 @@ export type AlignType = 'start' | 'end' | 'left' | 'right' | 'center' | 'justify
96100

97101
export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
98102
colSpan?: number;
99-
dataIndex?: DataIndex;
103+
dataIndex?: DataIndex<RecordType>;
100104
render?: (
101105
value: any,
102106
record: RecordType,

0 commit comments

Comments
 (0)