Skip to content

Commit 178fb19

Browse files
authored
feat: add dataIndex type check (#1091)
* feat: add dataIndex type check * fix: ts * feat: add source * fix: ts * fix: ts * feat: 泛型默认值设置为 any * feat: add type test * feat: 优化 ddemo * feat: support empty string * feat: support empty string
1 parent 0c74710 commit 178fb19

36 files changed

+174
-84
lines changed

docs/examples/animation.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import CSSMotionList from 'rc-animate/lib/CSSMotionList';
33
import classNames from 'classnames';
44
import toArray from 'rc-util/lib/Children/toArray';
5+
import type { TableProps } from 'rc-table';
56
import Table from 'rc-table';
67
import '../../assets/index.less';
78
import './animation.less';
@@ -10,7 +11,7 @@ type MotionBodyProps = React.HTMLAttributes<HTMLTableSectionElement>;
1011

1112
const MotionBody: React.FC<MotionBodyProps> = ({ children, ...props }) => {
1213
const nodeList = toArray(children);
13-
const nodesRef = React.useRef<Record<React.Key, React.ReactElement>>({});
14+
const nodesRef = React.useRef<Record<string, React.ReactElement>>({});
1415

1516
// Better apply clean up logic to avoid OOM
1617
const keys: React.Key[] = [];
@@ -46,9 +47,9 @@ interface DemoState {
4647
}
4748

4849
class Demo extends React.Component<{}, DemoState> {
49-
columns = [
50+
columns: TableProps['columns'] = [
5051
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
51-
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
52+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
5253
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
5354
{
5455
title: 'Operations',

docs/examples/aria.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
interface FieldType {
7+
name?: string;
8+
age?: string;
9+
address?: string;
10+
}
11+
12+
const columns: TableProps<FieldType>['columns'] = [
613
{
714
title: 'Name',
815
dataIndex: 'name',

docs/examples/caption.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{
78
title: 'Name',
89
dataIndex: 'name',

docs/examples/childrenIndent.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

@@ -8,6 +9,7 @@ interface RecordType {
89
age: number;
910
address: string;
1011
children?: RecordType[];
12+
operation?: string;
1113
}
1214

1315
function CustomExpandIcon(props) {
@@ -27,7 +29,7 @@ function CustomExpandIcon(props) {
2729
);
2830
}
2931

30-
const columns = [
32+
const columns: TableProps<RecordType>['columns'] = [
3133
{
3234
title: 'Name',
3335
dataIndex: 'name',

docs/examples/className.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{
78
title: 'title1',
89
dataIndex: 'a',
@@ -11,7 +12,6 @@ const columns = [
1112
width: 100,
1213
},
1314
{
14-
id: '123',
1515
title: 'title2',
1616
dataIndex: 'b',
1717
className: 'b',

docs/examples/colspan-rowspan-legacy.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Table from 'rc-table';
33
import '../../assets/index.less';
4-
import { ColumnsType, RenderedCell } from '@/interface';
4+
import type { ColumnsType, RenderedCell } from '@/interface';
55

66
interface RecordType {
77
a?: string;
@@ -116,7 +116,6 @@ const columns: ColumnsType<RecordType> = [
116116
},
117117
{
118118
title: 'Operations',
119-
dataIndex: '',
120119
key: 'f',
121120
render(o, row, index) {
122121
if (index === 5) {

docs/examples/colspan-rowspan.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ const columns: ColumnsType<RecordType> = [
9797
},
9898
{
9999
title: 'Operations',
100-
dataIndex: '',
101100
key: 'f',
102101
render() {
103102
return <a href="#">Operations</a>;

docs/examples/column-resize.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Resizable } from 'react-resizable';
33
import Table from 'rc-table';
44
import '../../assets/index.less';
55
import 'react-resizable/css/styles.css';
6-
import { ColumnType } from '@/interface';
6+
import type { ColumnType } from '@/interface';
77

88
const ResizableTitle = props => {
99
const { onResize, width, ...restProps } = props;
@@ -31,7 +31,6 @@ interface DemoState {
3131
columns: ColumnType<RecordType>[];
3232
}
3333

34-
3534
class Demo extends React.Component<{}, DemoState> {
3635
state: DemoState = {
3736
columns: [
@@ -61,16 +60,18 @@ class Demo extends React.Component<{}, DemoState> {
6160
{ a: '1333', c: 'eee', d: 2, key: '3' },
6261
];
6362

64-
handleResize = index => (e, { size }) => {
65-
this.setState(({ columns }) => {
66-
const nextColumns = [...columns];
67-
nextColumns[index] = {
68-
...nextColumns[index],
69-
width: size.width,
70-
};
71-
return { columns: nextColumns };
72-
});
73-
};
63+
handleResize =
64+
index =>
65+
(e, { size }) => {
66+
this.setState(({ columns }) => {
67+
const nextColumns = [...columns];
68+
nextColumns[index] = {
69+
...nextColumns[index],
70+
width: size.width,
71+
};
72+
return { columns: nextColumns };
73+
});
74+
};
7475

7576
render() {
7677
const columns = this.state.columns.map((col, index) => ({
@@ -79,7 +80,7 @@ class Demo extends React.Component<{}, DemoState> {
7980
({
8081
width: column.width,
8182
onResize: this.handleResize(index),
82-
} as any),
83+
}) as any,
8384
}));
8485

8586
return (

docs/examples/ellipsis-custom-tooltip.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import Tooltip from 'rc-tooltip';
3+
import type { TableProps } from 'rc-table';
34
import Table from 'rc-table';
45
import '../../assets/index.less';
56
import 'rc-tooltip/assets/bootstrap.css';
@@ -23,7 +24,7 @@ const createColumns = (length: number) => {
2324
}));
2425
};
2526

26-
const columns = [
27+
const columns: TableProps['columns'] = [
2728
{
2829
title: 'name',
2930
dataIndex: 'name',

docs/examples/ellipsis.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{ title: 'name', dataIndex: 'name', width: 100, ellipsis: true },
78
{ title: 'descrption', dataIndex: 'descrption', key: 'descrption 1', ellipsis: true, width: 50 },
89
{ title: 'descrption', dataIndex: 'descrption', key: 'descrption 2', ellipsis: true },

docs/examples/expandedRowClassName.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34

45
import styles from './expandedRowClassName.module.less';
56

6-
const columns = [
7+
const columns: TableProps['columns'] = [
78
{
89
title: 'Name',
910
dataIndex: 'name',

docs/examples/expandedRowRender.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const Demo = () => {
5757
{ title: 'title 1', dataIndex: 'a', key: 'a', width: 100 },
5858
{ title: 'title 2', dataIndex: 'b', key: 'b', width: 100 },
5959
{ title: 'title 3', dataIndex: 'c', key: 'c', width: 200 },
60-
{ title: 'Operation', dataIndex: '', key: 'x', render: renderAction },
60+
{ title: 'Operation', key: 'x', render: renderAction },
6161
];
6262

6363
if (fixColumns) {

docs/examples/fixedColumns-resize.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useCallback } from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45
import type { ColumnType } from '@/interface';
@@ -45,7 +46,7 @@ const Demo = () => {
4546
const [isShown, setIsShown] = useState(false);
4647
const [renderTime, setRenderTime] = useState(0);
4748
const [isFixed, setIsFixed] = useState(true);
48-
const [columns, setColumns] = useState(defaultColumns);
49+
const [columns, setColumns] = useState<TableProps<RecordType>['columns']>(defaultColumns);
4950
const onToggleSideBar = useCallback(() => {
5051
const s = window.performance.now();
5152
setIsShown(v => !v);
@@ -82,7 +83,7 @@ const Demo = () => {
8283
});
8384
}, []);
8485

85-
const expandedRowRender = useCallback(({ b, c }) => b || c, []);
86+
const expandedRowRender = useCallback<TableProps['expandedRowRender']>(({ b, c }) => b || c, []);
8687

8788
return (
8889
<div>
@@ -106,7 +107,7 @@ const Demo = () => {
106107
>
107108
<div style={{ flex: `0 0 ${isShown ? '10px' : '80px'}` }} />
108109
<div style={{ flex: 1, overflow: 'hidden' }}>
109-
<Table
110+
<Table<RecordType>
110111
columns={columns}
111112
scroll={isFixed ? { x: 1200 } : null}
112113
data={data}

docs/examples/grouping-columns-hidden.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{
78
title: '姓名',
89
dataIndex: 'name',

docs/examples/grouping-columns.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{
78
title: '姓名',
89
dataIndex: 'name',

docs/examples/hide-header.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
7-
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
8+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
89
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
910
{
1011
title: 'Operations',

docs/examples/nested.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{ title: 'First Name', dataIndex: ['names', 'first'], key: 'a', width: 100 },
78
{ title: 'Last Name', dataIndex: ['names', 'last'], key: 'b', width: 100 },
89
{ title: 'Age', dataIndex: 'age', key: 'c', width: 100 },

docs/examples/react-dnd.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { createGlobalStyle } from 'styled-components';
55
import update from 'immutability-helper';
66
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
77
import HTML5Backend from 'react-dnd-html5-backend';
8+
import type { TableProps } from 'rc-table';
89
import Table from 'rc-table';
910
import '../../assets/index.less';
1011

12+
// eslint-disable-next-line @babel/no-unused-expressions
1113
createGlobalStyle`
1214
tr.drop-over-downward td {
1315
border-bottom: 2px dashed red;
@@ -113,9 +115,9 @@ BodyRow = DropTarget('row', rowTarget, (connect, monitor) => ({
113115
}))(BodyRow),
114116
);
115117

116-
const columns = [
118+
const columns: TableProps['columns'] = [
117119
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
118-
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
120+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
119121
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
120122
{
121123
title: 'Operations',

docs/examples/row-hoverable.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ interface RecordType {
1010
const data = [{ a: 'A' }, { a: 'B' }, { a: 'C' }];
1111

1212
const Demo = () => {
13-
const columns: TableProps<any>['columns'] = [
14-
{
15-
title: 'title',
16-
dataIndex: 'a',
17-
},
18-
];
13+
const columns: TableProps<RecordType>['columns'] = [{ title: 'title', dataIndex: 'a' }];
1914

20-
return <Table<RecordType> columns={columns} data={data} rowHoverable={false} />;
15+
return <Table columns={columns} data={data} rowHoverable={false} />;
2116
};
2217

2318
export default Demo;

docs/examples/scopeCol.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ interface SecondTableRecordType {
2929
producedVenus?: string;
3030
soldVenus?: string;
3131
key?: string;
32+
mars?: string;
33+
venus?: string;
3234
}
3335

3436
const secondTableColumns: ColumnsType<SecondTableRecordType> = [

docs/examples/scrollX.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6-
{
7-
title: 'title1',
8-
dataIndex: 'a',
9-
key: 'a',
10-
width: 100,
11-
},
6+
const columns: TableProps['columns'] = [
7+
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
128
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
139
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100 },
1410
{ title: 'title4', dataIndex: 'b', key: 'd', width: 100 },

docs/examples/scrollXY.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { TableProps } from 'rc-table';
23
import Table from 'rc-table';
34
import '../../assets/index.less';
45

5-
const columns = [
6+
const columns: TableProps['columns'] = [
67
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
78
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
89
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100 },
@@ -43,12 +44,7 @@ const Demo = () => {
4344
Trigger Visible
4445
</button>
4546
<div style={{ display: visible ? undefined : 'none' }}>
46-
<Table
47-
style={{ width: 800 }}
48-
scroll={{ x: 1500, y: 300 }}
49-
columns={columns}
50-
data={data}
51-
/>
47+
<Table style={{ width: 800 }} scroll={{ x: 1500, y: 300 }} columns={columns} data={data} />
5248
</div>
5349
</div>
5450
);

0 commit comments

Comments
 (0)