UNPKG

1.31 kBJavaScriptView Raw
1import React, { useRef } from 'react';
2import PropTypes from 'prop-types';
3import { Button, Input } from 'reactstrap';
4import FilePicker from './FilePicker';
5
6const FilePickerBtn = ({
7 onClick,
8 onChange,
9 multiple,
10 name,
11 allowedFileTypes,
12 maxSize,
13 'data-testid': testId,
14 ...rest
15}) => {
16 const input = useRef(null);
17
18 const onBtnClick = (...args) => {
19 if (input.current) {
20 input.current.click();
21 }
22 if (onClick) {
23 onClick(...args);
24 }
25 };
26
27 return (
28 <>
29 <div className="d-none">
30 <FilePicker
31 tag={Input}
32 innerRef={input}
33 onChange={onChange}
34 multiple={multiple}
35 name={name}
36 allowedFileTypes={allowedFileTypes}
37 maxSize={maxSize}
38 data-testid={testId}
39 />
40 </div>
41 <Button {...rest} onClick={onBtnClick} />
42 </>
43 );
44};
45
46FilePickerBtn.propTypes = {
47 onClick: PropTypes.func,
48 onChange: PropTypes.func,
49 multiple: PropTypes.bool,
50 name: PropTypes.string,
51 color: PropTypes.string,
52 children: PropTypes.node,
53 allowedFileTypes: PropTypes.arrayOf(PropTypes.string),
54 maxSize: PropTypes.number,
55 'data-testid': PropTypes.string,
56};
57
58FilePickerBtn.defaultProps = {
59 color: 'primary',
60 children: 'Select File',
61};
62
63export default FilePickerBtn;