-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathImagePreviewer.tsx
83 lines (75 loc) · 2.64 KB
/
ImagePreviewer.tsx
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
import React, { MouseEvent, useCallback } from 'react';
import { LoadingIndicator } from './LoadingIndicator';
import { Thumbnail } from './Thumbnail';
import { ThumbnailPlaceholder } from './ThumbnailPlaceholder';
import { RetryIcon } from './icons';
import type { ImageUpload } from './types';
import clsx from 'clsx';
import { useTranslationContext } from '../../context';
type CustomMouseEvent = (id: string, event: MouseEvent<HTMLButtonElement>) => void;
export type ImagePreviewerProps = {
/** The list of image uploads that should be displayed */
disabled?: boolean;
/** A callback to call with newly selected files. If this is not provided no
* `ThumbnailPlaceholder` will be displayed.
*/
handleFiles?: (files: File[]) => void;
/** A callback to call when the remove icon is clicked */
handleRemove?: CustomMouseEvent;
/** A callback to call when the retry icon is clicked */
handleRetry?: CustomMouseEvent;
imageUploads?: ImageUpload[];
/** Allow drag 'n' drop (or selection from the file dialog) of multiple files */
multiple?: boolean;
};
export const ImagePreviewer = ({
disabled = false,
handleFiles,
handleRemove,
handleRetry,
imageUploads,
multiple = true,
}: ImagePreviewerProps) => {
const { t } = useTranslationContext('ImagePreviewer');
const onClose: CustomMouseEvent = useCallback(
(id, event) => {
if (!id) return console.warn(`image.id of closed image was "null", this shouldn't happen`);
handleRemove?.(id, event);
},
[handleRemove],
);
return (
<div className='rfu-image-previewer'>
{imageUploads?.map((image) => {
const url = image.url || image.previewUri;
return (
<div
className={clsx(
'rfu-image-previewer__image',
image.state === 'finished' && 'rfu-image-previewer__image--loaded',
)}
key={image.id}
>
{image.state === 'failed' && (
<button
aria-label={t('aria/Retry upload')}
className='rfu-image-previewer__retry'
onClick={(event) => handleRetry?.(image.id, event)}
type='button'
>
<RetryIcon />
</button>
)}
{url && <Thumbnail handleClose={(event) => onClose(image.id, event)} image={url} />}
{image.state === 'uploading' && (
<LoadingIndicator backgroundColor='#ffffff19' color='#ffffffb2' />
)}
</div>
);
})}
{handleFiles && !disabled && (
<ThumbnailPlaceholder handleFiles={handleFiles} multiple={multiple} />
)}
</div>
);
};