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(sheets-conditional-formatting-ui): enable stop-if-true toggles in rule list (#8097)

白熱73db408f9eff
diff --git a/packages/sheets-conditional-formatting-ui/src/views/panel/RuleList.tsx b/packages/sheets-conditional-formatting-ui/src/views/panel/RuleList.tsx
index 456f9de355..358c98cf3b 100644
--- a/packages/sheets-conditional-formatting-ui/src/views/panel/RuleList.tsx
+++ b/packages/sheets-conditional-formatting-ui/src/views/panel/RuleList.tsx
@@ -20,6 +20,7 @@ import type {
IConditionFormattingRule,
IDeleteCfCommandParams,
IMoveCfCommandParams,
+ ISetCfCommandParams,
} from '@univerjs/sheets-conditional-formatting';
import type { ComponentType } from 'react';
import {
@@ -48,6 +49,7 @@ import {
DeleteConditionalRuleMutation,
MoveCfCommand,
MoveConditionalRuleMutation,
+ SetCfCommand,
SetConditionalRuleMutation,
} from '@univerjs/sheets-conditional-formatting';
import { useHighlightRange } from '@univerjs/sheets-ui';
@@ -167,7 +169,7 @@ export function RuleList(props: IRuleListProps) {
if (!unitId || !subUnitId) {
throw new Error('No active sheet found');
}
- commandService.executeCommand(DeleteCfCommand.id, { unitId, subUnitId, cfId: rule.cfId } as IDeleteCfCommandParams);
+ return commandService.executeCommand(DeleteCfCommand.id, { unitId, subUnitId, cfId: rule.cfId } as IDeleteCfCommandParams);
};
const handleDragStart = (_layout: unknown, from: { y: number }) => {
@@ -193,7 +195,7 @@ export function RuleList(props: IRuleListProps) {
return;
}
if (cfId !== targetCfId) {
- commandService.executeCommand(MoveCfCommand.id, { unitId, subUnitId, start: { id: cfId, type: 'self' }, end: { id: targetCfId, type: to.y > from.y ? 'after' : 'before' } } as IMoveCfCommandParams);
+ return commandService.executeCommand(MoveCfCommand.id, { unitId, subUnitId, start: { id: cfId, type: 'self' }, end: { id: targetCfId, type: to.y > from.y ? 'after' : 'before' } } as IMoveCfCommandParams);
}
};
@@ -203,12 +205,10 @@ export function RuleList(props: IRuleListProps) {
const handleClear = () => {
if (selectValue === '2') {
- commandService.executeCommand(ClearWorksheetCfCommand.id);
+ return commandService.executeCommand(ClearWorksheetCfCommand.id);
} else if (selectValue === '1') {
const list = ruleList.map((rule) => ({ unitId, subUnitId, cfId: rule.cfId }));
- list.forEach((config) => {
- commandService.executeCommand(DeleteCfCommand.id, config);
- });
+ return Promise.all(list.map((config) => commandService.executeCommand(DeleteCfCommand.id, config)));
}
};
const ruleListByPermissionCheck = useMemo(() => {
@@ -344,12 +344,16 @@ export function RuleList(props: IRuleListProps) {
<ConditionalFormattingRulePreview rule={rule.rule} />
</div>
{rule.rule.type === CFRuleType.highlightCell && (
- <div className="univer-pointer-events-none univer-mt-2 univer-w-full">
+ <div className="univer-mt-2 univer-w-full" onClick={(event) => event.stopPropagation()}>
<Checkbox
className="univer-max-w-full"
contentClassName="univer-min-w-0"
checked={!!rule.stopIfTrue}
- disabled
+ onChange={(checked) => commandService.executeCommand<ISetCfCommandParams>(SetCfCommand.id, {
+ unitId,
+ subUnitId,
+ rule: { ...rule, stopIfTrue: !!checked },
+ })}
>
{localeService.t<LocaleKey>('sheets-conditional-formatting-ui.panel.stopIfTrue')}
</Checkbox>
@@ -367,8 +371,8 @@ export function RuleList(props: IRuleListProps) {
})}
onClick={(e) => {
e.stopPropagation();
- handleDelete(rule);
setCurrentRuleRanges([]);
+ return handleDelete(rule);
}}
>
<DeleteIcon />
diff --git a/packages/sheets-conditional-formatting-ui/src/views/panel/__tests__/RuleList.spec.tsx b/packages/sheets-conditional-formatting-ui/src/views/panel/__tests__/RuleList.spec.tsx
index a3dcc896e3..9d9d66d8dc 100644
--- a/packages/sheets-conditional-formatting-ui/src/views/panel/__tests__/RuleList.spec.tsx
+++ b/packages/sheets-conditional-formatting-ui/src/views/panel/__tests__/RuleList.spec.tsx
@@ -17,7 +17,7 @@
import type { IRange } from '@univerjs/core';
import type { IConditionFormattingRule } from '@univerjs/sheets-conditional-formatting';
import type { Root } from 'react-dom/client';
-import { BooleanNumber, LocaleService, LocaleType, RANGE_TYPE } from '@univerjs/core';
+import { BooleanNumber, LocaleService, LocaleType, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core';
import { SetSelectionsOperation } from '@univerjs/sheets';
import {
AddCfCommand,
@@ -26,6 +26,7 @@ import {
CFSubRuleType,
DeleteCfCommand,
MoveCfCommand,
+ SetCfCommand,
} from '@univerjs/sheets-conditional-formatting';
import { IMarkSelectionService } from '@univerjs/sheets-ui';
import { RediContext } from '@univerjs/ui';
@@ -91,29 +92,10 @@ async function createRuleListTestBed(stopIfTrue = false) {
testBed.commandService.registerCommand(AddCfCommand);
testBed.commandService.registerCommand(DeleteCfCommand);
testBed.commandService.registerCommand(MoveCfCommand);
+ testBed.commandService.registerCommand(SetCfCommand);
testBed.commandService.registerCommand(SetSelectionsOperation);
testBed.get(LocaleService).load({
[LocaleType.ZH_CN]: {
- sheets: {
- conditional: {
- formatting: {
- ui: {
- panel: {
- managerRuleSelect: 'Show {0}',
- workSheet: 'Worksheet',
- selectedRange: 'Selected range',
- createRule: 'Create rule',
- clear: 'Clear rules',
- },
- preview: {
- describe: {
- greaterThan: 'Greater than {0}',
- },
- },
- },
- },
- },
- },
'sheets-conditional-formatting-ui': {
panel: {
managerRuleSelect: 'Show {0}',
@@ -163,7 +145,7 @@ describe('RuleList', () => {
currentTestBed = undefined;
});
- it.each([false, true])('opens the rule from its read-only stopIfTrue=%s display without toggling it', async (stopIfTrue) => {
+ it.each([false, true])('toggles stopIfTrue=%s from the rule list with undo and redo without opening the editor', async (stopIfTrue) => {
currentTestBed = await createRuleListTestBed(stopIfTrue);
container = document.createElement('div');
document.body.appendChild(container);
@@ -182,19 +164,31 @@ describe('RuleList', () => {
const row = container.querySelector('[data-draggable-list-item-id="cf-active"]')!;
const label = row.querySelector('[data-u-comp="checkbox"]')!;
const checkbox = label.querySelector('input')!;
- expect(checkbox.checked).toBe(stopIfTrue);
- expect(checkbox.disabled).toBe(true);
+ const originalRule = currentTestBed.ruleModel.getRule(currentTestBed.unitId, currentTestBed.subUnitId, 'cf-active')!;
- // The display's icon and label both belong to the surrounding rule's edit target.
const target = stopIfTrue ? label.lastElementChild! : checkbox.parentElement!.lastElementChild!;
await act(async () => {
target.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await Promise.resolve();
});
- expect(openedRuleIds).toEqual(['cf-active']);
+ expect(currentTestBed.ruleModel.getRule(currentTestBed.unitId, currentTestBed.subUnitId, 'cf-active'))
+ .toEqual({ ...originalRule, stopIfTrue: !stopIfTrue });
+ expect(openedRuleIds).toEqual([]);
+ expect(checkbox.checked).toBe(!stopIfTrue);
+
+ await act(async () => {
+ expect(await currentTestBed!.commandService.executeCommand(UndoCommand.id)).toBe(true);
+ });
+ expect(currentTestBed.ruleModel.getRule(currentTestBed.unitId, currentTestBed.subUnitId, 'cf-active')).toEqual(originalRule);
expect(checkbox.checked).toBe(stopIfTrue);
- expect(currentTestBed.ruleModel.getRule(currentTestBed.unitId, currentTestBed.subUnitId, 'cf-active')?.stopIfTrue).toBe(stopIfTrue);
+
+ await act(async () => {
+ expect(await currentTestBed!.commandService.executeCommand(RedoCommand.id)).toBe(true);
+ });
+ expect(currentTestBed.ruleModel.getRule(currentTestBed.unitId, currentTestBed.subUnitId, 'cf-active'))
+ .toEqual({ ...originalRule, stopIfTrue: !stopIfTrue });
+ expect(checkbox.checked).toBe(!stopIfTrue);
});
it('shows only conditional formatting rules intersecting the current sheet selection in selected-range mode', async () => {