Skip to content

Commit

Permalink
feat: add dataIndex type check (#1091)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
crazyair authored Mar 27, 2024
1 parent 0c74710 commit 178fb19
Show file tree
Hide file tree
Showing 36 changed files with 174 additions and 84 deletions.
7 changes: 4 additions & 3 deletions docs/examples/animation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import CSSMotionList from 'rc-animate/lib/CSSMotionList';
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';
import './animation.less';
Expand All @@ -10,7 +11,7 @@ type MotionBodyProps = React.HTMLAttributes<HTMLTableSectionElement>;

const MotionBody: React.FC<MotionBodyProps> = ({ children, ...props }) => {
const nodeList = toArray(children);
const nodesRef = React.useRef<Record<React.Key, React.ReactElement>>({});
const nodesRef = React.useRef<Record<string, React.ReactElement>>({});

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

class Demo extends React.Component<{}, DemoState> {
columns = [
columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
Expand Down
9 changes: 8 additions & 1 deletion docs/examples/aria.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
interface FieldType {
name?: string;
age?: string;
address?: string;
}

const columns: TableProps<FieldType>['columns'] = [
{
title: 'Name',
dataIndex: 'name',
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/caption.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{
title: 'Name',
dataIndex: 'name',
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/childrenIndent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

Expand All @@ -8,6 +9,7 @@ interface RecordType {
age: number;
address: string;
children?: RecordType[];
operation?: string;
}

function CustomExpandIcon(props) {
Expand All @@ -27,7 +29,7 @@ function CustomExpandIcon(props) {
);
}

const columns = [
const columns: TableProps<RecordType>['columns'] = [
{
title: 'Name',
dataIndex: 'name',
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/className.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{
title: 'title1',
dataIndex: 'a',
Expand All @@ -11,7 +12,6 @@ const columns = [
width: 100,
},
{
id: '123',
title: 'title2',
dataIndex: 'b',
className: 'b',
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/colspan-rowspan-legacy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Table from 'rc-table';
import '../../assets/index.less';
import { ColumnsType, RenderedCell } from '@/interface';
import type { ColumnsType, RenderedCell } from '@/interface';

interface RecordType {
a?: string;
Expand Down Expand Up @@ -116,7 +116,6 @@ const columns: ColumnsType<RecordType> = [
},
{
title: 'Operations',
dataIndex: '',
key: 'f',
render(o, row, index) {
if (index === 5) {
Expand Down
1 change: 0 additions & 1 deletion docs/examples/colspan-rowspan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ const columns: ColumnsType<RecordType> = [
},
{
title: 'Operations',
dataIndex: '',
key: 'f',
render() {
return <a href="#">Operations</a>;
Expand Down
27 changes: 14 additions & 13 deletions docs/examples/column-resize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Resizable } from 'react-resizable';
import Table from 'rc-table';
import '../../assets/index.less';
import 'react-resizable/css/styles.css';
import { ColumnType } from '@/interface';
import type { ColumnType } from '@/interface';

const ResizableTitle = props => {
const { onResize, width, ...restProps } = props;
Expand Down Expand Up @@ -31,7 +31,6 @@ interface DemoState {
columns: ColumnType<RecordType>[];
}


class Demo extends React.Component<{}, DemoState> {
state: DemoState = {
columns: [
Expand Down Expand Up @@ -61,16 +60,18 @@ class Demo extends React.Component<{}, DemoState> {
{ a: '1333', c: 'eee', d: 2, key: '3' },
];

handleResize = index => (e, { size }) => {
this.setState(({ columns }) => {
const nextColumns = [...columns];
nextColumns[index] = {
...nextColumns[index],
width: size.width,
};
return { columns: nextColumns };
});
};
handleResize =
index =>
(e, { size }) => {
this.setState(({ columns }) => {
const nextColumns = [...columns];
nextColumns[index] = {
...nextColumns[index],
width: size.width,
};
return { columns: nextColumns };
});
};

render() {
const columns = this.state.columns.map((col, index) => ({
Expand All @@ -79,7 +80,7 @@ class Demo extends React.Component<{}, DemoState> {
({
width: column.width,
onResize: this.handleResize(index),
} as any),
}) as any,
}));

return (
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/ellipsis-custom-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Tooltip from 'rc-tooltip';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';
import 'rc-tooltip/assets/bootstrap.css';
Expand All @@ -23,7 +24,7 @@ const createColumns = (length: number) => {
}));
};

const columns = [
const columns: TableProps['columns'] = [
{
title: 'name',
dataIndex: 'name',
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/ellipsis.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{ title: 'name', dataIndex: 'name', width: 100, ellipsis: true },
{ title: 'descrption', dataIndex: 'descrption', key: 'descrption 1', ellipsis: true, width: 50 },
{ title: 'descrption', dataIndex: 'descrption', key: 'descrption 2', ellipsis: true },
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/expandedRowClassName.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';

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

const columns = [
const columns: TableProps['columns'] = [
{
title: 'Name',
dataIndex: 'name',
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/expandedRowRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const Demo = () => {
{ title: 'title 1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title 2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title 3', dataIndex: 'c', key: 'c', width: 200 },
{ title: 'Operation', dataIndex: '', key: 'x', render: renderAction },
{ title: 'Operation', key: 'x', render: renderAction },
];

if (fixColumns) {
Expand Down
7 changes: 4 additions & 3 deletions docs/examples/fixedColumns-resize.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState, useCallback } from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';
import type { ColumnType } from '@/interface';
Expand Down Expand Up @@ -45,7 +46,7 @@ const Demo = () => {
const [isShown, setIsShown] = useState(false);
const [renderTime, setRenderTime] = useState(0);
const [isFixed, setIsFixed] = useState(true);
const [columns, setColumns] = useState(defaultColumns);
const [columns, setColumns] = useState<TableProps<RecordType>['columns']>(defaultColumns);
const onToggleSideBar = useCallback(() => {
const s = window.performance.now();
setIsShown(v => !v);
Expand Down Expand Up @@ -82,7 +83,7 @@ const Demo = () => {
});
}, []);

const expandedRowRender = useCallback(({ b, c }) => b || c, []);
const expandedRowRender = useCallback<TableProps['expandedRowRender']>(({ b, c }) => b || c, []);

return (
<div>
Expand All @@ -106,7 +107,7 @@ const Demo = () => {
>
<div style={{ flex: `0 0 ${isShown ? '10px' : '80px'}` }} />
<div style={{ flex: 1, overflow: 'hidden' }}>
<Table
<Table<RecordType>
columns={columns}
scroll={isFixed ? { x: 1200 } : null}
data={data}
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/grouping-columns-hidden.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{
title: '姓名',
dataIndex: 'name',
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/grouping-columns.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{
title: '姓名',
dataIndex: 'name',
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/hide-header.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/nested.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{ title: 'First Name', dataIndex: ['names', 'first'], key: 'a', width: 100 },
{ title: 'Last Name', dataIndex: ['names', 'last'], key: 'b', width: 100 },
{ title: 'Age', dataIndex: 'age', key: 'c', width: 100 },
Expand Down
6 changes: 4 additions & 2 deletions docs/examples/react-dnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { createGlobalStyle } from 'styled-components';
import update from 'immutability-helper';
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

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

const columns = [
const columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
Expand Down
9 changes: 2 additions & 7 deletions docs/examples/row-hoverable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ interface RecordType {
const data = [{ a: 'A' }, { a: 'B' }, { a: 'C' }];

const Demo = () => {
const columns: TableProps<any>['columns'] = [
{
title: 'title',
dataIndex: 'a',
},
];
const columns: TableProps<RecordType>['columns'] = [{ title: 'title', dataIndex: 'a' }];

return <Table<RecordType> columns={columns} data={data} rowHoverable={false} />;
return <Table columns={columns} data={data} rowHoverable={false} />;
};

export default Demo;
2 changes: 2 additions & 0 deletions docs/examples/scopeCol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ interface SecondTableRecordType {
producedVenus?: string;
soldVenus?: string;
key?: string;
mars?: string;
venus?: string;
}

const secondTableColumns: ColumnsType<SecondTableRecordType> = [
Expand Down
10 changes: 3 additions & 7 deletions docs/examples/scrollX.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
{
title: 'title1',
dataIndex: 'a',
key: 'a',
width: 100,
},
const columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100 },
{ title: 'title4', dataIndex: 'b', key: 'd', width: 100 },
Expand Down
10 changes: 3 additions & 7 deletions docs/examples/scrollXY.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
const columns: TableProps['columns'] = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 100 },
Expand Down Expand Up @@ -43,12 +44,7 @@ const Demo = () => {
Trigger Visible
</button>
<div style={{ display: visible ? undefined : 'none' }}>
<Table
style={{ width: 800 }}
scroll={{ x: 1500, y: 300 }}
columns={columns}
data={data}
/>
<Table style={{ width: 800 }} scroll={{ x: 1500, y: 300 }} columns={columns} data={data} />
</div>
</div>
);
Expand Down
Loading

0 comments on commit 178fb19

Please sign in to comment.