forked from Saifullah-dev/react-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateFolder.action.jsx
More file actions
158 lines (138 loc) · 5.25 KB
/
CreateFolder.action.jsx
File metadata and controls
158 lines (138 loc) · 5.25 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
154
155
156
157
158
import { useEffect, useState } from "react";
import { useDetectOutsideClick } from "../../../hooks/useDetectOutsideClick";
import { duplicateNameHandler } from "../../../utils/duplicateNameHandler";
import NameInput from "../../../components/NameInput/NameInput";
import ErrorTooltip from "../../../components/ErrorTooltip/ErrorTooltip";
import { useFileNavigation } from "../../../contexts/FileNavigationContext";
import { useLayout } from "../../../contexts/LayoutContext";
import { validateApiCallback } from "../../../utils/validateApiCallback";
import {injectIntl} from "react-intl";
const maxNameLength = 220;
const CreateFolderAction = ({ filesViewRef, file, onCreateFolder, triggerAction, intl }) => {
const [folderName, setFolderName] = useState(file.name);
const [folderNameError, setFolderNameError] = useState(false);
const [folderErrorMessage, setFolderErrorMessage] = useState("");
const [errorXPlacement, setErrorXPlacement] = useState("right");
const [errorYPlacement, setErrorYPlacement] = useState("bottom");
const outsideClick = useDetectOutsideClick((e) => {
e.preventDefault();
e.stopPropagation();
});
const { currentFolder, currentPathFiles, setCurrentPathFiles } = useFileNavigation();
const { activeLayout } = useLayout();
// Folder name change handler function
const handleFolderNameChange = (e) => {
setFolderName(e.target.value);
setFolderNameError(false);
};
//
// Validate folder name and call "onCreateFolder" function
const handleValidateFolderName = (e) => {
e.stopPropagation();
if (e.key === "Enter") {
e.preventDefault();
handleFolderCreating();
return;
}
if (e.key === "Escape") {
e.preventDefault();
triggerAction.close();
setCurrentPathFiles((prev) => prev.filter((f) => f.key !== file.key));
return;
}
const invalidCharsRegex = /[\\/:*?"<>|]/;
if (invalidCharsRegex.test(e.key)) {
e.preventDefault();
setFolderErrorMessage(
intl.formatMessage({id: `folderErrorMessage`})+" \\ / : * ? \" < > |"
);
setFolderNameError(true);
} else {
setFolderNameError(false);
setFolderErrorMessage("");
}
};
// Auto hide error message after 7 seconds
useEffect(() => {
if (folderNameError) {
const autoHideError = setTimeout(() => {
setFolderNameError(false);
setFolderErrorMessage("");
}, 7000);
return () => clearTimeout(autoHideError);
}
}, [folderNameError]);
//
function handleFolderCreating() {
let newFolderName = folderName.trim();
const syncedCurrPathFiles = currentPathFiles.filter((f) => !(!!f.key && f.key === file.key));
const alreadyExists = syncedCurrPathFiles.find((f) => {
return f.name.toLowerCase() === newFolderName.toLowerCase();
});
if (alreadyExists) {
setFolderErrorMessage(intl.formatMessage({id: `folderNameAlreadyContains`})+'${newFolderName}');
setFolderNameError(true);
outsideClick.ref.current?.focus();
outsideClick.ref.current?.select();
outsideClick.setIsClicked(false);
return;
}
if (newFolderName === "") {
newFolderName = duplicateNameHandler(intl.formatMessage({id: `newFolder`}), true, syncedCurrPathFiles);
}
validateApiCallback(onCreateFolder, "onCreateFolder", newFolderName, currentFolder);
setCurrentPathFiles((prev) => prev.filter((f) => f.key !== file.key));
triggerAction.close();
}
//
// Folder name text selection upon visible
useEffect(() => {
outsideClick.ref.current?.focus();
outsideClick.ref.current?.select();
// Dynamic Error Message Placement based on available space
if (outsideClick.ref?.current) {
const errorMessageWidth = 292 + 8 + 8 + 5; // 8px padding on left and right + additional 5px for gap
const errorMessageHeight = 56 + 20 + 10 + 2; // 20px :before height
const filesContainer = filesViewRef.current;
const filesContainerRect = filesContainer.getBoundingClientRect();
const nameInputContainer = outsideClick.ref.current;
const nameInputContainerRect = nameInputContainer.getBoundingClientRect();
const rightAvailableSpace = filesContainerRect.right - nameInputContainerRect.left;
rightAvailableSpace > errorMessageWidth
? setErrorXPlacement("right")
: setErrorXPlacement("left");
const bottomAvailableSpace =
filesContainerRect.bottom - (nameInputContainerRect.top + nameInputContainer.clientHeight);
bottomAvailableSpace > errorMessageHeight
? setErrorYPlacement("bottom")
: setErrorYPlacement("top");
}
}, []);
//
useEffect(() => {
if (outsideClick.isClicked) {
handleFolderCreating();
}
}, [outsideClick.isClicked]);
return (
<>
<NameInput
nameInputRef={outsideClick.ref}
maxLength={maxNameLength}
value={folderName}
onChange={handleFolderNameChange}
onKeyDown={handleValidateFolderName}
onClick={(e) => e.stopPropagation()}
{...(activeLayout === "list" && { rows: 1 })}
/>
{folderNameError && (
<ErrorTooltip
message={folderErrorMessage}
xPlacement={errorXPlacement}
yPlacement={errorYPlacement}
/>
)}
</>
);
};
export default injectIntl(CreateFolderAction);