@wavv/ui 2.3.7-alpha.1 → 2.3.7
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/build/components/DropZone.js +39 -13
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@ import { marginProps, paddingProps, widthHeightProps } from "./helpers/styledPro
|
|
|
9
9
|
const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled, invalid, errorMessage, supportedFormats, maxFileSize, acceptedFileTypes, allowsMultiple, defaultCamera, acceptDirectory, onDrop, onFilesAdded, onFilesRemoved, width, margin, marginTop, marginRight, marginBottom, marginLeft, ...rest })=>{
|
|
10
10
|
const [files, setFiles] = useState([]);
|
|
11
11
|
const [sizeValidationError, setSizeValidationError] = useState(null);
|
|
12
|
+
const [typeValidationError, setTypeValidationError] = useState(null);
|
|
12
13
|
const instanceId = useId();
|
|
13
14
|
const hasFileCallbacks = !!onFilesAdded || !!onFilesRemoved;
|
|
14
15
|
const shouldManageFiles = showFileList || hasFileCallbacks;
|
|
@@ -51,7 +52,7 @@ const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled,
|
|
|
51
52
|
const { valid, oversized } = validateFileSizes(newFileList);
|
|
52
53
|
if (oversized.length > 0) {
|
|
53
54
|
const oversizedNames = oversized.map((file)=>file.name).join(', ');
|
|
54
|
-
setSizeValidationError(
|
|
55
|
+
setSizeValidationError(`${oversizedNames} ${1 === oversized.length ? 'exceeds' : 'exceed'} maximum file size of ${maxFileSize}`);
|
|
55
56
|
} else setSizeValidationError(null);
|
|
56
57
|
if (0 === valid.length) return;
|
|
57
58
|
const newEntries = valid.map((file, index)=>({
|
|
@@ -68,7 +69,17 @@ const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled,
|
|
|
68
69
|
};
|
|
69
70
|
const handleFileDrop = async (event)=>{
|
|
70
71
|
let fileItems = event.items.filter((file)=>'file' === file.kind);
|
|
71
|
-
if (acceptedFileTypes && acceptedFileTypes.length > 0)
|
|
72
|
+
if (acceptedFileTypes && acceptedFileTypes.length > 0) {
|
|
73
|
+
const allFileItems = [
|
|
74
|
+
...fileItems
|
|
75
|
+
];
|
|
76
|
+
fileItems = fileItems.filter((file)=>matchesFileTypes(file, acceptedFileTypes));
|
|
77
|
+
const unsupportedItems = allFileItems.filter((file)=>!matchesFileTypes(file, acceptedFileTypes));
|
|
78
|
+
if (unsupportedItems.length > 0 && shouldManageFiles) {
|
|
79
|
+
const unsupportedNames = unsupportedItems.map((item)=>item.name || 'Unknown file').join(', ');
|
|
80
|
+
setTypeValidationError(`${unsupportedNames} ${1 === unsupportedItems.length ? 'is not a supported file type' : 'are not supported file types'}`);
|
|
81
|
+
} else setTypeValidationError(null);
|
|
82
|
+
} else setTypeValidationError(null);
|
|
72
83
|
if (!allowsMultiple && fileItems.length > 1) fileItems = fileItems.slice(0, 1);
|
|
73
84
|
if (shouldManageFiles) {
|
|
74
85
|
const filePromises = fileItems.map((item)=>{
|
|
@@ -103,6 +114,14 @@ const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled,
|
|
|
103
114
|
};
|
|
104
115
|
const showManagedFileList = showFileList && files.length > 0;
|
|
105
116
|
const showFileInfo = !!supportedFormats || !!maxFileSize;
|
|
117
|
+
const getErrorMessage = ()=>{
|
|
118
|
+
if (errorMessage) return errorMessage;
|
|
119
|
+
const errors = [];
|
|
120
|
+
if (typeValidationError) errors.push(typeValidationError);
|
|
121
|
+
if (sizeValidationError) errors.push(sizeValidationError);
|
|
122
|
+
return errors.length > 0 ? errors.join('. ') : null;
|
|
123
|
+
};
|
|
124
|
+
const displayError = getErrorMessage();
|
|
106
125
|
return /*#__PURE__*/ jsxs(Container, {
|
|
107
126
|
...containerProps,
|
|
108
127
|
children: [
|
|
@@ -131,12 +150,13 @@ const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled,
|
|
|
131
150
|
})
|
|
132
151
|
]
|
|
133
152
|
}),
|
|
134
|
-
|
|
135
|
-
children:
|
|
153
|
+
displayError && /*#__PURE__*/ jsx(ErrorMessageText, {
|
|
154
|
+
children: displayError
|
|
136
155
|
})
|
|
137
156
|
]
|
|
138
157
|
}),
|
|
139
158
|
showFileInfo && /*#__PURE__*/ jsxs(FileInfo, {
|
|
159
|
+
disabled: disabled,
|
|
140
160
|
children: [
|
|
141
161
|
/*#__PURE__*/ jsx(FileInfoLeft, {
|
|
142
162
|
children: supportedFormats && /*#__PURE__*/ jsxs("div", {
|
|
@@ -158,16 +178,17 @@ const DropZone_DropZone = ({ label = 'Drop here', showFileList = true, disabled,
|
|
|
158
178
|
}),
|
|
159
179
|
showManagedFileList && /*#__PURE__*/ jsx(FileList, {
|
|
160
180
|
children: files.map((file)=>/*#__PURE__*/ jsxs(FileItem, {
|
|
181
|
+
disabled: disabled,
|
|
161
182
|
children: [
|
|
162
183
|
/*#__PURE__*/ jsx(FileName, {
|
|
163
184
|
title: file.name,
|
|
164
185
|
children: file.name
|
|
165
186
|
}),
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
size: "small",
|
|
187
|
+
/*#__PURE__*/ jsx(Button, {
|
|
188
|
+
tiny: true,
|
|
169
189
|
subtle: true,
|
|
170
190
|
secondary: true,
|
|
191
|
+
icon: "close",
|
|
171
192
|
onClick: ()=>handleRemoveFile(file.id)
|
|
172
193
|
})
|
|
173
194
|
]
|
|
@@ -230,10 +251,11 @@ const ErrorMessageText = styled(StyledText)(({ theme })=>({
|
|
|
230
251
|
}));
|
|
231
252
|
const FileList = styled.div(({ theme })=>({
|
|
232
253
|
display: 'flex',
|
|
233
|
-
flexDirection: '
|
|
254
|
+
flexDirection: 'row',
|
|
255
|
+
flexWrap: 'wrap',
|
|
234
256
|
gap: theme.size.xs
|
|
235
257
|
}));
|
|
236
|
-
const FileItem = styled.div(({ theme })=>({
|
|
258
|
+
const FileItem = styled.div(({ theme, disabled })=>({
|
|
237
259
|
display: 'flex',
|
|
238
260
|
alignItems: 'center',
|
|
239
261
|
justifyContent: 'space-between',
|
|
@@ -241,23 +263,27 @@ const FileItem = styled.div(({ theme })=>({
|
|
|
241
263
|
padding: '4px 8px',
|
|
242
264
|
paddingRight: 4,
|
|
243
265
|
backgroundColor: theme.scale0,
|
|
244
|
-
borderRadius: 4
|
|
266
|
+
borderRadius: 4,
|
|
267
|
+
opacity: disabled ? 0.4 : 1,
|
|
268
|
+
pointerEvents: disabled ? 'none' : 'auto',
|
|
269
|
+
userSelect: disabled ? 'none' : 'auto'
|
|
245
270
|
}));
|
|
246
271
|
const FileName = styled.div(({ theme })=>({
|
|
247
272
|
fontSize: theme.font.size.sm,
|
|
248
|
-
lineHeight: '
|
|
273
|
+
lineHeight: '12px',
|
|
249
274
|
flex: 1,
|
|
250
275
|
overflow: 'hidden',
|
|
251
276
|
textOverflow: 'ellipsis',
|
|
252
277
|
whiteSpace: 'nowrap'
|
|
253
278
|
}));
|
|
254
|
-
const FileInfo = styled.div(({ theme })=>({
|
|
279
|
+
const FileInfo = styled.div(({ theme, disabled })=>({
|
|
255
280
|
display: 'flex',
|
|
256
281
|
alignItems: 'center',
|
|
257
282
|
justifyContent: 'space-between',
|
|
258
283
|
gap: 16,
|
|
259
284
|
fontSize: theme.font.size.sm,
|
|
260
|
-
|
|
285
|
+
userSelect: 'none',
|
|
286
|
+
color: disabled ? theme.scale4 : theme.scale6
|
|
261
287
|
}));
|
|
262
288
|
const FileInfoLeft = styled.div({});
|
|
263
289
|
const FileInfoRight = styled.div({
|