Browse Source

type: optimization AnyObject type (#43366)

* type: optimization AnyObject type

* fix lint

* rerun
pull/43375/head
lijianan 1 year ago
committed by GitHub
parent
commit
67900a7aad
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      components/_util/reactNode.ts
  2. 2
      components/_util/type.ts
  3. 6
      components/table/InternalTable.tsx
  4. 7
      components/table/Table.tsx
  5. 2
      components/table/__tests__/Table.test.tsx
  6. 8
      components/table/hooks/useSelection.tsx
  7. 9
      components/table/interface.ts

3
components/_util/reactNode.ts

@ -1,4 +1,5 @@
import * as React from 'react';
import type { AnyObject } from './type';
export const { isValidElement } = React;
@ -6,8 +7,6 @@ export function isFragment(child: any): boolean {
return child && isValidElement(child) && child.type === React.Fragment;
}
type AnyObject = Record<PropertyKey, any>;
type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void);
export function replaceElement(

2
components/_util/type.ts

@ -1,2 +1,4 @@
/** https://github.com/Microsoft/TypeScript/issues/29729 */
export type LiteralUnion<T extends string> = T | (string & {});
export type AnyObject = Record<PropertyKey, any>;

6
components/table/InternalTable.tsx

@ -1,10 +1,11 @@
import classNames from 'classnames';
import { type TableProps as RcTableProps, INTERNAL_HOOKS } from 'rc-table';
import { INTERNAL_HOOKS, type TableProps as RcTableProps } from 'rc-table';
import { convertChildrenToColumns } from 'rc-table/lib/hooks/useColumns';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import type { Breakpoint } from '../_util/responsiveObserver';
import scrollTo from '../_util/scrollTo';
import type { AnyObject } from '../_util/type';
import warning from '../_util/warning';
import type { SizeType } from '../config-provider/SizeContext';
import type { ConfigConsumerProps } from '../config-provider/context';
@ -19,7 +20,6 @@ import Spin from '../spin';
import type { TooltipProps } from '../tooltip';
import renderExpandIcon from './ExpandIcon';
import RcTable from './RcTable';
import type { AnyObject } from './Table';
import type { FilterState } from './hooks/useFilter';
import useFilter, { getFilterData } from './hooks/useFilter';
import useLazyKVMap from './hooks/useLazyKVMap';
@ -109,7 +109,7 @@ export interface TableProps<RecordType>
showSorterTooltip?: boolean | TooltipProps;
}
const InternalTable = <RecordType extends AnyObject = any>(
const InternalTable = <RecordType extends AnyObject = AnyObject>(
props: InternalTableProps<RecordType>,
ref: React.MutableRefObject<HTMLDivElement>,
) => {

7
components/table/Table.tsx

@ -1,5 +1,6 @@
import { EXPAND_COLUMN, Summary } from 'rc-table';
import * as React from 'react';
import type { AnyObject } from '../_util/type';
import Column from './Column';
import ColumnGroup from './ColumnGroup';
import type { TableProps } from './InternalTable';
@ -12,13 +13,11 @@ import {
} from './hooks/useSelection';
import type { RefTable } from './interface';
export type AnyObject = Record<PropertyKey, any>;
const Table = <RecordType extends AnyObject = any>(
const Table = <RecordType extends AnyObject = AnyObject>(
props: TableProps<RecordType>,
ref: React.Ref<HTMLDivElement>,
) => {
const renderTimesRef = React.useRef(0);
const renderTimesRef = React.useRef<number>(0);
renderTimesRef.current += 1;
return <InternalTable<RecordType> {...props} ref={ref} _renderTimes={renderTimesRef.current} />;
};

2
components/table/__tests__/Table.test.tsx

@ -251,7 +251,7 @@ describe('Table', () => {
dataIndex: 'name',
},
];
render(<Table columns={columns} rowKey={(record, index) => record + index} />);
render(<Table columns={columns} rowKey={(record, index) => record.key + index} />);
expect(warnSpy).toHaveBeenCalledWith(
'Warning: [antd: Table] `index` parameter of `rowKey` function is deprecated. There is no guarantee that it will work as expected.',
);

8
components/table/hooks/useSelection.tsx

@ -9,12 +9,12 @@ import { convertDataToEntities } from 'rc-tree/lib/utils/treeUtil';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import * as React from 'react';
import { useCallback, useMemo, useState } from 'react';
import type { AnyObject } from '../../_util/type';
import warning from '../../_util/warning';
import type { CheckboxProps } from '../../checkbox';
import Checkbox from '../../checkbox';
import Dropdown from '../../dropdown';
import Radio from '../../radio';
import type { AnyObject } from '../Table';
import type {
ColumnType,
ColumnsType,
@ -38,7 +38,7 @@ export const SELECTION_NONE = 'SELECT_NONE' as const;
const EMPTY_LIST: React.Key[] = [];
interface UseSelectionConfig<RecordType extends AnyObject = any> {
interface UseSelectionConfig<RecordType extends AnyObject = AnyObject> {
prefixCls: string;
pageData: RecordType[];
data: RecordType[];
@ -56,7 +56,7 @@ export type INTERNAL_SELECTION_ITEM =
| typeof SELECTION_INVERT
| typeof SELECTION_NONE;
const flattenData = <RecordType extends AnyObject = any>(
const flattenData = <RecordType extends AnyObject = AnyObject>(
childrenColumnName: keyof RecordType,
data?: RecordType[],
): RecordType[] => {
@ -70,7 +70,7 @@ const flattenData = <RecordType extends AnyObject = any>(
return list;
};
const useSelection = <RecordType extends AnyObject = any>(
const useSelection = <RecordType extends AnyObject = AnyObject>(
config: UseSelectionConfig<RecordType>,
rowSelection?: TableRowSelection<RecordType>,
): readonly [TransformColumns<RecordType>, Set<Key>] => {

9
components/table/interface.ts

@ -7,23 +7,24 @@ import type {
import { ExpandableConfig, GetRowKey } from 'rc-table/lib/interface';
import type * as React from 'react';
import type { Breakpoint } from '../_util/responsiveObserver';
import type { AnyObject } from '../_util/type';
import type { CheckboxProps } from '../checkbox';
import type { PaginationProps } from '../pagination';
import type { TooltipProps } from '../tooltip';
import type { InternalTableProps, TableProps } from './InternalTable';
import type { INTERNAL_SELECTION_ITEM } from './hooks/useSelection';
export type RefTable = <RecordType extends object = any>(
export type RefTable = <RecordType extends AnyObject = AnyObject>(
props: React.PropsWithChildren<TableProps<RecordType>> & { ref?: React.Ref<HTMLDivElement> },
) => React.ReactElement;
export type RefInternalTable = <RecordType extends object = any>(
export type RefInternalTable = <RecordType extends AnyObject = AnyObject>(
props: React.PropsWithChildren<InternalTableProps<RecordType>> & {
ref?: React.Ref<HTMLDivElement>;
},
) => React.ReactElement;
export { GetRowKey, ExpandableConfig };
export { ExpandableConfig, GetRowKey };
export type Key = React.Key;
@ -82,7 +83,7 @@ export type ColumnTitle<RecordType> =
export type FilterValue = (Key | boolean)[];
export type FilterKey = Key[] | null;
export type FilterSearchType<RecordType = Record<string, any>> =
export type FilterSearchType<RecordType = AnyObject> =
| boolean
| ((input: string, record: RecordType) => boolean);
export interface FilterConfirmProps {

Loading…
Cancel
Save