Skip to content

Commit

Permalink
Merge pull request #222 from umijs/chore/removeAntdDeps
Browse files Browse the repository at this point in the history
chore: remove antd as peer dependency
  • Loading branch information
brickspert authored Jan 13, 2020
2 parents 9310f54 + 8f8e48e commit 0badf8f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"resize-observer-polyfill": "^1.5.1"
},
"peerDependencies": {
"antd": "^3.26.6",
"react": "^16.8.6"
},
"devDependencies": {
Expand Down
55 changes: 26 additions & 29 deletions src/useAntdTable/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { WrappedFormUtils } from 'antd/lib/form/Form';
import { PaginationConfig } from 'antd/lib/pagination';
import { SorterResult } from 'antd/lib/table';
import {
DependencyList,
useCallback,
Expand All @@ -11,23 +8,24 @@ import {
Reducer,
} from 'react';
import isEqual from 'lodash.isequal';
import { PaginationConfig, Sorter, Filter } from './typings';
import useAsync from '../useAsync';
import useUpdateEffect from '../useUpdateEffect';

interface UseAntdTableFormUtils extends WrappedFormUtils {
interface UseAntdTableFormUtils {
getFieldInstance?: (name: string) => {};
setFieldsValue?: (value: { [key: string]: any }) => void;
getFieldsValue?: (...args: any) => any;
resetFields?: () => void;
[key: string]: any;
}

export interface ReturnValue<Item> {
/* table 已经废弃 */
table?: {
dataSource: Item[];
loading: boolean;
onChange: (
pagination: PaginationConfig,
filters?: Partial<Record<keyof Item, string[]>>,
sorter?: SorterResult<Item>,
) => void;
onChange: (pagination: PaginationConfig, filters?: Filter, sorter?: Sorter) => void;
pagination: {
current: number;
pageSize: number;
Expand All @@ -37,19 +35,15 @@ export interface ReturnValue<Item> {
tableProps: {
dataSource: Item[];
loading: boolean;
onChange: (
pagination: PaginationConfig,
filters?: Partial<Record<keyof Item, string[]>>,
sorter?: SorterResult<Item>,
) => void;
onChange: (pagination: PaginationConfig, filters?: Filter, sorter?: Sorter) => void;
pagination: {
current: number;
pageSize: number;
total: number;
} & { [K in keyof PaginationConfig]?: PaginationConfig[K] };
};
sorter: SorterResult<Item>;
filters: Record<keyof Item, string[]>;
sorter: Sorter;
filters: Filter;
refresh: () => void;
// TODO 如果有 form,则一定有 search
search?: {
Expand Down Expand Up @@ -78,8 +72,8 @@ export interface Options<Result, Item> {
export interface FnParams<Item> {
current: number;
pageSize: number;
sorter?: SorterResult<Item>;
filters?: Record<keyof Item, string[]>;
sorter?: Sorter;
filters?: Filter;
[key: string]: any;
}

Expand Down Expand Up @@ -112,9 +106,9 @@ class UseTableInitState<Item> {
// 列表数据
data: Item[] = [];

filters: Record<keyof Item, string[]> = {} as Record<keyof Item, string[]>;
filters: Filter = {} as Filter;

sorter: SorterResult<Item> = {} as SorterResult<Item>;
sorter: Sorter = {} as Sorter;
}

// 缓存
Expand Down Expand Up @@ -263,7 +257,9 @@ function useAntdTable<Result, Item>(
existFormData[key] = targetFormData[key];
}
});
form.setFieldsValue(existFormData);
if (form.setFieldsValue) {
form.setFieldsValue(existFormData);
}
tempFieldsValueRef.current = {};
}, [state.searchType, state.formData]);

Expand All @@ -272,7 +268,10 @@ function useAntdTable<Result, Item>(
if (!form) {
return [];
}
const fieldsValue = form.getFieldsValue();
let fieldsValue = {} as FormData;
if (form.getFieldsValue) {
fieldsValue = form.getFieldsValue();
}
const filterFiledsValue: FormData = {};
Object.keys(fieldsValue).forEach((key: string) => {
if (form.getFieldInstance ? form.getFieldInstance(key) : true) {
Expand Down Expand Up @@ -313,7 +312,9 @@ function useAntdTable<Result, Item>(
return;
}
// 恢复初始值
form.resetFields();
if (form.resetFields) {
form.resetFields();
}
// 重置表单后,拿到当前默认值
const activeFormData = getCurrentFieldsValues();

Expand Down Expand Up @@ -345,23 +346,19 @@ function useAntdTable<Result, Item>(

// 表格翻页 排序 筛选等
const changeTable = useCallback(
(
p: PaginationConfig,
f: Partial<Record<keyof Item, string[]>> = {} as Partial<Record<keyof Item, string[]>>,
s: SorterResult<Item> = {} as SorterResult<Item>,
) => {
(p: PaginationConfig, f: Filter = {} as Filter, s: Sorter = {} as Sorter) => {
// antd table 的初始状态 filter 带有 null 字段,需要先去除后再比较
const realFilter = { ...f };
Object.entries(realFilter).forEach(item => {
if (item[1] === null) {
delete (realFilter as Object)[item[0] as keyof Object];
}
});
/* 如果 filter,或者 sort 变化,就初始化 current */
const needReload =
!isEqual(realFilter, state.filters) ||
s.field !== state.sorter.field ||
s.order !== state.sorter.order;

dispatch({
type: 'updateState',
payload: {
Expand Down
45 changes: 45 additions & 0 deletions src/useAntdTable/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export interface PaginationConfig {
total?: number;
defaultCurrent?: number;
disabled?: boolean;
current?: number;
defaultPageSize?: number;
pageSize?: number;
onChange?: (page: number, pageSize?: number) => void;
hideOnSinglePage?: boolean;
showSizeChanger?: boolean;
pageSizeOptions?: string[];
onShowSizeChange?: (current: number, size: number) => void;
showQuickJumper?:
| boolean
| {
goButton?: React.ReactNode;
};
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
size?: string;
simple?: boolean;
style?: React.CSSProperties;
locale?: Object;
className?: string;
prefixCls?: string;
selectPrefixCls?: string;
itemRender?: (
page: number,
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
originalElement: React.ReactElement<HTMLElement>,
) => React.ReactNode;
role?: string;
showLessItems?: boolean;
position?: 'top' | 'bottom' | 'both';
[key: string]: any;
}

export interface Sorter {
column?: any;
order?: 'ascend' | 'descend';
field?: string;
columnKey?: string;
[key: string]: any;
}

export type Filter = any;

1 comment on commit 0badf8f

@vercel
Copy link

@vercel vercel bot commented on 0badf8f Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.