-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileSelectorButton.tsx
More file actions
103 lines (97 loc) · 2.48 KB
/
Copy pathFileSelectorButton.tsx
File metadata and controls
103 lines (97 loc) · 2.48 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
import type { ComponentPropsWithoutRef } from 'react';
import { type DropzoneOptions, useDropzone } from 'react-dropzone';
import cls from 'classnames';
import Button from '../ButtonV2/Button';
import { CLX_COMPONENT } from '../../theme/constants';
export type FileSelectorButtonProps = Omit<
DropzoneOptions,
| 'disabled'
| 'preventDropOnDocument'
| 'noClick'
| 'noDrag'
| 'noKeyboard'
| 'noDragEventsBubbling'
| 'getFilesFromEvent'
| 'useFsAccessApi'
| 'minSize'
| 'maxSize'
| 'onDrop'
| 'onDropAccepted'
| 'onDropRejected'
> & {
/** File selector button label */
label?: string;
/** Expands button to full width of the parrent component */
isExpanded?: boolean;
/** Disables the FileSelector */
isDisabled?: boolean;
/** Minimal file size in bytes */
minFileSize?: number;
/** Maximal file size in bytes */
maxFileSize?: number;
/** Callback fired on file drop or select through native dialog */
onFilesDrop?: DropzoneOptions['onDrop'];
/** Callback fired when selected files are accepted */
onFilesAccepted?: DropzoneOptions['onDropAccepted'];
/** Callback fired when selected files are rejected */
onFilesRejected?: DropzoneOptions['onDropRejected'];
};
function FileSelectorButton({
label = 'Upload',
isDisabled = false,
isExpanded = false,
multiple = false,
accept,
minFileSize,
maxFileSize,
maxFiles,
onFilesDrop,
onFilesAccepted,
onFilesRejected,
onFileDialogCancel,
onFileDialogOpen,
onDragEnter,
onDragOver,
onDragLeave,
validator,
className,
...props
}: FileSelectorButtonProps & ComponentPropsWithoutRef<'button'>) {
const { getRootProps, getInputProps } = useDropzone({
disabled: isDisabled,
noDrag: true,
multiple,
accept,
minSize: minFileSize,
maxSize: maxFileSize,
maxFiles,
onDrop: onFilesDrop,
onDropAccepted: onFilesAccepted,
onDropRejected: onFilesRejected,
onFileDialogCancel,
onFileDialogOpen,
onDragEnter,
onDragOver,
onDragLeave,
validator,
});
return (
<div className={cls(CLX_COMPONENT, className)}>
<input aria-label="upload file" aria-hidden {...getInputProps()} />
<Button
{...getRootProps({
...props,
iconStart: { name: 'upload' },
isDisabled,
variant: 'subtle',
isExpanded,
})}
role={undefined}
>
{label}
</Button>
</div>
);
}
FileSelectorButton.displayName = 'FileSelectorButton';
export default FileSelectorButton;