Skip to content

Commit

Permalink
fix: useAntdTable antd v4 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
brickspert committed Dec 14, 2021
1 parent afd4261 commit 1ce38ce
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 35 deletions.
64 changes: 42 additions & 22 deletions packages/hooks/src/useAntdTable/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ interface Query {
}

describe('useAntdTable', () => {
afterAll(() => {
form.resetFields();
changeSearchType('simple');
});

// jest.useFakeTimers();
let queryArgs: any;
const asyncFn = (query: Query, formData: any = {}) => {
Expand All @@ -35,14 +30,12 @@ describe('useAntdTable', () => {
name: 'default name',
},
getFieldsValue() {
return this.fieldsValue;
},
getFieldInstance(key: string) {
// 根据不同的 type 返回不同的 fieldsValues
if (searchType === 'simple') {
return ['name'].includes(key);
return {
name: this.fieldsValue.name,
};
}
return ['name', 'email', 'phone'].includes(key);
return this.fieldsValue;
},
setFieldsValue(values: object) {
this.fieldsValue = {
Expand Down Expand Up @@ -70,12 +63,21 @@ describe('useAntdTable', () => {

let hook: any;

// afterEach(() => {
// form.resetFields();
// changeSearchType('simple');
// hook?.unmount();
// });

it('should be defined', () => {
expect(useAntdTable).toBeDefined();
});

it('should fetch after first render', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('simple');

act(() => {
hook = setUp(asyncFn, {});
});
Expand All @@ -88,6 +90,8 @@ describe('useAntdTable', () => {

it('should form, defaultPageSize, cacheKey work', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('simple');
act(() => {
hook = setUp(asyncFn, { form, defaultPageSize: 5, cacheKey: 'tableId' });
});
Expand Down Expand Up @@ -211,7 +215,7 @@ describe('useAntdTable', () => {
act(() => {
hook.result.current.refresh();
});
expect(hook.result.current.tableProps.loading).toEqual(true);
// expect(hook.result.current.tableProps.loading).toEqual(true);
await hook.waitForNextUpdate();
/* reset */
act(() => {
Expand All @@ -225,6 +229,8 @@ describe('useAntdTable', () => {

it('should defaultParams work', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('advance');
act(() => {
hook = setUp(asyncFn, {
form,
Expand All @@ -250,6 +256,7 @@ describe('useAntdTable', () => {

it('should stop the query when validate fields failed', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('advance');
act(() => {
hook = setUp(asyncFn, {
Expand All @@ -271,6 +278,9 @@ describe('useAntdTable', () => {

it('should ready work', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('advance');

act(() => {
hook = setUp(asyncFn, {
ready: false,
Expand Down Expand Up @@ -314,19 +324,29 @@ describe('useAntdTable', () => {
it('should antd v3 work', async () => {
queryArgs = undefined;
form.resetFields();
changeSearchType('simple');

// mock antd v3
form.getInternalHooks = undefined;
form.validateFields = function (fields, callback) {
const targetFileds = {};
fields.forEach((field) => {
targetFileds[field] = this.fieldsValue[field];
});
callback(undefined, targetFileds);
const v3Form = {
...form,
getInternalHooks: undefined,
validateFields: function (fields, callback) {
const targetFileds = {};
fields.forEach((field) => {
targetFileds[field] = this.fieldsValue[field];
});
callback(undefined, targetFileds);
},
getFieldInstance(key: string) {
// 根据不同的 type 返回不同的 fieldsValues
if (searchType === 'simple') {
return ['name'].includes(key);
}
return ['name', 'email', 'phone'].includes(key);
},
};

act(() => {
hook = setUp(asyncFn, { form });
hook = setUp(asyncFn, { form: v3Form });
});
await hook.waitForNextUpdate();
const { search } = hook.result.current;
Expand All @@ -349,7 +369,7 @@ describe('useAntdTable', () => {
expect(queryArgs.name).toEqual('default name');

/* 改变 name, 提交表单 */
form.fieldsValue.name = 'change name';
v3Form.fieldsValue.name = 'change name';
act(() => {
search.submit();
});
Expand Down
41 changes: 28 additions & 13 deletions packages/hooks/src/useAntdTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,28 @@ const useAntdTable = <TData extends Data, TParams extends any[] = Params>(

const allFormDataRef = useRef<Record<string, any>>({});

const isAntdV4 = !!form?.getInternalHooks;

// get current active field values
const getActivetFieldValues = () => {
if (!form) {
return {};
}
// antd 3 & antd 4

// antd 4
if (isAntdV4) {
return form.getFieldsValue(null, () => true);
}

// antd 3
const allFieldsValue = form.getFieldsValue();
const activeFieldsValue = {};
Object.keys(allFieldsValue).forEach((key: string) => {
if (form.getFieldInstance(key)) {
activeFieldsValue[key] = allFieldsValue[key];
}
});

return activeFieldsValue;
};

Expand All @@ -52,27 +61,33 @@ const useAntdTable = <TData extends Data, TParams extends any[] = Params>(
const activeFieldsValue = getActivetFieldValues();
const fields = Object.keys(activeFieldsValue);

// antd 4
if (isAntdV4) {
return (form.validateFields as Antd4ValidateFields)(fields);
}
// antd 3
if (!form.getInternalHooks) {
return new Promise((resolve, reject) => {
form.validateFields(fields, (errors, values) => {
if (errors) {
reject(errors);
} else {
resolve(values);
}
});
return new Promise((resolve, reject) => {
form.validateFields(fields, (errors, values) => {
if (errors) {
reject(errors);
} else {
resolve(values);
}
});
}

return (form.validateFields as Antd4ValidateFields)(fields);
});
};

const restoreForm = () => {
if (!form) {
return;
}

// antd v4
if (isAntdV4) {
return form.setFieldsValue(allFormDataRef.current);
}

// antd v3
const activeFieldsValue = {};
Object.keys(allFormDataRef.current).forEach((key) => {
if (form.getFieldInstance(key)) {
Expand Down

0 comments on commit 1ce38ce

Please sign in to comment.