@codeleap/web 3.21.3 → 3.21.4
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
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DropzoneFilePreviewProps, DropzoneProps, DropzoneRef } from './types'
|
|
1
|
+
import { DropzoneFilePreviewProps, DropzoneInnerFilePreviewProps, DropzoneProps, DropzoneRef } from './types'
|
|
2
2
|
import { DropzonePresets } from './styles'
|
|
3
|
-
import { IconPlaceholder, PropsOf, onUpdate, useCallback, useDefaultComponentStyle, useNestedStylesByKey, useRef } from '@codeleap/common'
|
|
3
|
+
import { IconPlaceholder, PropsOf, TypeGuards, onUpdate, useCallback, useDefaultComponentStyle, useNestedStylesByKey, useRef } from '@codeleap/common'
|
|
4
4
|
import { FileRejection, useDropzone } from 'react-dropzone'
|
|
5
5
|
import { View, ViewProps } from '../View'
|
|
6
6
|
import { Text } from '../Text'
|
|
@@ -23,29 +23,20 @@ function isImage(file) {
|
|
|
23
23
|
return file?.type?.startsWith('image/')
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const [imageUrl, setImageUrl] = useState()
|
|
41
|
-
|
|
42
|
-
const revokeImageUrl = () => {
|
|
43
|
-
URL.revokeObjectURL(imageUrl)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
onUpdate(() => {
|
|
47
|
-
if (_isImage) setImageUrl(URL.createObjectURL(file))
|
|
48
|
-
}, [file, _isImage])
|
|
26
|
+
const DefaultFilePreview = (props: DropzoneInnerFilePreviewProps) => {
|
|
27
|
+
const {
|
|
28
|
+
file,
|
|
29
|
+
variantStyles,
|
|
30
|
+
errors = [],
|
|
31
|
+
onRemove,
|
|
32
|
+
fileLeftIcon,
|
|
33
|
+
fileRightIcon,
|
|
34
|
+
fileRightIconStyles,
|
|
35
|
+
hasErrors,
|
|
36
|
+
revokeImageUrl,
|
|
37
|
+
imageUrl,
|
|
38
|
+
isPreview,
|
|
39
|
+
} = props
|
|
49
40
|
|
|
50
41
|
return (
|
|
51
42
|
<View css={[variantStyles.fileWrapper, hasErrors && variantStyles['fileWrapper:error']]}>
|
|
@@ -76,7 +67,45 @@ const FilePreview = ({
|
|
|
76
67
|
name={fileRightIcon}
|
|
77
68
|
styles={fileRightIconStyles}
|
|
78
69
|
/>
|
|
79
|
-
</View>
|
|
70
|
+
</View>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const FilePreview = (props: DropzoneFilePreviewProps) => {
|
|
75
|
+
const {
|
|
76
|
+
file,
|
|
77
|
+
withImagePreview,
|
|
78
|
+
errors = [],
|
|
79
|
+
FilePreviewComponent,
|
|
80
|
+
...rest
|
|
81
|
+
} = props
|
|
82
|
+
const hasErrors = errors?.length > 0
|
|
83
|
+
const _isImage = isImage(file)
|
|
84
|
+
const isPreview = withImagePreview && _isImage
|
|
85
|
+
|
|
86
|
+
const [imageUrl, setImageUrl] = useState()
|
|
87
|
+
|
|
88
|
+
const revokeImageUrl = () => {
|
|
89
|
+
URL.revokeObjectURL(imageUrl)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
onUpdate(() => {
|
|
93
|
+
if (_isImage) setImageUrl(URL.createObjectURL(file))
|
|
94
|
+
}, [file, _isImage])
|
|
95
|
+
|
|
96
|
+
const _FilePreview = !TypeGuards.isNil(FilePreviewComponent) ? FilePreviewComponent : DefaultFilePreview
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<_FilePreview
|
|
100
|
+
file={file}
|
|
101
|
+
hasErrors={hasErrors}
|
|
102
|
+
isPreview={isPreview}
|
|
103
|
+
imageUrl={imageUrl}
|
|
104
|
+
revokeImageUrl={revokeImageUrl}
|
|
105
|
+
errors={errors}
|
|
106
|
+
{...rest}
|
|
107
|
+
/>
|
|
108
|
+
)
|
|
80
109
|
}
|
|
81
110
|
|
|
82
111
|
const DropzoneComponent = (props: DropzoneProps, ref: React.ForwardedRef<DropzoneRef>) => {
|
|
@@ -101,6 +130,7 @@ const DropzoneComponent = (props: DropzoneProps, ref: React.ForwardedRef<Dropzon
|
|
|
101
130
|
onDrop,
|
|
102
131
|
onRemove,
|
|
103
132
|
children,
|
|
133
|
+
FilePreviewComponent,
|
|
104
134
|
...rest } = allProps
|
|
105
135
|
|
|
106
136
|
const [rejectedFilesState,
|
|
@@ -169,21 +199,25 @@ const DropzoneComponent = (props: DropzoneProps, ref: React.ForwardedRef<Dropzon
|
|
|
169
199
|
|
|
170
200
|
{hasFiles && (
|
|
171
201
|
<View css={variantStyles.filesWrapper}>
|
|
172
|
-
{acceptedFiles.map(file => (
|
|
202
|
+
{acceptedFiles.map((file, index) => (
|
|
173
203
|
<FilePreview
|
|
174
204
|
{...fileProps}
|
|
205
|
+
index={index}
|
|
175
206
|
file={file}
|
|
176
207
|
key={file.name}
|
|
177
208
|
onRemove={() => handleRemoveFile(file)}
|
|
209
|
+
FilePreviewComponent={FilePreviewComponent}
|
|
178
210
|
/>))}
|
|
179
211
|
|
|
180
|
-
{rejectedFiles.map(({ file, errors }) => (
|
|
212
|
+
{rejectedFiles.map(({ file, errors }, index) => (
|
|
181
213
|
<FilePreview
|
|
182
214
|
{...fileProps}
|
|
215
|
+
index={index}
|
|
183
216
|
key={file.name}
|
|
184
217
|
file={file}
|
|
185
218
|
errors={errors}
|
|
186
219
|
onRemove={() => handleRemoveFile(file, true)}
|
|
220
|
+
FilePreviewComponent={FilePreviewComponent}
|
|
187
221
|
/>))}
|
|
188
222
|
</View>
|
|
189
223
|
)}
|
|
@@ -35,17 +35,26 @@ export type DropzoneProps = ComponentVariants<typeof DropzonePresets> &
|
|
|
35
35
|
fileRightIcon?: IconPlaceholder
|
|
36
36
|
fileLeftIcon?: IconPlaceholder
|
|
37
37
|
withImagePreview?: boolean
|
|
38
|
+
FilePreviewComponent?: (props: DropzoneInnerFilePreviewProps) => JSX.Element
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export type DropzoneFilePreviewProps = Pick<
|
|
41
42
|
DropzoneProps,
|
|
42
|
-
'fileRightIcon' | 'fileLeftIcon' | 'withImagePreview'
|
|
43
|
+
'fileRightIcon' | 'fileLeftIcon' | 'withImagePreview' | 'FilePreviewComponent'
|
|
43
44
|
> & {
|
|
44
45
|
file: DropzoneFile
|
|
45
46
|
errors?: DropzoneFileRejection['errors']
|
|
46
47
|
variantStyles: StylesOf<DropzoneComposition>
|
|
47
48
|
onRemove?: () => void
|
|
48
49
|
fileRightIconStyles?: StylesOf<ActionIconComposition>
|
|
50
|
+
index?: number
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type DropzoneInnerFilePreviewProps = DropzoneFilePreviewProps & {
|
|
54
|
+
hasErrors: boolean
|
|
55
|
+
revokeImageUrl: () => void
|
|
56
|
+
imageUrl: string
|
|
57
|
+
isPreview: boolean
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
export type DropzoneRef = ReactDropzoneRef & {
|