浏览代码

feat: create confirm dialog to use instead of window.confirm (#4256)

* feat: create confirm dialog to use instead of window.confirm

* move confirm to right

* add types

* less margin
vanilla_orig
Aakansha Doshi 3 年前
父节点
当前提交
3ff9744b39
没有帐户链接到提交者的电子邮件
共有 5 个文件被更改,包括 76 次插入51 次删除
  1. 9
    33
      src/components/ClearCanvas.tsx
  2. 10
    15
      src/components/ConfirmDialog.scss
  3. 52
    0
      src/components/ConfirmDialog.tsx
  4. 3
    2
      src/components/Dialog.tsx
  5. 2
    1
      src/locales/en.json

+ 9
- 33
src/components/ClearCanvas.tsx 查看文件

@@ -1,11 +1,10 @@
1 1
 import { useState } from "react";
2 2
 import { t } from "../i18n";
3 3
 import { useIsMobile } from "./App";
4
-import { Dialog } from "./Dialog";
5 4
 import { trash } from "./icons";
6 5
 import { ToolButton } from "./ToolButton";
7 6
 
8
-import "./ClearCanvas.scss";
7
+import ConfirmDialog from "./ConfirmDialog";
9 8
 
10 9
 const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
11 10
   const [showDialog, setShowDialog] = useState(false);
@@ -26,39 +25,16 @@ const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
26 25
       />
27 26
 
28 27
       {showDialog && (
29
-        <Dialog
30
-          onCloseRequest={toggleDialog}
28
+        <ConfirmDialog
29
+          onConfirm={() => {
30
+            onConfirm();
31
+            toggleDialog();
32
+          }}
33
+          onCancel={toggleDialog}
31 34
           title={t("clearCanvasDialog.title")}
32
-          className="clear-canvas"
33
-          small={true}
34 35
         >
35
-          <>
36
-            <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
37
-            <div className="clear-canvas-buttons">
38
-              <ToolButton
39
-                type="button"
40
-                title={t("buttons.clear")}
41
-                aria-label={t("buttons.clear")}
42
-                label={t("buttons.clear")}
43
-                onClick={() => {
44
-                  onConfirm();
45
-                  toggleDialog();
46
-                }}
47
-                data-testid="confirm-clear-canvas-button"
48
-                className="clear-canvas--confirm"
49
-              />
50
-              <ToolButton
51
-                type="button"
52
-                title={t("buttons.cancel")}
53
-                aria-label={t("buttons.cancel")}
54
-                label={t("buttons.cancel")}
55
-                onClick={toggleDialog}
56
-                data-testid="cancel-clear-canvas-button"
57
-                className="clear-canvas--cancel"
58
-              />
59
-            </div>
60
-          </>
61
-        </Dialog>
36
+          <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
37
+        </ConfirmDialog>
62 38
       )}
63 39
     </>
64 40
   );

src/components/ClearCanvas.scss → src/components/ConfirmDialog.scss 查看文件

@@ -1,22 +1,21 @@
1 1
 @import "../css/variables.module";
2 2
 
3 3
 .excalidraw {
4
-  .clear-canvas {
4
+  .confirm-dialog {
5 5
     &-buttons {
6 6
       display: flex;
7 7
       padding: 0.2rem 0;
8 8
       justify-content: flex-end;
9
+    }
10
+    .ToolIcon__icon {
11
+      min-width: 2.5rem;
12
+      width: auto;
13
+      font-size: 1rem;
14
+    }
9 15
 
10
-      .ToolIcon__icon {
11
-        min-width: 2.5rem;
12
-        width: auto;
13
-        font-size: 1rem;
14
-      }
15
-
16
-      .ToolIcon_type_button {
17
-        margin-left: 1.5rem;
18
-        padding: 0 0.5rem;
19
-      }
16
+    .ToolIcon_type_button {
17
+      margin-left: 0.8rem;
18
+      padding: 0 0.5rem;
20 19
     }
21 20
 
22 21
     &__content {
@@ -34,9 +33,5 @@
34 33
         color: $oc-white;
35 34
       }
36 35
     }
37
-
38
-    &--cancel.ToolIcon_type_button {
39
-      background-color: $oc-gray-2;
40
-    }
41 36
   }
42 37
 }

+ 52
- 0
src/components/ConfirmDialog.tsx 查看文件

@@ -0,0 +1,52 @@
1
+import { t } from "../i18n";
2
+import { Dialog, DialogProps } from "./Dialog";
3
+import { ToolButton } from "./ToolButton";
4
+
5
+import "./ConfirmDialog.scss";
6
+
7
+interface Props extends Omit<DialogProps, "onCloseRequest"> {
8
+  onConfirm: () => void;
9
+  onCancel: () => void;
10
+  confirmText?: string;
11
+  cancelText?: string;
12
+}
13
+const ConfirmDialog = (props: Props) => {
14
+  const {
15
+    onConfirm,
16
+    onCancel,
17
+    children,
18
+    confirmText = t("buttons.confirm"),
19
+    cancelText = t("buttons.cancel"),
20
+    className = "",
21
+    ...rest
22
+  } = props;
23
+  return (
24
+    <Dialog
25
+      onCloseRequest={onCancel}
26
+      small={true}
27
+      {...rest}
28
+      className={`confirm-dialog ${className}`}
29
+    >
30
+      {children}
31
+      <div className="confirm-dialog-buttons">
32
+        <ToolButton
33
+          type="button"
34
+          title={cancelText}
35
+          aria-label={cancelText}
36
+          label={cancelText}
37
+          onClick={onCancel}
38
+          className="confirm-dialog--cancel"
39
+        />
40
+        <ToolButton
41
+          type="button"
42
+          title={confirmText}
43
+          aria-label={confirmText}
44
+          label={confirmText}
45
+          onClick={onConfirm}
46
+          className="confirm-dialog--confirm"
47
+        />
48
+      </div>
49
+    </Dialog>
50
+  );
51
+};
52
+export default ConfirmDialog;

+ 3
- 2
src/components/Dialog.tsx 查看文件

@@ -10,7 +10,7 @@ import { Island } from "./Island";
10 10
 import { Modal } from "./Modal";
11 11
 import { AppState } from "../types";
12 12
 
13
-export const Dialog = (props: {
13
+export interface DialogProps {
14 14
   children: React.ReactNode;
15 15
   className?: string;
16 16
   small?: boolean;
@@ -18,7 +18,8 @@ export const Dialog = (props: {
18 18
   title: React.ReactNode;
19 19
   autofocus?: boolean;
20 20
   theme?: AppState["theme"];
21
-}) => {
21
+}
22
+export const Dialog = (props: DialogProps) => {
22 23
   const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>();
23 24
   const [lastActiveElement] = useState(document.activeElement);
24 25
   const { id } = useExcalidrawContainer();

+ 2
- 1
src/locales/en.json 查看文件

@@ -135,7 +135,8 @@
135 135
     "zenMode": "Zen mode",
136 136
     "exitZenMode": "Exit zen mode",
137 137
     "cancel": "Cancel",
138
-    "clear": "Clear"
138
+    "clear": "Clear",
139
+    "confirm": "Confirm"
139 140
   },
140 141
   "alerts": {
141 142
     "clearReset": "This will clear the whole canvas. Are you sure?",

正在加载...
取消
保存