Skip to content

Commit

Permalink
fix(workbench-layout): 清空操作增加确认
Browse files Browse the repository at this point in the history
Closes DESKTOP-395
  • Loading branch information
willc001 committed Jul 25, 2024
1 parent f50f5f8 commit e484026
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
23 changes: 21 additions & 2 deletions bricks/advanced/src/workbench-layout/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { getByTestId, fireEvent } from "@testing-library/dom";
import { describe, test, expect, jest } from "@jest/globals";
import { act } from "react-dom/test-utils";
import * as utilsGeneral from "@next-core/utils/general";
import "./";
import type { EoWorkbenchLayout } from "./index.js";

jest.mock("@next-core/theme", () => ({}));
jest.mock("@next-core/utils/general", () => {
const mockedUnwrapedProvider = jest.fn();

return {
...(jest.requireActual("@next-core/utils/general") as Record<
string,
unknown
>),
mockedUnwrapedProvider,
unwrapProvider: jest.fn(() => mockedUnwrapedProvider),
};
});

describe("eo-workbench-layout", () => {
test("basic usage", async () => {
Expand Down Expand Up @@ -125,8 +138,8 @@ describe("eo-workbench-layout", () => {
1,
expect.objectContaining({
detail: [
{ w: 2, h: 1, x: 0, y: 0, i: "card-1", moved: false, static: false },
{ w: 1, h: 1, x: 0, y: 1, i: "card-2", moved: false, static: false },
{ w: 2, h: 1, x: 0, y: 0, i: "card-1" },
{ w: 1, h: 1, x: 0, y: 1, i: "card-2" },
],
})
);
Expand Down Expand Up @@ -245,6 +258,11 @@ describe("eo-workbench-layout", () => {
).toBe(1);

// reset
(
utilsGeneral as unknown as {
mockedUnwrapedProvider: jest.Mock<() => Promise<void>>;
}
).mockedUnwrapedProvider.mockResolvedValueOnce();
await act(async () => {
fireEvent(
getByTestId(
Expand All @@ -253,6 +271,7 @@ describe("eo-workbench-layout", () => {
),
new CustomEvent("action.click", { detail: { event: "clear" } })
);
await (global as any).flushPromises();
});

expect(
Expand Down
22 changes: 17 additions & 5 deletions bricks/advanced/src/workbench-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, {
} from "react";
import { EventEmitter, createDecorators } from "@next-core/element";
import { ReactNextElement, wrapBrick } from "@next-core/react-element";
import { unwrapProvider } from "@next-core/utils/general";
import { UseSingleBrickConf } from "@next-core/types";
import { ReactUseBrick } from "@next-core/react-runtime";
import { auth } from "@next-core/easyops-runtime";
Expand Down Expand Up @@ -37,6 +38,7 @@ import type {
CheckboxOptionType,
CheckboxProps,
} from "@next-bricks/form/checkbox";
import type { showDialog as _showDialog } from "@next-bricks/basic/data-providers/show-dialog/show-dialog";
import { SimpleAction } from "@next-bricks/basic/actions";

const { defineElement, property, event, method } = createDecorators();
Expand Down Expand Up @@ -75,6 +77,7 @@ const WrappedCheckbox = wrapBrick<
>("eo-checkbox", {
onChange: "change",
});
const showDialog = unwrapProvider<typeof _showDialog>("basic.show-dialog");

export interface EoWorkbenchLayoutProps {
cardTitle?: string;
Expand Down Expand Up @@ -121,11 +124,16 @@ export const EoWorkbenchLayoutComponent = forwardRef<
const layoutCacheRef = useRef<Layout[]>();
const layoutWrapperRef = useRef<HTMLDivElement>(null);

const [layouts, setLayouts] = useState<Layout[]>(layoutsProps ?? []);
const [layouts, _setLayouts] = useState<Layout[]>(layoutsProps ?? []);
const [cols, setCols] = useState<number>(3);
const [layoutWrapperStyle, setLayoutWrapperStyle] =
useState<React.CSSProperties>();

const setLayouts = useCallback((layouts: Layout[]) => {
layoutCacheRef.current = layouts;
_setLayouts(layouts);
}, []);

useImperativeHandle(ref, () => ({
setLayouts,
}));
Expand Down Expand Up @@ -171,7 +179,7 @@ export const EoWorkbenchLayoutComponent = forwardRef<
}
}
if (!isAllowAction) {
setLayouts((items) =>
_setLayouts((items) =>

Check warning on line 182 in bricks/advanced/src/workbench-layout/index.tsx

View check run for this annotation

Codecov / codecov/patch

bricks/advanced/src/workbench-layout/index.tsx#L182

Added line #L182 was not covered by tests
items?.map((item) => {
const matchLayout = layoutCacheRef.current?.find(
(layout) => getRealKey(layout.i) === getRealKey(item.i)
Expand Down Expand Up @@ -206,7 +214,7 @@ export const EoWorkbenchLayoutComponent = forwardRef<
y: Infinity,
}));

setLayouts((layouts) => {
_setLayouts((layouts) => {
return layouts
.filter((layout) => checkedKeys.includes(getRealKey(layout.i)))
.concat(addItems);
Expand Down Expand Up @@ -237,7 +245,11 @@ export const EoWorkbenchLayoutComponent = forwardRef<

switch (event) {
case "clear":
handleClearLayout();
showDialog({
type: "confirm",
title: "清空确认",
content: "将清空所有卡片,从零开始编辑,该操作无法撤回。",
}).then(handleClearLayout);
break;
default:
onActionClick?.(
Expand Down Expand Up @@ -381,7 +393,7 @@ export const EoWorkbenchLayoutComponent = forwardRef<
? [{ text: "另存为模板", event: "saveAsTemplate" }]
: []),
{ text: "从模版加载", event: "loadFromTemplate" },
{ text: "清除", danger: true, event: "clear" },
{ text: "清空所有", danger: true, event: "clear" },
]}
onActionClick={(e) => {
handleActionClick(e.detail);
Expand Down

0 comments on commit e484026

Please sign in to comment.