Skip to content

Commit

Permalink
Merge c6d86f7 into 481d7a2
Browse files Browse the repository at this point in the history
  • Loading branch information
xliez committed Aug 8, 2023
2 parents 481d7a2 + c6d86f7 commit c4fc393
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
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

0 comments on commit c4fc393

Please sign in to comment.