Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(workbench-layout): 清空操作增加确认 #1232

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} 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 @@
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 @@
>("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 @@
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 @@
}
}
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 @@
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 @@

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