From d450003cae62fc4b9061f3d1814fb14750e791a8 Mon Sep 17 00:00:00 2001 From: tyr1dev Date: Sun, 11 Jun 2023 00:20:00 +0800 Subject: [PATCH 1/3] fix(core): not consider empty string as undefined --- packages/s2-core/src/utils/dataset/pivot-data-set.ts | 10 +++++++--- packages/s2-core/src/utils/sort-action.ts | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/s2-core/src/utils/dataset/pivot-data-set.ts b/packages/s2-core/src/utils/dataset/pivot-data-set.ts index 85b3f7570f..7b7fc46779 100644 --- a/packages/s2-core/src/utils/dataset/pivot-data-set.ts +++ b/packages/s2-core/src/utils/dataset/pivot-data-set.ts @@ -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[]; @@ -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; }); } diff --git a/packages/s2-core/src/utils/sort-action.ts b/packages/s2-core/src/utils/sort-action.ts index 921df04fb0..12a5f2f32d 100644 --- a/packages/s2-core/src/utils/sort-action.ts +++ b/packages/s2-core/src/utils/sort-action.ts @@ -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'; @@ -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) => { From 3314736a242d4a2ccf67982c83c9762b65d09372 Mon Sep 17 00:00:00 2001 From: tyr1dev Date: Sun, 11 Jun 2023 18:45:32 +0800 Subject: [PATCH 2/3] chore: react playground update --- packages/s2-react/playground/config.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/s2-react/playground/config.ts b/packages/s2-react/playground/config.ts index 0a2117cee5..37a24517c9 100644 --- a/packages/s2-react/playground/config.ts +++ b/packages/s2-react/playground/config.ts @@ -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'; @@ -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 = { From cc75efe325dc397916ff7b445ae3c3b622cd40b2 Mon Sep 17 00:00:00 2001 From: tyr1dev Date: Sun, 11 Jun 2023 19:10:59 +0800 Subject: [PATCH 3/3] test: consider empty string in sort action --- .../__tests__/unit/utils/sort-action-spec.tsx | 125 ++++++++++++++++-- 1 file changed, 114 insertions(+), 11 deletions(-) diff --git a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx index 5b90bdb267..ffc5d5a62f 100644 --- a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx +++ b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx @@ -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', () => { @@ -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: '纸张', + }, + ]); + }); });