-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStickyNoteModal.tsx
More file actions
153 lines (142 loc) · 5.3 KB
/
StickyNoteModal.tsx
File metadata and controls
153 lines (142 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React from "react";
import getColorConfiguration from "../../../common/utils/getColorConfiguration";
import { CodeEditor } from "../../../extensions";
import { ReactFlowHotkeyContext } from "../extensions/ReactFlowHotkeyContext";
import {
Button,
CodeEditorProps,
FieldItem,
Icon,
SimpleDialog,
SimpleDialogProps,
Tag,
TagList,
} from "./../../../index";
export type StickyNoteModalTranslationKeys = "modalTitle" | "noteLabel" | "colorLabel" | "saveButton" | "cancelButton";
export type StickyNoteMetadataType = { note: string; color: string };
export interface StickyNoteModalProps {
/**
* sticky data containing the sticky note and the selected color
*/
metaData?: StickyNoteMetadataType;
/**
* utility to close the sticky note modal when cancelled as well as closed also
*/
onClose: () => void;
/**
* utility to save recently entered metadata for sticky
* note and add on to the canvas
*/
onSubmit: (data: StickyNoteMetadataType) => void;
/**
* translation utility for language compatibility
*/
translate: (key: StickyNoteModalTranslationKeys) => string;
/**
* Forward other properties to the `SimpleModal` element that is used for this dialog.
*/
simpleDialogProps?: Omit<SimpleDialogProps, "size" | "title" | "hasBorder" | "isOpen" | "onClose" | "actions" | "children">;
/**
* Code editor props
*/
codeEditorProps?: Omit<CodeEditorProps, "defaultValue" | "onChange" | "preventLinuNumbers" | "id" | "name">;
}
export const StickyNoteModal: React.FC<StickyNoteModalProps> = React.memo(
({ metaData, onClose, onSubmit, translate, simpleDialogProps, codeEditorProps }) => {
const refNote = React.useRef<string>(metaData?.note ?? "");
const [color, setSelectedColor] = React.useState<string>(metaData?.color ?? "");
const noteColors: [string, string][] = Object.entries(getColorConfiguration("stickynotes")).map(
([key, value]) => [key, value as string]
);
const { disableHotKeys } = React.useContext(ReactFlowHotkeyContext);
React.useEffect(() => {
disableHotKeys(true);
return () => {
disableHotKeys(false);
};
}, []);
React.useEffect(() => {
if (!color && noteColors[0][1]) {
setSelectedColor(noteColors[0][1]);
}
}, [color, noteColors]);
const predefinedColorsMenu = (
<TagList>
{noteColors &&
noteColors.map(([colorName, colorValue]) => {
const selectedFeedback =
color === colorValue
? {
icon: <Icon name="state-checkedsimple" />,
large: true,
}
: {};
return (
<Tag
round
onClick={() => setSelectedColor(colorValue)}
backgroundColor={colorValue}
{...selectedFeedback}
key={colorName}
/>
);
})}
</TagList>
);
return (
<SimpleDialog
size="small"
title={translate("modalTitle")}
hasBorder
isOpen
onClose={onClose}
actions={[
<Button
key="submit"
data-test-id="sticky-submit-btn"
affirmative
onClick={() => {
onSubmit({ note: refNote.current, color: color || "#444444" });
onClose();
}}
>
{translate("saveButton")}
</Button>,
<Button key="cancel" onClick={onClose}>
{translate("cancelButton")}
</Button>,
]}
{...simpleDialogProps}
>
<FieldItem
key="note"
labelProps={{
htmlFor: "noteinput",
text: translate("noteLabel"),
}}
>
<CodeEditor
name={translate("noteLabel")}
id={"sticky-note-input"}
mode="markdown"
useToolbar
onChange={(value) => {
refNote.current = value;
}}
defaultValue={refNote.current}
{...codeEditorProps}
/>
</FieldItem>
<FieldItem
key="color"
labelProps={{
htmlFor: "colorinput",
text: translate("colorLabel"),
}}
>
{predefinedColorsMenu}
</FieldItem>
</SimpleDialog>
);
}
);