Skip to content

Commit

Permalink
fix: sortByFunc 排序后行列维度节点丢失 (#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcx-seima authored Jul 25, 2022
1 parent 61a81fb commit 47f8d35
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit 47f8d35

Please sign in to comment.