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(data-validation): preserve commas in custom list options (#6982)

wpxp123456c6b36771c7e6
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 adfb07fa47..20fd65679e 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
@@ -21,7 +21,7 @@ import { awaitTime, DataValidationType, generateRandomId, isFormulaString, Local
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
import { borderClassName, clsx, DraggableList, Dropdown, FormLayout, Input, Radio, RadioGroup } from '@univerjs/design';
import { DeleteIcon, IncreaseIcon, MoreDownIcon, SequenceIcon } from '@univerjs/icons';
-import { deserializeListOptions, serializeListOptions } from '@univerjs/sheets';
+import { deserializeListOptions } from '@univerjs/sheets';
import { DataValidationFormulaController } from '@univerjs/sheets-data-validation';
import { FormulaEditor } from '@univerjs/sheets-formula-ui';
import { useDependency, useEvent, useObservable, useSidebarClick } from '@univerjs/ui';
@@ -29,6 +29,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { debounceTime } from 'rxjs';
import { DROP_DOWN_DEFAULT_COLOR } from '../../../const';
import { DataValidationPanelService } from '../../../services/data-validation-panel.service';
+import { buildCustomListFormulaPayload } from './utils';
const DEFAULT_COLOR_PRESET = [
'#FFFFFF',
@@ -163,8 +164,10 @@ const Template = (props: { item: IDropdownItem; commonProps: any; className?: st
);
};
+const NOOP = () => { /* empty */ };
+
export function ListFormulaInput(props: IFormulaInputProps) {
- const { value, onChange: _onChange = () => { /* empty */ }, unitId, subUnitId, validResult, showError, ruleId } = props;
+ const { value, onChange: _onChange = NOOP, unitId, subUnitId, validResult, showError, ruleId } = props;
const { formula1 = '', formula2 = '' } = value || {};
const [isFormulaStr, setIsFormulaStr] = useState(() => isFormulaString(formula1) ? '1' : '0');
const [formulaStr, setFormulaStr] = useState(isFormulaStr === '1' ? formula1 : '=');
@@ -235,14 +238,12 @@ export function ListFormulaInput(props: IFormulaInputProps) {
}
};
- const colorList = formula2.split(',');
-
const refFinalList: IDropdownItem[] = useMemo(() => refOptions.map((label, i) => ({
label,
- color: colorList[i] || DROP_DOWN_DEFAULT_COLOR,
+ color: refColors[i] || DROP_DOWN_DEFAULT_COLOR,
id: `${i}`,
isRef: true,
- })), [colorList, refOptions]);
+ })), [refColors, refOptions]);
const handleRefItemChange = (id: string, value: string, color: string) => {
const newColors = [...refColors];
@@ -271,31 +272,8 @@ export function ListFormulaInput(props: IFormulaInputProps) {
if (isFormulaStr === '1') {
return;
}
- const labelSet = new Set<string>();
- const finalList: { color: string; label: string }[] = [];
- strList.map((item) => {
- const labelList = item.label.split(',');
- return {
- labelList,
- item,
- };
- }).forEach(({ item, labelList }) => {
- labelList.forEach((labelItem) => {
- if (!labelSet.has(labelItem)) {
- labelSet.add(labelItem);
- finalList.push({
- label: labelItem,
- color: item.color,
- });
- }
- });
- });
-
- onChange({
- formula1: serializeListOptions(finalList.map((item) => item.label)),
- formula2: finalList.map((item) => item.color === DROP_DOWN_DEFAULT_COLOR ? '' : item.color).join(','),
- });
- }, [strList, onChange, isFormulaStr, formulaStrCopy, refColors]);
+ onChange(buildCustomListFormulaPayload(strList, DROP_DOWN_DEFAULT_COLOR));
+ }, [strList, onChange, isFormulaStr]);
const updateFormula = useEvent(async (str: string) => {
if (!isFormulaString(str)) {
diff --git a/packages/sheets-data-validation-ui/src/views/components/formula-input/__tests__/list-formula-input.spec.ts b/packages/sheets-data-validation-ui/src/views/components/formula-input/__tests__/list-formula-input.spec.ts
new file mode 100644
index 0000000000..eec97ad1e2
--- /dev/null
+++ b/packages/sheets-data-validation-ui/src/views/components/formula-input/__tests__/list-formula-input.spec.ts
@@ -0,0 +1,31 @@
+/**
+ * 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';
+import { describe, expect, it } from 'vitest';
+import { buildCustomListFormulaPayload } from '../utils';
+
+describe('ListFormulaInput', () => {
+ it('keeps commas inside custom list values', () => {
+ const payload = buildCustomListFormulaPayload([
+ { label: 'a,b', color: '#ff0000' },
+ { label: 'c', color: '#ffffff' },
+ ], '#ffffff');
+
+ expect(deserializeListOptions(payload.formula1)).toEqual(['a,b', 'c']);
+ expect(payload.formula2).toBe('#ff0000,');
+ });
+});
diff --git a/packages/sheets-data-validation-ui/src/views/components/formula-input/utils.ts b/packages/sheets-data-validation-ui/src/views/components/formula-input/utils.ts
new file mode 100644
index 0000000000..fa626c873f
--- /dev/null
+++ b/packages/sheets-data-validation-ui/src/views/components/formula-input/utils.ts
@@ -0,0 +1,41 @@
+/**
+ * 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 { serializeListOptions } from '@univerjs/sheets';
+
+interface ICustomListItem {
+ color: string;
+ label: string;
+}
+
+export function buildCustomListFormulaPayload(items: ICustomListItem[], defaultColor: string) {
+ const labelSet = new Set<string>();
+ const finalList: ICustomListItem[] = [];
+
+ items.forEach((item) => {
+ if (!item.label || labelSet.has(item.label)) {
+ return;
+ }
+
+ labelSet.add(item.label);
+ finalList.push(item);
+ });
+
+ return {
+ formula1: serializeListOptions(finalList.map((item) => item.label)),
+ formula2: finalList.map((item) => item.color === defaultColor ? '' : item.color).join(','),
+ };
+}