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(core): 在排序中不将空字符串视为 undefined #2249

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
125 changes: 114 additions & 11 deletions packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { getContainer } from 'tests/util/helpers';
import { sortData } from 'tests/data/sort-advanced';
import {
getSortByMeasureValues,
sortAction,
sortByCustom,
sortByFunc,
} from '@/utils/sort-action';
import { getContainer } from 'tests/util/helpers';
import {
EXTRA_FIELD,
type S2Options,
type SortParam,
TOTAL_VALUE,
type S2DataConfig,
VALUE_FIELD,
type S2DataConfig,
type S2Options,
type SortParam,
} from '@/common';
import { PivotDataSet, type SortActionParams } from '@/data-set';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
import { BaseDataSet, PivotDataSet, type SortActionParams } from '@/data-set';
import {
getSortByMeasureValues,
sortAction,
sortByCustom,
sortByFunc,
} from '@/utils/sort-action';

describe('Sort Action Test', () => {
describe('Sort Action', () => {
Expand Down Expand Up @@ -714,4 +714,107 @@ describe('GetSortByMeasureValues Total Fallback Tests', () => {
},
]);
});

test('should consider empty string in sort', () => {
// 行小计进行 组内排序
const sortParam: SortParam = {
sortFieldId: 'city',
sortBy: ['', '舟山', '杭州'],
};

sortData.data.unshift({
province: '浙江',
city: '',
type: '笔',
price: '2',
});
dataSet.setDataCfg(sortData);

const params: SortActionParams = {
dataSet,
sortParam,
originValues: [
'浙江[&]',
'浙江[&]杭州',
'浙江[&]舟山',
'吉林[&]长春',
'吉林[&]白山',
],
};
const measureValues = getSortByMeasureValues(params);
expect(measureValues).toEqual([
{
$$extra$$: 'price',
$$value$$: '2',
city: '',
price: '2',
province: '浙江',
type: '笔',
},
{
$$extra$$: 'price',
$$value$$: '1',
city: '杭州',
price: '1',
province: '浙江',
type: '笔',
},
{
$$extra$$: 'price',
$$value$$: '2',
city: '杭州',
price: '2',
province: '浙江',
type: '纸张',
},
{
$$extra$$: 'price',
$$value$$: '17',
city: '舟山',
price: '17',
province: '浙江',
type: '笔',
},
{
$$extra$$: 'price',
$$value$$: '25.5',
city: '舟山',
price: '25.5',
province: '浙江',
type: '纸张',
},
{
$$extra$$: 'price',
$$value$$: '10',
city: '长春',
price: '10',
province: '吉林',
type: '笔',
},
{
$$extra$$: 'price',
$$value$$: '3',
city: '长春',
price: '3',
province: '吉林',
type: '纸张',
},
{
$$extra$$: 'price',
$$value$$: '9',
city: '白山',
price: '9',
province: '吉林',
type: '笔',
},
{
$$extra$$: 'price',
$$value$$: '11',
city: '白山',
price: '11',
province: '吉林',
type: '纸张',
},
]);
});
});
10 changes: 7 additions & 3 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import {
forEach,
get,
intersection,
isNil,
isUndefined,
last,
reduce,
set,
} from 'lodash';
import { i18n } from '../../common/i18n';
import { EXTRA_FIELD, ID_SEPARATOR, ROOT_ID } from '../../common/constant';
import type { Meta } from '../../common/interface/basic';
import type {
DataPathParams,
DataType,
PivotMeta,
SortedDimensionValues,
} from '../../data-set/interface';
import type { Meta } from '../../common/interface/basic';

interface Param {
rows: string[];
Expand Down Expand Up @@ -68,7 +68,11 @@ export function transformDimensionsValues(
export function getDimensionsWithoutPathPre(dimensions: string[]) {
return dimensions.map((item) => {
const splitArr = item?.split(ID_SEPARATOR);
return splitArr[splitArr?.length - 1] || item;
const dimensionsWithoutPathPre = splitArr[splitArr?.length - 1];
if (isNil(dimensionsWithoutPathPre)) {
return item;
}
return dimensionsWithoutPathPre;
});
}

Expand Down
11 changes: 8 additions & 3 deletions packages/s2-core/src/utils/sort-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { EXTRA_FIELD, ID_SEPARATOR, TOTAL_VALUE } from '../common/constant';
import type { Fields, SortMethod, SortParam } from '../common/interface';
import type { PivotDataSet } from '../data-set';
import type { DataType, SortActionParams } from '../data-set/interface';
import { getLeafColumnsWithKey } from '../facet/utils';
import { getListBySorted, sortByItems } from '../utils/data-set-operate';
import { getDimensionsWithParentPath } from '../utils/dataset/pivot-data-set';
import { getLeafColumnsWithKey } from '../facet/utils';

export const isAscSort = (sortMethod) => toUpper(sortMethod) === 'ASC';

Expand Down Expand Up @@ -86,8 +86,13 @@ export const sortByCustom = (params: SortActionParams): string[] => {
const { sortByValues, originValues } = params;

// 从 originValues 中过滤出所有包含 sortByValue 的 id
const idWithPre = originValues.filter((originItem) =>
sortByValues.find((value) => endsWith(originItem, value)),
const idWithPre = originValues.filter(
(originItem) =>
!isNil(
sortByValues.find((value) =>
endsWith(originItem, isNil(value) ? value : value || '[&]'),
),
),
);
// 将 id 拆分为父节点和目标节点
const idListWithPre = idWithPre.map((idStr) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/s2-react/playground/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isUpDataValue, type Columns } from '@antv/s2';
import type { S2DataConfig } from '@antv/s2';
import { isUpDataValue, type Columns } from '@antv/s2';
import { getBaseSheetComponentOptions } from '@antv/s2-shared';
import type { SliderSingleProps } from 'antd';
import {
data,
totalData,
meta,
fields,
meta,
totalData,
} from '../__tests__/data/mock-dataset.json';
import type { SheetComponentOptions } from '../src/components';

Expand Down Expand Up @@ -39,11 +39,20 @@ export const tableSheetDataCfg: S2DataConfig = {
},
};

data.unshift({
number: 7799,
province: '浙江省',
city: '',
type: '家具',
sub_type: '桌子',
});

export const pivotSheetDataCfg: S2DataConfig = {
data,
totalData,
meta,
fields,
sortParams: [{ sortFieldId: 'city', sortBy: ['', '舟山市', '宁波市'] }],
};

export const s2Options: SheetComponentOptions = {
Expand Down
Loading