-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathUploadButton.tsx
62 lines (53 loc) · 1.45 KB
/
UploadButton.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
import './UploadButton.less';
import * as React from 'react';
import { CSS_PREFIX } from '../../constants';
import SimpleButton, { SimpleButtonProps } from '../SimpleButton/SimpleButton';
export interface OwnProps {
/**
* The className which should be added.
*/
className?: string;
/**
* Object of props that should be passed to the input field.
*/
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
/**
* The onChange handler for the upload input field.
*/
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
}
export type UploadButtonProps = OwnProps & SimpleButtonProps;
/**
* Component representing an upload button. Can be used as wrapper if children
* are given. Otherwise a Simplebutton will be rendered.
*
* To use a text with the UploadButton provide a SimpleButton as children.
*
* This automatically supports uploads via drag and drop from the operating
* system.
*/
const UploadButton: React.FC<UploadButtonProps> = ({
className,
children,
onChange,
inputProps,
...passThroughProps
}) => {
const finalClassName = className
? `${className} ${CSS_PREFIX}uploadbutton`
: `${CSS_PREFIX}uploadbutton`;
const button = <SimpleButton {...passThroughProps} />;
return (
<div
className={finalClassName}
>
{children || button}
<input
type="file"
onChange={onChange}
{...inputProps}
/>
</div>
);
};
export default UploadButton;