@linzjs/windows 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "panel",
13
13
  "popout"
14
14
  ],
15
- "version": "1.1.2",
15
+ "version": "1.2.0",
16
16
  "peerDependencies": {
17
17
  "@linzjs/lui": "^17",
18
18
  "lodash-es": ">=4",
@@ -1,16 +1,26 @@
1
1
  import { Modal } from "./Modal";
2
2
  import { ModalCallback } from "./ModalContext";
3
3
  import { useShowModal } from "./useShowModal";
4
+ import { isEmpty } from "lodash-es";
4
5
  import { ReactElement } from "react";
5
6
 
6
7
  import { LuiAlertModalButtons, LuiButton, LuiIcon } from "@linzjs/lui";
8
+ import { LuiButtonProps } from "@linzjs/lui/dist/components/LuiButton/LuiButton";
7
9
  import { IconName } from "@linzjs/lui/dist/components/LuiIcon/LuiIcon";
8
10
 
9
11
  export type WarningLevel = "success" | "info" | "warning" | "error";
10
12
 
11
- export interface PrefabModalProps extends ModalCallback<boolean> {
13
+ export interface PrefabButton<RT> {
14
+ default?: boolean;
15
+ level?: LuiButtonProps["level"];
16
+ title: string;
17
+ value: RT;
18
+ }
19
+
20
+ export interface PrefabModalProps extends ModalCallback<string | null> {
12
21
  level?: WarningLevel;
13
22
  children: ReactElement;
23
+ buttons?: PrefabButton<any>[];
14
24
  }
15
25
  export const getIconForLevel = (level: "success" | "info" | "warning" | "error"): IconName => {
16
26
  switch (level) {
@@ -25,20 +35,44 @@ export const getIconForLevel = (level: "success" | "info" | "warning" | "error")
25
35
  }
26
36
  };
27
37
 
28
- export const PrefabModal = ({ level = "warning", children, resolve }: PrefabModalProps) => {
38
+ export const PrefabModal = ({ level = "warning", children, resolve, ...props }: PrefabModalProps) => {
29
39
  const icon = getIconForLevel(level);
40
+ const buttons: PrefabButton<any>[] = props.buttons ?? [];
41
+ if (isEmpty(buttons)) {
42
+ if (level === "warning") {
43
+ buttons.push({
44
+ title: "Cancel",
45
+ value: null,
46
+ });
47
+ }
48
+ buttons.push({
49
+ title: "Continue",
50
+ value: true,
51
+ });
52
+ }
53
+
54
+ buttons.forEach((b, i) => {
55
+ if (!b.level) {
56
+ b.level = i === buttons.length - 1 ? "primary" : "secondary";
57
+ }
58
+ });
59
+
30
60
  return (
31
61
  <Modal>
32
62
  <div className={`lui-modal lui-box-shadow lui-modal-${level}`} style={{ minWidth: 400 }}>
33
63
  <LuiIcon name={icon} alt={"warning"} size={"lg"} className={"lui-msg-status-icon"} />
34
64
  {children}
35
65
  <LuiAlertModalButtons>
36
- {level === "warning" && (
37
- <LuiButton level="secondary" onClick={() => resolve(false)} buttonProps={{ "data-autofocus": true }}>
38
- Cancel
66
+ {buttons.map((pf, i) => (
67
+ <LuiButton
68
+ key={i}
69
+ level={pf.level}
70
+ onClick={() => resolve(pf.value === undefined ? pf.title : pf.value)}
71
+ buttonProps={pf.default ? { "data-autofocus": true } : undefined}
72
+ >
73
+ {pf.title}
39
74
  </LuiButton>
40
- )}
41
- <LuiButton onClick={() => resolve(true)}>Continue</LuiButton>
75
+ ))}
42
76
  </LuiAlertModalButtons>
43
77
  </div>
44
78
  </Modal>
@@ -49,17 +83,21 @@ export const usePrefabModal = () => {
49
83
  const { showModal, modalOwnerRef } = useShowModal();
50
84
  return {
51
85
  modalOwnerRef,
52
- showPrefabModal: (level: WarningLevel, title: ReactElement | string, content: ReactElement | string) => {
53
- const children = (
54
- <>
55
- {typeof title === "string" ? <h2>{title}</h2> : title}
56
- {typeof content === "string" ? <p>{content}</p> : content}
57
- </>
58
- );
59
- return showModal(PrefabModal, {
86
+ showPrefabModal: <RT extends any = boolean>(
87
+ level: WarningLevel,
88
+ title: ReactElement | string,
89
+ content: ReactElement | string,
90
+ buttons?: PrefabButton<RT>[],
91
+ ) =>
92
+ showModal(PrefabModal, {
60
93
  level,
61
- children,
62
- });
63
- },
94
+ children: (
95
+ <>
96
+ {typeof title === "string" ? <h2>{title}</h2> : title}
97
+ {typeof content === "string" ? <p>{content}</p> : content}
98
+ </>
99
+ ),
100
+ buttons,
101
+ }) as Promise<RT>,
64
102
  };
65
103
  };
@@ -20,6 +20,7 @@ export const PrefabModalUsage = () => {
20
20
  // Remember to add the modalOwnerRef!
21
21
  return (
22
22
  <div ref={modalOwnerRef} style={{ display: "flex", gap: 10 }}>
23
+ {/* Warning */}
23
24
  <button
24
25
  onClick={async () =>
25
26
  alert(
@@ -34,15 +35,31 @@ export const PrefabModalUsage = () => {
34
35
  >
35
36
  Warning Prefab-Modal
36
37
  </button>
38
+ {/* Info */}
37
39
  <button onClick={() => showPrefabModal("info", "You are a fantastic person", "Keep it up!")}>
38
40
  Info Prefab-Modal
39
41
  </button>
42
+ {/* Error */}
40
43
  <button onClick={() => showPrefabModal("error", "Something is not right", "Maybe stop doing that")}>
41
44
  Error Prefab-Modal
42
45
  </button>
46
+ {/* Success */}
43
47
  <button onClick={() => showPrefabModal("success", "You are a success", "Keep succeeding!")}>
44
48
  Success Prefab-Modal
45
49
  </button>
50
+ {/* Custom */}
51
+ <button
52
+ onClick={async () => {
53
+ const result = await showPrefabModal("success", "Custom buttons", <b>Custom buttons are customary!</b>, [
54
+ { value: null, title: "Nope" },
55
+ { value: "barple", title: "Barple" },
56
+ { value: "floople", title: "Floople", default: true },
57
+ ]);
58
+ alert(result);
59
+ }}
60
+ >
61
+ Custom buttons
62
+ </button>
46
63
  </div>
47
64
  );
48
65
  };