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: sortByFunc 排序后行列维度节点丢失 #1606

Merged
merged 1 commit into from
Jul 25, 2022
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
30 changes: 28 additions & 2 deletions packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,33 @@ describe('Sort By Func Tests', () => {
expect(result).toEqual(originValues);
});

test('should return sortFunc result', () => {
test('should return merged result', () => {
const originValues = ['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州'];

const result = sortByFunc({
originValues,
sortParam: {
sortFieldId: 'city',
sortFunc: () => ['浙江[&]杭州'],
},
dataSet: {
fields: {
rows: ['province', 'city'],
},
} as unknown as PivotDataSet,
});

// sortFunc 返回的值在前,未返回的值在后
expect(result).toEqual(['浙江[&]杭州', '四川[&]成都', '四川[&]绵阳']);
});

test('should return merged result when sorting by ASC', () => {
const originValues = ['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州'];

const result = sortByFunc({
originValues,
sortParam: {
sortMethod: 'ASC',
sortFieldId: 'city',
sortFunc: () => ['浙江[&]杭州'],
},
Expand All @@ -207,7 +231,9 @@ describe('Sort By Func Tests', () => {
} as unknown as PivotDataSet,
});

expect(result).toEqual(['浙江[&]杭州']);
// asc 升序时
// sortFunc 没返回的值在前,返回的值在后
expect(result).toEqual(['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州']);
});

test('should return fallback result', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/s2-core/src/utils/sort-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const sortByCustom = (params: SortActionParams): string[] => {

export const sortByFunc = (params: SortActionParams): string[] => {
const { originValues, measureValues, sortParam, dataSet } = params;
const { sortFunc, sortFieldId } = sortParam;
const { sortFunc, sortFieldId, sortMethod } = sortParam;

const sortResult = sortFunc({
data: measureValues,
Expand All @@ -145,7 +145,9 @@ export const sortByFunc = (params: SortActionParams): string[] => {
originValues,
});
}
return sortResult;

// 用户返回的 sortResult 可能是不全的,需要用原始数据补全
return mergeDataWhenASC(sortResult, originValues, isAscSort(sortMethod));
};

export const sortByMethod = (params: SortActionParams): string[] => {
Expand Down