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.

feat(conditional-formatting): add error interceptor (#2600)

Gpound.liub523f6246425
diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx
index 7535b04f6d..ab2b252c9c 100644
--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx
+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx
@@ -24,6 +24,7 @@ import type { IAverageHighlightCell, IConditionalFormattingRuleConfig, IHighligh
import { ConditionalStyleEditor } from '../../conditional-style-editor';
import stylesBase from '../index.module.less';
import { Preview } from '../../preview';
+import { WrapperError } from '../../wrapper-error/WrapperError';
import styles from './index.module.less';
import type { IStyleEditorProps } from './type';
@@ -43,15 +44,18 @@ export const FormulaStyleEditor = (props: IStyleEditorProps) => {
}
return '=';
});
+ const [formulaError, formulaErrorSet] = useState('');
const getResult = (config: {
style: IHighlightCell['style'];
formula: string;
}) => {
- return { style: config.style,
- value: formula,
- type: CFRuleType.highlightCell,
- subType: CFSubRuleType.formula };
+ return {
+ style: config.style,
+ value: formula,
+ type: CFRuleType.highlightCell,
+ subType: CFSubRuleType.formula,
+ };
};
useEffect(() => {
const dispose = interceptorManager.intercept(interceptorManager.getInterceptPoints().submit, {
@@ -62,6 +66,18 @@ export const FormulaStyleEditor = (props: IStyleEditorProps) => {
return dispose as () => void;
}, [style, formula, interceptorManager]);
+ useEffect(() => {
+ interceptorManager.intercept(interceptorManager.getInterceptPoints().beforeSubmit, {
+ handler: (v, _c, next) => {
+ if (!formula || formula.length === 1 || !formulaError.startsWith('=')) {
+ formulaErrorSet(localeService.t('sheet.cf.errorMessage.formulaError'));
+ return false;
+ }
+ return next(v);
+ },
+ });
+ }, [formula]);
+
const _onChange = (config: {
formula: string;
style: IHighlightCell['style'];
@@ -72,19 +88,23 @@ export const FormulaStyleEditor = (props: IStyleEditorProps) => {
<div>
<div className={`${stylesBase.title} ${stylesBase.mTBase}`}>{localeService.t('sheet.cf.panel.styleRule')}</div>
<div className={`${stylesBase.mTSm}`}>
- <TextEditor
- id={createInternalEditorID(`${SHEET_CONDITIONAL_FORMATTING_PLUGIN}_formula`)}
- openForSheetSubUnitId={worksheet?.getSheetId()}
- openForSheetUnitId={workbook.getUnitId()}
- value={formula}
- canvasStyle={{ fontSize: 10 }}
- onlyInputFormula={true}
- onChange={(v = '') => {
- const formula = v || '';
- formulaSet(formula);
- _onChange({ style, formula });
- }}
- />
+ <WrapperError errorText={formulaError}>
+ <TextEditor
+ id={createInternalEditorID(`${SHEET_CONDITIONAL_FORMATTING_PLUGIN}_formula`)}
+ openForSheetSubUnitId={worksheet?.getSheetId()}
+ openForSheetUnitId={workbook.getUnitId()}
+ value={formula}
+ canvasStyle={{ fontSize: 10 }}
+ onlyInputFormula={true}
+ onChange={(v = '') => {
+ const formula = v || '';
+ formulaSet(formula);
+ _onChange({ style, formula });
+ formulaErrorSet('');
+ }}
+ />
+ </WrapperError>
+
</div>
<div className={`${styles.cfPreviewWrap} `}>
diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/highlightCell.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/highlightCell.tsx
index 2ef6e73b50..caf47f7180 100644
--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/highlightCell.tsx
+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/highlightCell.tsx
@@ -18,7 +18,8 @@ import React, { useEffect, useMemo, useState } from 'react';
import { Input, InputNumber, Select } from '@univerjs/design';
import { useDependency } from '@wendellhu/redi/react-bindings';
import { LocaleService } from '@univerjs/core';
-import { CFNumberOperator,
+import {
+ CFNumberOperator,
CFRuleType,
CFSubRuleType,
CFTextOperator,
@@ -35,29 +36,39 @@ import type {
import { ConditionalStyleEditor } from '../../conditional-style-editor';
import { Preview } from '../../preview';
import stylesBase from '../index.module.less';
+import { WrapperError } from '../../wrapper-error/WrapperError';
import type { IStyleEditorProps } from './type';
-
import styles from './index.module.less';
const createOptionItem = (text: string, localeService: LocaleService) => ({ label: localeService.t(`sheet.cf.operator.${text}`), value: text });
type IValue = number | string | [number, number];
-type IResult = (Pick< ITextHighlightCell | INumberHighlightCell | ITimePeriodHighlightCell, 'operator' | 'subType'>) & { value?: IValue };
+type IResult = (Pick<ITextHighlightCell | INumberHighlightCell | ITimePeriodHighlightCell, 'operator' | 'subType'>) & { value?: IValue };
+
+const HighlightCellInput = (props: {
+ type: IResult['subType'] | CFSubRuleType.duplicateValues | CFSubRuleType.uniqueValues;
+ operator?: IResult['operator'];
+ interceptorManager: IStyleEditorProps['interceptorManager'];
+ rule?: IHighlightCell;
+ value: IValue;
+ onChange: (value: IValue) => void;
+}) => {
+ const { type, operator, onChange, value, interceptorManager } = props;
-const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.duplicateValues | CFSubRuleType.uniqueValues;
- operator?: IResult['operator'];
- interceptorManager: IStyleEditorProps['interceptorManager'];
- rule?: IHighlightCell;
- value: IValue;
- onChange: (value: IValue) => void; }) => {
- const { type, operator, onChange, value } = props;
+ const localeService = useDependency(LocaleService);
const [inputNumberValue, inputNumberValueSet] = useState(() => typeof value === 'number' ? value : 0);
+ const [numberError, numberErrorSet] = useState('');
+
const [inputTextValue, inputTextValueSet] = useState(() => typeof value === 'string' ? value : '');
+ const [textError, textErrorSet] = useState('');
const [inputNumberMin, inputNumberMinSet] = useState(() => Array.isArray(value) ? value[0] === undefined ? 0 : value[0] : 0);
+ const [numberMinError, numberMinErrorSet] = useState('');
+
const [inputNumberMax, inputNumberMaxSet] = useState(() => Array.isArray(value) ? value[1] === undefined ? 100 : value[1] : 100);
+ const [numberMaxError, numberMaxErrorSet] = useState('');
useEffect(() => {
switch (type) {
- case CFSubRuleType.text:{
+ case CFSubRuleType.text: {
if ([CFTextOperator.beginsWith, CFTextOperator.endsWith,
CFTextOperator.containsText, CFTextOperator.notContainsText,
CFTextOperator.equal, CFTextOperator.notEqual].includes(operator as CFTextOperator)) {
@@ -65,7 +76,7 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
}
break;
}
- case CFSubRuleType.number:{
+ case CFSubRuleType.number: {
if ([CFNumberOperator.equal, CFNumberOperator.notEqual,
CFNumberOperator.greaterThan, CFNumberOperator.greaterThanOrEqual,
CFNumberOperator.lessThan, CFNumberOperator.lessThanOrEqual,
@@ -79,9 +90,30 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
}
}
}, [type]);
+ useEffect(() => {
+ const dispose = interceptorManager.intercept(interceptorManager.getInterceptPoints().beforeSubmit, {
+ handler: (v, _c, next) => {
+ switch (type) {
+ case CFSubRuleType.text: {
+ if ([CFTextOperator.beginsWith, CFTextOperator.containsText, CFTextOperator.endsWith, CFTextOperator.notEqual, CFTextOperator.notContainsText, CFTextOperator.equal]) {
+ if (!inputTextValue) {
+ textErrorSet(localeService.t('sheet.cf.errorMessage.notBlank'));
+ return false;
+ }
+ return next(v);
+ }
+ }
+ }
+ return next(v);
+ },
+ });
+ return () => {
+ dispose();
+ };
+ }, [type, inputNumberValue, inputTextValue, inputNumberMin, inputNumberMax]);
switch (type) {
- case CFSubRuleType.text:{
+ case CFSubRuleType.text: {
if ([CFTextOperator.beginsWith, CFTextOperator.endsWith,
CFTextOperator.containsText, CFTextOperator.notContainsText,
CFTextOperator.equal, CFTextOperator.notEqual].includes(operator as CFTextOperator)) {
@@ -91,13 +123,22 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
};
return (
<div className={`${stylesBase.mTSm}`}>
- <Input className={styles.width100} value={inputTextValue} onChange={_onChange} />
+ <WrapperError errorText={textError}>
+ <Input
+ className={styles.width100}
+ value={inputTextValue}
+ onChange={(v) => {
+ textErrorSet('');
+ _onChange(v);
+ }}
+ />
+ </WrapperError>
</div>
);
}
break;
}
- case CFSubRuleType.number:{
+ case CFSubRuleType.number: {
if ([CFNumberOperator.equal, CFNumberOperator.notEqual,
CFNumberOperator.greaterThan, CFNumberOperator.greaterThanOrEqual,
CFNumberOperator.lessThan, CFNumberOperator.lessThanOrEqual,
@@ -105,10 +146,13 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
const _onChange = (value: number | null) => {
inputNumberValueSet(value || 0);
onChange(value || 0);
+ numberErrorSet('');
};
return (
<div className={`${stylesBase.mTSm}`}>
- <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} className={styles.width100} value={inputNumberValue} onChange={_onChange} />
+ <WrapperError errorText={numberError}>
+ <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} className={styles.width100} value={inputNumberValue} onChange={_onChange} />
+ </WrapperError>
</div>
);
}
@@ -117,16 +161,23 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
inputNumberMinSet(_value || 0);
const value: [number, number] = [_value || 0, inputNumberMax];
onChange(value);
+ numberMinErrorSet('');
};
const onChangeMax = (_value: number | null) => {
inputNumberMaxSet(_value || 0);
const value: [number, number] = [inputNumberMin, _value || 0];
onChange(value);
+ numberMaxErrorSet('');
};
return (
<div className={`${stylesBase.mTSm} ${stylesBase.labelContainer} `}>
- <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} value={inputNumberMin} onChange={onChangeMin} />
- <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} className={`${stylesBase.mLSm}`} value={inputNumberMax} onChange={onChangeMax} />
+ <WrapperError errorText={numberMinError}>
+ <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} value={inputNumberMin} onChange={onChangeMin} />
+ </WrapperError>
+ <WrapperError errorText={numberMaxError}>
+ <InputNumber min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} className={`${stylesBase.mLSm}`} value={inputNumberMax} onChange={onChangeMax} />
+ </WrapperError>
+
</div>
);
}
@@ -136,7 +187,7 @@ const HighlightCellInput = (props: { type: IResult['subType'] | CFSubRuleType.du
};
const getOperatorOptions = (type: CFSubRuleType.duplicateValues | CFSubRuleType.uniqueValues | CFSubRuleType.number | CFSubRuleType.text | CFSubRuleType.timePeriod | CFSubRuleType.formula, localeService: LocaleService) => {
switch (type) {
- case CFSubRuleType.text:{
+ case CFSubRuleType.text: {
return [
createOptionItem(CFTextOperator.containsText, localeService),
createOptionItem(CFTextOperator.notContainsText, localeService),
@@ -150,7 +201,7 @@ const getOperatorOptions = (type: CFSubRuleType.duplicateValues | CFSubRuleType.
createOptionItem(CFTextOperator.notContainsErrors, localeService),
];
}
- case CFSubRuleType.number:{
+ case CFSubRuleType.number: {
return [
createOptionItem(CFNumberOperator.between, localeService),
createOptionItem(CFNumberOperator.notBetween, localeService),
@@ -162,7 +213,7 @@ const getOperatorOptions = (type: CFSubRuleType.duplicateValues | CFSubRuleType.
createOptionItem(CFNumberOperator.lessThanOrEqual, localeService),
];
}
- case CFSubRuleType.timePeriod:{
+ case CFSubRuleType.timePeriod: {
return [
createOptionItem(CFTimePeriodOperator.yesterday, localeService),
createOptionItem(CFTimePeriodOperator.today, localeService),
@@ -183,7 +234,7 @@ export const HighlightCellStyleEditor = (props: IStyleEditorProps<any, ITextHigh
const localeService = useDependency(LocaleService);
const rule = props.rule?.type === CFRuleType.highlightCell ? props.rule : undefined;
- const [subType, subTypeSet] = useState<CFSubRuleType.number | CFSubRuleType.text | CFSubRuleType.timePeriod | CFSubRuleType.duplicateValues | CFSubRuleType.uniqueValues >(() => {
+ const [subType, subTypeSet] = useState<CFSubRuleType.number | CFSubRuleType.text | CFSubRuleType.timePeriod | CFSubRuleType.duplicateValues | CFSubRuleType.uniqueValues>(() => {
const defaultV = CFSubRuleType.text;
if (!rule) {
return defaultV;
@@ -228,9 +279,9 @@ export const HighlightCellStyleEditor = (props: IStyleEditorProps<any, ITextHigh
});
const [style, styleSet] = useState<IHighlightCell['style']>({});
- const getResult = useMemo(() => (option: { subType?: string;operator?: string;value?: IValue;style?: IHighlightCell['style'] }) => {
+ const getResult = useMemo(() => (option: { subType?: string; operator?: string; value?: IValue; style?: IHighlightCell['style'] }) => {
switch (option.subType || subType) {
- case CFSubRuleType.text:{
+ case CFSubRuleType.text: {
if ([CFTextOperator.beginsWith, CFTextOperator.endsWith,
CFTextOperator.containsText, CFTextOperator.notContainsText,
CFTextOperator.equal, CFTextOperator.notEqual].includes(operator as CFTextOperator)) {
@@ -244,7 +295,7 @@ export const HighlightCellStyleEditor = (props: IStyleEditorProps<any, ITextHigh
}
break;
}
- case CFSubRuleType.number:{
+ case CFSubRuleType.number: {
if ([CFNumberOperator.equal, CFNumberOperator.notEqual,
CFNumberOperator.greaterThan, CFNumberOperator.greaterThanOrEqual,
CFNumberOperator.lessThan, CFNumberOperator.lessThanOrEqual,
diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx
index 347adee5d3..d7d8676456 100644
--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx
+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx
@@ -182,7 +182,6 @@ export const RuleEdit = (props: IRuleEditProps) => {
};
const handleSubmit = () => {
- const beforeSubmitResult = interceptorManager.fetchThroughInterceptors(interceptorManager.getInterceptPoints().beforeSubmit)(true, null);
const getRanges = () => {
const worksheet = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
if (!worksheet) {
@@ -192,11 +191,11 @@ export const RuleEdit = (props: IRuleEditProps) => {
const result = ranges.filter((range) => !(Number.isNaN(range.startRow) || Number.isNaN(range.startColumn)));
return result;
};
-
+ const ranges = getRanges();
+ const beforeSubmitResult = interceptorManager.fetchThroughInterceptors(interceptorManager.getInterceptPoints().beforeSubmit)(true, null);
if (beforeSubmitResult) {
const result = interceptorManager.fetchThroughInterceptors(interceptorManager.getInterceptPoints().submit)(null, null);
- const ranges = getRanges();
- if (result && ranges.length) {
+ if (result) {
// When you switch the child table, you need to fetch it again here, instead of using the
const unitId = getUnitId(univerInstanceService);
const subUnitId = getSubUnitId(univerInstanceService);
diff --git a/packages/sheets-conditional-formatting-ui/src/components/wrapper-error/WrapperError.tsx b/packages/sheets-conditional-formatting-ui/src/components/wrapper-error/WrapperError.tsx
new file mode 100644
index 0000000000..ae849cff3a
--- /dev/null
+++ b/packages/sheets-conditional-formatting-ui/src/components/wrapper-error/WrapperError.tsx
@@ -0,0 +1,39 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 React from 'react';
+
+// TODO@gggpound: Error checking needs to be implemented by the form control
+export const WrapperError = (props: {
+ errorText: string;
+ children: React.ReactElement;
+}) => {
+ return (
+ <div style={{ position: 'relative' }}>
+ <div style={{
+ position: 'absolute',
+ zIndex: 999,
+ bottom: '-13px',
+ fontSize: '10px',
+ color: 'red',
+ }}
+ >
+ {props.errorText}
+ </div>
+ {props.children}
+ </div>
+ );
+};
diff --git a/packages/sheets-conditional-formatting-ui/src/locale/en-US.ts b/packages/sheets-conditional-formatting-ui/src/locale/en-US.ts
index c042c065b7..3e58c5abc3 100644
--- a/packages/sheets-conditional-formatting-ui/src/locale/en-US.ts
+++ b/packages/sheets-conditional-formatting-ui/src/locale/en-US.ts
@@ -168,6 +168,10 @@ const locale: typeof zhCN = {
formula: 'Formula',
none: 'None',
},
+ errorMessage: {
+ notBlank: 'Condition can not be empty',
+ formulaError: 'Wrong formula',
+ },
},
},
};
diff --git a/packages/sheets-conditional-formatting-ui/src/locale/ru-RU.ts b/packages/sheets-conditional-formatting-ui/src/locale/ru-RU.ts
index 3e9c2ae420..7b86eaaabf 100644
--- a/packages/sheets-conditional-formatting-ui/src/locale/ru-RU.ts
+++ b/packages/sheets-conditional-formatting-ui/src/locale/ru-RU.ts
@@ -167,6 +167,11 @@ const locale: typeof zhCN = {
formula: 'Формула',
none: 'Нет',
},
+ errorMessage: {
+ notBlank: 'Условие не может быть пустым',
+ formulaError: 'Ошибка формулы',
+
+ },
},
},
};
diff --git a/packages/sheets-conditional-formatting-ui/src/locale/zh-CN.ts b/packages/sheets-conditional-formatting-ui/src/locale/zh-CN.ts
index 9d8d525ab7..c55859c5e6 100644
--- a/packages/sheets-conditional-formatting-ui/src/locale/zh-CN.ts
+++ b/packages/sheets-conditional-formatting-ui/src/locale/zh-CN.ts
@@ -167,6 +167,10 @@ const locale = {
formula: '公式',
none: '无',
},
+ errorMessage: {
+ notBlank: '条件不能为空',
+ formulaError: '公式错误',
+ },
},
},
};