Skip to content

Commit

Permalink
fix(form): fix transform no support lableInValue (#3630)
Browse files Browse the repository at this point in the history
* fix(form): fix transform no support lableInValue

* fix test
  • Loading branch information
chenshuai2144 authored Sep 9, 2021
1 parent 6dfe99e commit de76c5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/utils/src/hooks/useFetchData/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ function useFetchData<T, U extends Record<string, any> = Record<string, any>>(pr
return [proFieldKeyRef.current, JSON.stringify(props.params)];
}, [props.params]);

const { data } = useSWR(key, fetchData, {
const { data, error } = useSWR(key, fetchData, {
revalidateOnFocus: false,
shouldRetryOnError: false,
revalidateOnReconnect: false,
});

return [
data as T,
(data as T) || error,
() => {
mutate(key);
},
Expand Down
46 changes: 28 additions & 18 deletions packages/utils/src/transformKeySubmitValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,36 @@ const transformKeySubmitValue = <T = any>(
if (tempValues == null || tempValues === undefined) {
return result;
}

Object.keys(tempValues).forEach((entityKey) => {
const key = parentsKey ? [parentsKey, entityKey].flat(1) : [entityKey].flat(1);
const itemValue = tempValues[entityKey];
const transformFunction = get(dataFormatMap, key);
const transform = () => {
const tempKey =
typeof transformFunction === 'function'
? transformFunction?.(itemValue, entityKey, tempValues)
: entityKey;
// { [key:string]:any } 数组也能通过编译
if (Array.isArray(tempKey)) {
result = namePathSet(result, tempKey, itemValue);
return;
}
if (typeof tempKey === 'object') {
finalValues = {
...finalValues,
...tempKey,
};
} else if (tempKey) {
result = namePathSet(result, [tempKey], itemValue);
}
};

/** 如果存在转化器提前渲染一下 */
if (transformFunction && typeof transformFunction === 'function') {
transform();
}

if (
typeof itemValue === 'object' &&
!Array.isArray(itemValue) &&
Expand All @@ -54,24 +81,7 @@ const transformKeySubmitValue = <T = any>(
result = namePathSet(result, [entityKey], genValues);
return;
}
const transformFunction = get(dataFormatMap, key);
const tempKey =
typeof transformFunction === 'function'
? transformFunction?.(itemValue, entityKey, tempValues)
: entityKey;
// { [key:string]:any } 数组也能通过编译
if (Array.isArray(tempKey)) {
result = namePathSet(result, tempKey, itemValue);
return;
}
if (typeof tempKey === 'object') {
finalValues = {
...finalValues,
...tempKey,
};
} else if (tempKey) {
result = namePathSet(result, [tempKey], itemValue);
}
transform();
});
// namePath、transform在omit为false时需正常返回 https://github.com/ant-design/pro-components/issues/2901#issue-908097115
return omit ? result : tempValues;
Expand Down

0 comments on commit de76c5f

Please sign in to comment.