Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: handle comma values in list dropdowns (#6986)

wpxp1234561a357ff458dc
diff --git a/packages/sheets-data-validation-ui/src/services/__tests__/dropdown-manager.service.spec.ts b/packages/sheets-data-validation-ui/src/services/__tests__/dropdown-manager.service.spec.ts
index 50385a9322..fb02d2485d 100644
--- a/packages/sheets-data-validation-ui/src/services/__tests__/dropdown-manager.service.spec.ts
+++ b/packages/sheets-data-validation-ui/src/services/__tests__/dropdown-manager.service.spec.ts
@@ -19,7 +19,7 @@ import type { SheetDataValidationModel } from '@univerjs/sheets-data-validation'
import type { IDropdownParam, IEditorBridgeService } from '@univerjs/sheets-ui';
import { DataValidationRenderMode } from '@univerjs/core';
import { DataValidatorDropdownType } from '@univerjs/data-validation';
-import { serializeListOptions, SetRangeValuesCommand } from '@univerjs/sheets';
+import { SetRangeValuesCommand } from '@univerjs/sheets';
import { SetCellEditVisibleOperation } from '@univerjs/sheets-ui';
import { Subject } from 'rxjs';
import { describe, expect, it, vi } from 'vitest';
@@ -154,7 +154,7 @@ describe('DataValidationDropdownManagerService', () => {
endRow: 1,
},
value: {
- v: serializeListOptions(['Closed']),
+ v: 'Closed',
p: null,
f: null,
si: null,
@@ -189,4 +189,87 @@ describe('DataValidationDropdownManagerService', () => {
service.hideDropdown();
expect(onHide).toHaveBeenCalledTimes(2);
});
+
+ it('serializes multiple list selections when saving to the cell', async () => {
+ let dropdownParam: IDropdownParam | undefined;
+ const worksheet = {
+ getSheetId: () => 'sheet-1',
+ getCellRaw: () => ({ v: '' }),
+ };
+ const workbook = {
+ getUnitId: () => 'book-1',
+ getSheetBySheetId: () => worksheet,
+ };
+ const rule = {
+ uid: 'rule-1',
+ type: 'list',
+ renderMode: DataValidationRenderMode.CUSTOM,
+ };
+ const commandService = {
+ executeCommand: vi.fn(() => Promise.resolve(true)),
+ };
+
+ const service = new DataValidationDropdownManagerService(
+ {
+ getUnit: () => workbook,
+ } as never,
+ {
+ getValidatorItem: vi.fn(() => ({
+ dropdownType: DataValidatorDropdownType.MULTIPLE_LIST,
+ getListWithColor: vi.fn(() => [
+ { label: '1,2', color: '#0f0' },
+ { label: '3', color: '#f00' },
+ ]),
+ })),
+ } as never,
+ {
+ visible$: new Subject<boolean>(),
+ } as never,
+ {
+ getRuleByLocation: vi.fn(() => rule),
+ } as never,
+ {
+ selectionMoveEnd$: new Subject<ISelectionWithStyle[]>(),
+ } as unknown as SheetsSelectionsService,
+ {
+ showDropdown: vi.fn((param: IDropdownParam) => {
+ dropdownParam = param;
+
+ return {
+ dispose: vi.fn(),
+ };
+ }),
+ } as never,
+ {
+ getRuleByLocation: vi.fn(() => rule),
+ } as unknown as SheetDataValidationModel,
+ commandService as never,
+ {
+ isVisible: () => ({ visible: false }),
+ } as IEditorBridgeService,
+ {
+ has: () => false,
+ } as never,
+ {
+ getConfig: vi.fn(() => undefined),
+ } as never
+ );
+
+ service.showDataValidationDropdown('book-1', 'sheet-1', 1, 2);
+
+ const listProps = dropdownParam?.props as {
+ onChange?: (value: string[]) => Promise<boolean>;
+ } | undefined;
+
+ await expect(listProps?.onChange?.(['1,2', '3'])).resolves.toBe(false);
+
+ expect(commandService.executeCommand).toHaveBeenCalledWith(SetRangeValuesCommand.id, expect.objectContaining({
+ value: {
+ v: '["1,2","3"]',
+ p: null,
+ f: null,
+ si: null,
+ },
+ }));
+ });
});
diff --git a/packages/sheets-data-validation-ui/src/services/dropdown-manager.service.ts b/packages/sheets-data-validation-ui/src/services/dropdown-manager.service.ts
index 150782c962..52507750f8 100644
--- a/packages/sheets-data-validation-ui/src/services/dropdown-manager.service.ts
+++ b/packages/sheets-data-validation-ui/src/services/dropdown-manager.service.ts
@@ -302,7 +302,7 @@ export class DataValidationDropdownManagerService extends Disposable {
case DataValidatorDropdownType.MULTIPLE_LIST: {
const multiple = validator.dropdownType === DataValidatorDropdownType.MULTIPLE_LIST;
const handleSave = async (newValue: string[]) => {
- const str = serializeListOptions(newValue);
+ const str = multiple ? serializeListOptions(newValue) : (newValue[0] ?? '');
const params: ISetRangeValuesCommandParams = {
unitId,
subUnitId,
diff --git a/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx b/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
index 20fd65679e..29183eaa48 100644
--- a/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
+++ b/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
@@ -378,7 +378,7 @@ export function ListFormulaInput(props: IFormulaInputProps) {
)
: (
<FormLayout error={formula1Res}>
- <div className="-univer-mt-3">
+ <div>
<DraggableList
list={strList}
onListChange={setStrList}
diff --git a/packages/sheets-data-validation/src/validators/__tests__/validators.spec.ts b/packages/sheets-data-validation/src/validators/__tests__/validators.spec.ts
index 79b39a9273..198fb79e68 100644
--- a/packages/sheets-data-validation/src/validators/__tests__/validators.spec.ts
+++ b/packages/sheets-data-validation/src/validators/__tests__/validators.spec.ts
@@ -15,7 +15,7 @@
*/
import type { Injector, LocaleService } from '@univerjs/core';
-import { CellValueType, DataValidationOperator, WrapStrategy } from '@univerjs/core';
+import { CellValueType, DataValidationOperator, DataValidationType, WrapStrategy } from '@univerjs/core';
import { LexerTreeBuilder } from '@univerjs/engine-formula';
import { describe, expect, it, vi } from 'vitest';
import { DataValidationCustomFormulaService } from '../../services/dv-custom-formula.service';
@@ -27,6 +27,7 @@ import { CustomFormulaValidator } from '../custom-validator';
import { DateValidator } from '../date-validator';
import { DecimalValidator, getCellValueNumber } from '../decimal-validator';
import { ListMultipleValidator } from '../list-multiple-validator';
+import { ListValidator } from '../list-validator';
import { TextLengthValidator } from '../text-length-validator';
import { WholeValidator } from '../whole-validator';
@@ -237,4 +238,15 @@ describe('validators', () => {
expect(listMultiple.skipDefaultFontRender!()).toBe(true);
expect(formulaService.getRuleFormulaResult).toHaveBeenCalled();
});
+
+ it('treats comma-containing single list values as one selected value', async () => {
+ const { localeService, injector } = createContext();
+ const list = new ListValidator(localeService, injector);
+
+ await expect(list.isValidType(
+ { value: '1,2', unitId: 'u', subUnitId: 's' } as never,
+ {} as never,
+ { type: DataValidationType.LIST, formula1: '["1,2","3"]' } as never
+ )).resolves.toBe(true);
+ });
});
diff --git a/packages/sheets-data-validation/src/validators/list-validator.ts b/packages/sheets-data-validation/src/validators/list-validator.ts
index 266259a538..bcfc99ce68 100644
--- a/packages/sheets-data-validation/src/validators/list-validator.ts
+++ b/packages/sheets-data-validation/src/validators/list-validator.ts
@@ -177,7 +177,9 @@ export class ListValidator extends BaseSheetValidator {
rule
).list
: deserializeListOptions(formula1);
- const selected = this.parseCellValue(value!);
+ const selected = rule.type === DataValidationType.LIST
+ ? (value === undefined || value === null || value === '' ? [] : [`${value}`])
+ : this.parseCellValue(value!);
return selected.every((i) => formula1Result.includes(i));
}
diff --git a/packages/sheets-ui/src/views/dropdown/list-dropdown/__tests__/utils.spec.ts b/packages/sheets-ui/src/views/dropdown/list-dropdown/__tests__/utils.spec.ts
new file mode 100644
index 0000000000..bc0241107d
--- /dev/null
+++ b/packages/sheets-ui/src/views/dropdown/list-dropdown/__tests__/utils.spec.ts
@@ -0,0 +1,28 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+import { getListDropdownValue } from '../utils';
+
+describe('list dropdown utils', () => {
+ it('keeps comma-containing single list values selected as one option', () => {
+ expect(getListDropdownValue('1,2', false)).toEqual(['1,2']);
+ });
+
+ it('deserializes multiple list values', () => {
+ expect(getListDropdownValue('["1,2","3"]', true)).toEqual(['1,2', '3']);
+ });
+});
diff --git a/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx b/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
index bd1ac4a15d..3c74a14ee8 100644
--- a/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
+++ b/packages/sheets-ui/src/views/dropdown/list-dropdown/index.tsx
@@ -25,7 +25,6 @@ import {
import { borderClassName, borderTopClassName, clsx, scrollbarClassName } from '@univerjs/design';
import { CheckMarkIcon } from '@univerjs/icons';
import {
- deserializeListOptions,
RangeProtectionPermissionEditPoint,
serializeListOptions,
SheetPermissionCheckController,
@@ -34,6 +33,7 @@ import {
} from '@univerjs/sheets';
import { useDependency } from '@univerjs/ui';
import { useEffect, useMemo, useRef, useState } from 'react';
+import { getListDropdownValue } from './utils';
interface ISelectListProps {
value: string[];
@@ -234,10 +234,10 @@ export function ListDropDown(props: { popup: IPopup<IListDropdownProps & IBaseDr
<SelectList
style={{ minWidth: cellWidth, maxWidth: Math.max(cellWidth, 200) }}
title={multiple ? localeService.t('sheets-ui.data-validation.listMultiple.dropdown') : localeService.t('sheets-ui.data-validation.list.dropdown')}
- value={deserializeListOptions(localValue ?? '')}
+ value={getListDropdownValue(localValue, multiple)}
multiple={multiple}
onChange={async (newValue) => {
- const str = serializeListOptions(newValue);
+ const str = multiple ? serializeListOptions(newValue) : (newValue[0] ?? '');
setLocalValue(str);
const success = await onChange?.(newValue);
if (!(success === false)) {
diff --git a/packages/sheets-ui/src/views/dropdown/list-dropdown/utils.ts b/packages/sheets-ui/src/views/dropdown/list-dropdown/utils.ts
new file mode 100644
index 0000000000..bb2e77f5e9
--- /dev/null
+++ b/packages/sheets-ui/src/views/dropdown/list-dropdown/utils.ts
@@ -0,0 +1,25 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { deserializeListOptions } from '@univerjs/sheets';
+
+export function getListDropdownValue(value: string | undefined, multiple?: boolean) {
+ if (!value) {
+ return [];
+ }
+
+ return multiple ? deserializeListOptions(value) : [value];
+}