Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): 修复transform方法与文档用法预期不符 #7486

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/utils/src/transformKeySubmitValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,27 @@ export const transformKeySubmitValue = <T extends object = any>(
const transformFunction = get(dataFormatMap, key);

const transform = () => {
const tempKey =
typeof transformFunction === 'function'
? transformFunction?.(itemValue, entityKey, tempValues)
: transformForArray(transformFunction, itemValue);
let tempKey,
transformedResult,
isTransformedResultPrimitive = false;

/**
* 先判断是否是方法,是的话执行后拿到值,如果是基本类型,则认为是直接 transform 为新的值,
* 如果返回是 Object 则认为是 transform 为新的 {newKey: newValue}
*/
if (typeof transformFunction === 'function') {
transformedResult = transformFunction?.(itemValue, entityKey, tempValues);
const typeOfResult = typeof transformedResult;
if (typeOfResult !== 'object' && typeOfResult !== 'undefined') {
tempKey = entityKey;
isTransformedResultPrimitive = true;
} else {
tempKey = transformedResult;
}
} else {
tempKey = transformForArray(transformFunction, itemValue);
}

// { [key:string]:any } 数组也能通过编译
if (Array.isArray(tempKey)) {
result = namePathSet(result, tempKey, itemValue);
Expand All @@ -152,7 +169,7 @@ export const transformKeySubmitValue = <T extends object = any>(
} else if (typeof tempKey === 'object' && Array.isArray(finalValues)) {
result = { ...result, ...tempKey };
} else if (tempKey !== null || tempKey !== undefined) {
result = namePathSet(result, [tempKey], itemValue);
result = namePathSet(result, [tempKey], isTransformedResultPrimitive ? transformedResult : itemValue);
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/table/search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ describe('BasicTable Search', () => {
dataIndex: 'state',
initialValue: 'state',
search: {
transform: () => 'status',
transform: (value) => ({'status': value}),
},
},
{
Expand Down
12 changes: 6 additions & 6 deletions tests/utils/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,10 @@ describe('utils', () => {
dateRange2: ['2019-11-16 12:50:26', '2019-11-16 12:55:26'],
},
{
dataTime: () => 'new-dataTime',
time: () => 'new-time',
dataTime: (value) => ({'new-dataTime': value}),
time: (value) => ({'new-time': value}),
name: () => 'new-name',
money: () => 'new-money',
money: (value) => ({'new-money': value}),
// @ts-ignore
dateRange2: [
(itemValue, _, tempValues) => tempValues,
Expand All @@ -680,7 +680,7 @@ describe('utils', () => {
'new-dataTime',
'new-time',
'dateRange2',
'new-name',
'name',
'new-money',
'dateTimeRange',
'dateRange',
Expand All @@ -690,7 +690,7 @@ describe('utils', () => {
[
'dataTime',
'time',
'name',
'new-name',
'dateRange2',
'money',
'dateTimeRange',
Expand All @@ -699,7 +699,7 @@ describe('utils', () => {
);
expect((html as any)['new-dataTime']).toBe('2019-11-16 12:50:26');
expect((html as any)['new-time']).toBe('2019-11-16 12:50:26');
expect((html as any)['new-name']).toBe('qixian');
expect((html as any).name).toBe('new-name');
expect((html as any)['new-money']).toBe(20);
expect(html.dateTimeRange.join(',')).toBe(
'2019-11-16 12:50:26,2019-11-16 12:55:26',
Expand Down
Loading